Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
78.38% |
29 / 37 |
|
80.00% |
8 / 10 |
CRAP | |
0.00% |
0 / 1 |
Backend | |
78.38% |
29 / 37 |
|
80.00% |
8 / 10 |
19.92 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
search | |
68.18% |
15 / 22 |
|
0.00% |
0 / 1 |
5.81 | |||
retrieve | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setQueryBuilder | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getQueryBuilder | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getRecordCollectionFactory | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getConnector | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
lookupDoi | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
lookupIssns | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
createRecordCollection | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * BrowZine backend. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2017. |
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 Search |
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 |
28 | */ |
29 | |
30 | namespace VuFindSearch\Backend\BrowZine; |
31 | |
32 | use VuFindSearch\Backend\AbstractBackend; |
33 | use VuFindSearch\Backend\Exception\BackendException; |
34 | use VuFindSearch\ParamBag; |
35 | use VuFindSearch\Query\AbstractQuery; |
36 | use VuFindSearch\Response\RecordCollectionFactoryInterface; |
37 | use VuFindSearch\Response\RecordCollectionInterface; |
38 | |
39 | use function array_slice; |
40 | use function count; |
41 | use function is_array; |
42 | |
43 | /** |
44 | * BrowZine backend. |
45 | * |
46 | * @category VuFind |
47 | * @package Search |
48 | * @author Demian Katz <demian.katz@villanova.edu> |
49 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
50 | * @link https://vufind.org |
51 | */ |
52 | class Backend extends AbstractBackend |
53 | { |
54 | /** |
55 | * Connector. |
56 | * |
57 | * @var Connector |
58 | */ |
59 | protected $connector; |
60 | |
61 | /** |
62 | * Query builder. |
63 | * |
64 | * @var QueryBuilder |
65 | */ |
66 | protected $queryBuilder = null; |
67 | |
68 | /** |
69 | * Constructor. |
70 | * |
71 | * @param Connector $connector BrowZine connector |
72 | * @param RecordCollectionFactoryInterface $factory Record collection factory |
73 | * (null for default) |
74 | * |
75 | * @return void |
76 | */ |
77 | public function __construct( |
78 | Connector $connector, |
79 | RecordCollectionFactoryInterface $factory = null |
80 | ) { |
81 | if (null !== $factory) { |
82 | $this->setRecordCollectionFactory($factory); |
83 | } |
84 | $this->connector = $connector; |
85 | } |
86 | |
87 | /** |
88 | * Perform a search and return record collection. |
89 | * |
90 | * @param AbstractQuery $query Search query |
91 | * @param int $offset Search offset |
92 | * @param int $limit Search limit |
93 | * @param ParamBag $params Search backend parameters |
94 | * |
95 | * @return RecordCollectionInterface |
96 | */ |
97 | public function search( |
98 | AbstractQuery $query, |
99 | $offset, |
100 | $limit, |
101 | ParamBag $params = null |
102 | ) { |
103 | $baseParams = $this->getQueryBuilder()->build($query); |
104 | if (null !== $params) { |
105 | $baseParams->mergeWith($params); |
106 | } |
107 | try { |
108 | $response = $this->connector |
109 | ->search(current($baseParams->get('query'))); |
110 | } catch (\Exception $e) { |
111 | throw new BackendException( |
112 | $e->getMessage(), |
113 | $e->getCode(), |
114 | $e |
115 | ); |
116 | } |
117 | // Use array_values and array_filter to strip any nulls out of the |
118 | // response (these are present sometimes due to an apparent API bug) |
119 | $results = isset($response['data']) && is_array($response['data']) |
120 | ? array_values(array_filter($response['data'])) : []; |
121 | $collection = $this->createRecordCollection( |
122 | [ |
123 | 'offset' => $offset, |
124 | 'recordCount' => count($results), |
125 | 'data' => array_slice($results, $offset, $limit), |
126 | ] |
127 | ); |
128 | $this->injectSourceIdentifier($collection); |
129 | |
130 | return $collection; |
131 | } |
132 | |
133 | /** |
134 | * Retrieve a single document. |
135 | * |
136 | * @param string $id Document identifier |
137 | * @param ParamBag $params Search backend parameters |
138 | * |
139 | * @return RecordCollectionInterface |
140 | */ |
141 | public function retrieve($id, ParamBag $params = null) |
142 | { |
143 | throw new \Exception('retrieve() not supported by BrowZine.'); |
144 | } |
145 | |
146 | /** |
147 | * Set the query builder. |
148 | * |
149 | * @param QueryBuilder $queryBuilder Query builder |
150 | * |
151 | * @return void |
152 | */ |
153 | public function setQueryBuilder(QueryBuilder $queryBuilder) |
154 | { |
155 | $this->queryBuilder = $queryBuilder; |
156 | } |
157 | |
158 | /** |
159 | * Return query builder. |
160 | * |
161 | * Lazy loads an empty QueryBuilder if none was set. |
162 | * |
163 | * @return QueryBuilder |
164 | */ |
165 | public function getQueryBuilder() |
166 | { |
167 | if (!$this->queryBuilder) { |
168 | $this->queryBuilder = new QueryBuilder(); |
169 | } |
170 | return $this->queryBuilder; |
171 | } |
172 | |
173 | /** |
174 | * Return the record collection factory. |
175 | * |
176 | * Lazy loads a generic collection factory. |
177 | * |
178 | * @return RecordCollectionFactoryInterface |
179 | */ |
180 | public function getRecordCollectionFactory() |
181 | { |
182 | if ($this->collectionFactory === null) { |
183 | $this->collectionFactory = new Response\RecordCollectionFactory(); |
184 | } |
185 | return $this->collectionFactory; |
186 | } |
187 | |
188 | /** |
189 | * Return the BrowZine connector. |
190 | * |
191 | * @return Connector |
192 | */ |
193 | public function getConnector() |
194 | { |
195 | return $this->connector; |
196 | } |
197 | |
198 | /** |
199 | * Perform a DOI lookup |
200 | * |
201 | * @param string $doi DOI |
202 | * @param bool $includeJournal Include journal data in response? |
203 | * |
204 | * @return mixed |
205 | */ |
206 | public function lookupDoi($doi, $includeJournal = false) |
207 | { |
208 | return $this->getConnector()->lookupDoi($doi, $includeJournal); |
209 | } |
210 | |
211 | /** |
212 | * Perform an ISSN lookup. |
213 | * |
214 | * @param string|array $issns ISSN(s) to look up. |
215 | * |
216 | * @return mixed |
217 | */ |
218 | public function lookupIssns($issns) |
219 | { |
220 | return $this->getConnector()->lookupIssns($issns); |
221 | } |
222 | |
223 | /// Internal API |
224 | |
225 | /** |
226 | * Create record collection. |
227 | * |
228 | * @param array $records Records to process |
229 | * |
230 | * @return RecordCollectionInterface |
231 | */ |
232 | protected function createRecordCollection($records) |
233 | { |
234 | return $this->getRecordCollectionFactory()->factory($records); |
235 | } |
236 | } |