Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractILSChannelProvider
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 7
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 setOptions
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getFromRecord
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFromSearch
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getIlsResponse
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0
 extractIdsFromResponse
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
0
 getChannel
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * Abstract base class for channel providers relying on the ILS.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2018, 2022.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Channels
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\ChannelProvider;
31
32use VuFind\I18n\Translator\TranslatorAwareInterface;
33use VuFind\RecordDriver\AbstractBase as RecordDriver;
34use VuFind\Search\Base\Results;
35use VuFindSearch\Command\RetrieveBatchCommand;
36
37use function count;
38
39/**
40 * Abstract base class for channel providers relying on the ILS.
41 *
42 * @category VuFind
43 * @package  Channels
44 * @author   Demian Katz <demian.katz@villanova.edu>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     https://vufind.org/wiki/development Wiki
47 */
48abstract class AbstractILSChannelProvider extends AbstractChannelProvider implements TranslatorAwareInterface
49{
50    use \VuFind\I18n\Translator\TranslatorAwareTrait;
51
52    /**
53     * Number of results to include in each channel.
54     *
55     * @var int
56     */
57    protected $channelSize;
58
59    /**
60     * Channel title (will be run through translator).
61     *
62     * @var string
63     */
64    protected $channelTitle = 'Please set $channelTitle property!';
65
66    /**
67     * Maximum age (in days) of results to retrieve.
68     *
69     * @var int
70     */
71    protected $maxAge;
72
73    /**
74     * ILS connection
75     *
76     * @var \VuFind\ILS\Connection
77     */
78    protected $ils;
79
80    /**
81     * Search service
82     *
83     * @var \VuFindSearch\Service
84     */
85    protected $searchService;
86
87    /**
88     * Constructor
89     *
90     * @param \VuFindSearch\Service  $search  Search service
91     * @param \VuFind\ILS\Connection $ils     ILS connection
92     * @param array                  $options Settings (optional)
93     */
94    public function __construct(
95        \VuFindSearch\Service $search,
96        \VuFind\ILS\Connection $ils,
97        array $options = []
98    ) {
99        $this->searchService = $search;
100        $this->ils = $ils;
101        $this->setOptions($options);
102    }
103
104    /**
105     * Set the options for the provider.
106     *
107     * @param array $options Options
108     *
109     * @return void
110     */
111    public function setOptions(array $options)
112    {
113        $this->channelSize = $options['channelSize'] ?? 20;
114        $this->maxAge = $options['maxAge'] ?? 30;
115    }
116
117    /**
118     * Return channel information derived from a record driver object.
119     *
120     * @param RecordDriver $driver       Record driver
121     * @param string       $channelToken Token identifying a single specific channel
122     * to load (if omitted, all channels will be loaded) -- not used in this provider
123     *
124     * @return array
125     *
126     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
127     */
128    public function getFromRecord(RecordDriver $driver, $channelToken = null)
129    {
130        return $this->getChannel();
131    }
132
133    /**
134     * Return channel information derived from a search results object.
135     *
136     * @param Results $results      Search results
137     * @param string  $channelToken Token identifying a single specific channel
138     * to load (if omitted, all channels will be loaded) -- not used in this provider
139     *
140     * @return array
141     *
142     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
143     */
144    public function getFromSearch(Results $results, $channelToken = null)
145    {
146        return $this->getChannel();
147    }
148
149    /**
150     * Retrieve data from the ILS.
151     *
152     * @return array
153     */
154    abstract protected function getIlsResponse();
155
156    /**
157     * Given one element from the ILS function's response array, extract the
158     * ID value.
159     *
160     * @param array $response Response array
161     *
162     * @return string
163     */
164    abstract protected function extractIdsFromResponse($response);
165
166    /**
167     * Recently returned channel contents are always the same; this does not
168     * care about specific records or search parameters.
169     *
170     * @return array
171     */
172    protected function getChannel()
173    {
174        // Use a callback to extract IDs from the arrays in the ILS return value:
175        $ids = array_map([$this, 'extractIdsFromResponse'], $this->getIlsResponse());
176        // No IDs means no response!
177        if (empty($ids)) {
178            return [];
179        }
180        // Look up the record drivers for the recently returned IDs:
181        $command = new RetrieveBatchCommand('Solr', $ids);
182        $records = $this->searchService->invoke($command)->getResult()->getRecords();
183        // Build the return value:
184        $retVal = [
185            'title' => $this->translate($this->channelTitle),
186            'providerId' => $this->providerId,
187            'contents' => $this->summarizeRecordDrivers($records),
188        ];
189        return (count($retVal['contents']) > 0) ? [$retVal] : [];
190    }
191}