Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.55% |
28 / 29 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
Random | |
96.55% |
28 / 29 |
|
80.00% |
4 / 5 |
8 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
setOptions | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getFromRecord | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getFromSearch | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
buildChannelFromParams | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * "Random items" channel provider. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2016, 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 | |
30 | namespace VuFind\ChannelProvider; |
31 | |
32 | use VuFind\I18n\Translator\TranslatorAwareInterface; |
33 | use VuFind\RecordDriver\AbstractBase as RecordDriver; |
34 | use VuFind\Search\Base\Params; |
35 | use VuFind\Search\Base\Results; |
36 | use VuFindSearch\Command\RandomCommand; |
37 | |
38 | use function count; |
39 | |
40 | /** |
41 | * "Random items" channel provider. |
42 | * |
43 | * @category VuFind |
44 | * @package Channels |
45 | * @author Demian Katz <demian.katz@villanova.edu> |
46 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
47 | * @link https://vufind.org/wiki/development Wiki |
48 | */ |
49 | class Random extends AbstractChannelProvider implements TranslatorAwareInterface |
50 | { |
51 | use \VuFind\I18n\Translator\TranslatorAwareTrait; |
52 | |
53 | /** |
54 | * Number of results to include in each channel. |
55 | * |
56 | * @var int |
57 | */ |
58 | protected $channelSize; |
59 | |
60 | /** |
61 | * Mode |
62 | * |
63 | * @var string |
64 | */ |
65 | protected $mode; |
66 | |
67 | /** |
68 | * Search service |
69 | * |
70 | * @var \VuFindSearch\Service |
71 | */ |
72 | protected $searchService; |
73 | |
74 | /** |
75 | * Params manager |
76 | * |
77 | * @var \VuFind\Search\Params\PluginManager |
78 | */ |
79 | protected $paramManager; |
80 | |
81 | /** |
82 | * Constructor |
83 | * |
84 | * @param \VuFindSearch\Service $search Search service |
85 | * @param \VuFind\Search\Params\PluginManager $paramManager Params manager |
86 | * @param array $options Settings (optional) |
87 | */ |
88 | public function __construct( |
89 | \VuFindSearch\Service $search, |
90 | \VuFind\Search\Params\PluginManager $paramManager, |
91 | array $options = [] |
92 | ) { |
93 | $this->searchService = $search; |
94 | $this->paramManager = $paramManager; |
95 | $this->setOptions($options); |
96 | } |
97 | |
98 | /** |
99 | * Set the options for the provider. |
100 | * |
101 | * @param array $options Options |
102 | * |
103 | * @return void |
104 | */ |
105 | public function setOptions(array $options) |
106 | { |
107 | $this->channelSize = $options['channelSize'] ?? 20; |
108 | $this->mode = $options['mode'] ?? 'retain'; |
109 | } |
110 | |
111 | /** |
112 | * Return channel information derived from a record driver object. |
113 | * |
114 | * @param RecordDriver $driver Record driver |
115 | * @param string $channelToken Token identifying a single specific channel |
116 | * to load (if omitted, all channels will be loaded) -- not used in this provider |
117 | * |
118 | * @return array |
119 | * |
120 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
121 | */ |
122 | public function getFromRecord(RecordDriver $driver, $channelToken = null) |
123 | { |
124 | $randomParams = $this->paramManager->get($driver->getSourceIdentifier()); |
125 | $channel = $this->buildChannelFromParams($randomParams); |
126 | return (count($channel['contents']) > 0) ? [$channel] : []; |
127 | } |
128 | |
129 | /** |
130 | * Return channel information derived from a search results object. |
131 | * |
132 | * @param Results $results Search results |
133 | * @param string $channelToken Token identifying a single specific channel |
134 | * to load (if omitted, all channels will be loaded) -- not used in this provider |
135 | * |
136 | * @return array |
137 | * |
138 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
139 | */ |
140 | public function getFromSearch(Results $results, $channelToken = null) |
141 | { |
142 | $params = $results->getParams(); |
143 | if ('retain' !== $this->mode) { |
144 | $randomParams = $this->paramManager->get($params->getSearchClassId()); |
145 | } else { |
146 | $randomParams = clone $params; |
147 | } |
148 | $channel = $this->buildChannelFromParams($randomParams); |
149 | return (count($channel['contents']) > 0) ? [$channel] : []; |
150 | } |
151 | |
152 | /** |
153 | * Add a new filter to an existing search results object to populate a |
154 | * channel. |
155 | * |
156 | * @param Params $params Search parameter object |
157 | * |
158 | * @return array |
159 | */ |
160 | protected function buildChannelFromParams(Params $params) |
161 | { |
162 | $retVal = [ |
163 | 'title' => $this->translate('random_recommendation_title'), |
164 | 'providerId' => $this->providerId, |
165 | ]; |
166 | $query = $params->getQuery(); |
167 | $paramBag = $params->getBackendParameters(); |
168 | $command = new RandomCommand( |
169 | $params->getSearchClassId(), |
170 | $query, |
171 | $this->channelSize, |
172 | $paramBag |
173 | ); |
174 | $random = $this->searchService->invoke($command)->getResult()->getRecords(); |
175 | $retVal['contents'] = $this->summarizeRecordDrivers($random); |
176 | return $retVal; |
177 | } |
178 | } |