Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
ExternalVuFind | |
0.00% |
0 / 25 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
setBaseUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
search | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | /** |
4 | * External VuFind API connection class. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2023. |
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 Connection |
25 | * @author Maccabee Levine <msl321@lehigh.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org |
28 | */ |
29 | |
30 | namespace VuFind\Connection; |
31 | |
32 | use Exception; |
33 | use Laminas\Log\LoggerAwareInterface; |
34 | |
35 | /** |
36 | * External VuFind API connection class. |
37 | * |
38 | * @category VuFind |
39 | * @package Connection |
40 | * @author Maccabee Levine <msl321@lehigh.edu> |
41 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
42 | * @link https://vufind.org |
43 | */ |
44 | class ExternalVuFind implements |
45 | \VuFindHttp\HttpServiceAwareInterface, |
46 | LoggerAwareInterface |
47 | { |
48 | use \VuFindHttp\HttpServiceAwareTrait; |
49 | use \VuFind\Log\LoggerAwareTrait; |
50 | |
51 | /** |
52 | * Base URL of the LibGuides API |
53 | * |
54 | * @var string |
55 | */ |
56 | protected $baseUrl = null; |
57 | |
58 | /** |
59 | * Set the API base URL. |
60 | * |
61 | * @param string $baseUrl The base url |
62 | * |
63 | * @return void |
64 | */ |
65 | public function setBaseUrl(string $baseUrl): void |
66 | { |
67 | $this->baseUrl = $baseUrl; |
68 | } |
69 | |
70 | /** |
71 | * Execute a search against the remote VuFind API. |
72 | * |
73 | * @param string $queryString Query string |
74 | * @param string $requestParam Request parameter for the query string |
75 | * @param int $limit Maximum number of results to return |
76 | * @param array $searchFilters Query filters |
77 | * |
78 | * @return array The JSON-decoded response from the API. |
79 | */ |
80 | public function search( |
81 | string $queryString, |
82 | string $requestParam, |
83 | int $limit, |
84 | array $searchFilters = [] |
85 | ): array { |
86 | if (!$this->baseUrl) { |
87 | $this->logError('Must call setBaseUrl() before searching.'); |
88 | return []; |
89 | } |
90 | |
91 | $params = []; |
92 | $params[] = $requestParam . '=' . urlencode($queryString); |
93 | $params[] = "limit=$limit"; |
94 | |
95 | foreach ($searchFilters as $filter) { |
96 | $params[] = 'filter[]=' . urlencode($filter); |
97 | } |
98 | |
99 | try { |
100 | $response = $this->httpService->get($this->baseUrl . '/search', $params); |
101 | } catch (Exception $ex) { |
102 | $this->logError( |
103 | 'Exception during request: ' . |
104 | $ex->getMessage() |
105 | ); |
106 | return []; |
107 | } |
108 | |
109 | if ($response->isServerError()) { |
110 | $this->logError( |
111 | 'ExternalVuFind API HTTP Error: ' . |
112 | $response->getStatusCode() |
113 | ); |
114 | return []; |
115 | } |
116 | |
117 | $responseData = trim($response->getBody()); |
118 | $arr = json_decode($responseData, true); |
119 | return $arr ?? []; |
120 | } |
121 | } |