Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
LibGuidesBackendFactory | |
0.00% |
0 / 42 |
|
0.00% |
0 / 6 |
56 | |
0.00% |
0 / 1 |
getServiceName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
__invoke | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
createBackend | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
createConnector | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 | |||
createQueryBuilder | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
createRecordCollectionFactory | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * Factory for LibGuides backends. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2013. |
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 David Maus <maus@hab.de> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org Main Site |
28 | */ |
29 | |
30 | namespace VuFind\Search\Factory; |
31 | |
32 | use Psr\Container\ContainerInterface; |
33 | use VuFindSearch\Backend\LibGuides\Backend; |
34 | use VuFindSearch\Backend\LibGuides\Connector; |
35 | use VuFindSearch\Backend\LibGuides\QueryBuilder; |
36 | use VuFindSearch\Backend\LibGuides\Response\RecordCollectionFactory; |
37 | |
38 | /** |
39 | * Factory for LibGuides backends. |
40 | * |
41 | * @category VuFind |
42 | * @package Search |
43 | * @author David Maus <maus@hab.de> |
44 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
45 | * @link https://vufind.org Main Site |
46 | */ |
47 | class LibGuidesBackendFactory extends AbstractBackendFactory |
48 | { |
49 | /** |
50 | * Return the service name. |
51 | * |
52 | * @return string |
53 | */ |
54 | protected function getServiceName() |
55 | { |
56 | return 'LibGuides'; |
57 | } |
58 | |
59 | /** |
60 | * Logger. |
61 | * |
62 | * @var \Laminas\Log\LoggerInterface |
63 | */ |
64 | protected $logger; |
65 | |
66 | /** |
67 | * LibGuides configuration |
68 | * |
69 | * @var \Laminas\Config\Config |
70 | */ |
71 | protected $libGuidesConfig; |
72 | |
73 | /** |
74 | * Create service |
75 | * |
76 | * @param ContainerInterface $sm Service manager |
77 | * @param string $name Requested service name (unused) |
78 | * @param array $options Extra options (unused) |
79 | * |
80 | * @return Backend |
81 | * |
82 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
83 | */ |
84 | public function __invoke(ContainerInterface $sm, $name, array $options = null) |
85 | { |
86 | $this->setup($sm); |
87 | $configReader = $this->serviceLocator |
88 | ->get(\VuFind\Config\PluginManager::class); |
89 | $this->libGuidesConfig = $configReader->get($this->getServiceName()); |
90 | if ($this->serviceLocator->has(\VuFind\Log\Logger::class)) { |
91 | $this->logger = $this->serviceLocator->get(\VuFind\Log\Logger::class); |
92 | } |
93 | $connector = $this->createConnector(); |
94 | $backend = $this->createBackend($connector); |
95 | return $backend; |
96 | } |
97 | |
98 | /** |
99 | * Create the LibGuides backend. |
100 | * |
101 | * @param Connector $connector Connector |
102 | * |
103 | * @return Backend |
104 | */ |
105 | protected function createBackend(Connector $connector) |
106 | { |
107 | $defaultSearch = $this->libGuidesConfig->General->defaultSearch ?? null; |
108 | $backend = new Backend( |
109 | $connector, |
110 | $this->createRecordCollectionFactory(), |
111 | $defaultSearch |
112 | ); |
113 | $backend->setLogger($this->logger); |
114 | $backend->setQueryBuilder($this->createQueryBuilder()); |
115 | return $backend; |
116 | } |
117 | |
118 | /** |
119 | * Create the LibGuides connector. |
120 | * |
121 | * @return Connector |
122 | */ |
123 | protected function createConnector() |
124 | { |
125 | // Load credentials: |
126 | $iid = $this->libGuidesConfig->General->iid ?? null; |
127 | |
128 | // Pick version: |
129 | $ver = $this->libGuidesConfig->General->version ?? 1; |
130 | |
131 | // Get base URI, if available: |
132 | $baseUrl = $this->libGuidesConfig->General->baseUrl ?? null; |
133 | |
134 | // Optionally parse the resource description |
135 | $displayDescription = $this->libGuidesConfig->General->displayDescription ?? false; |
136 | |
137 | // Create connector: |
138 | $connector = new Connector( |
139 | $iid, |
140 | $this->createHttpClient($this->libGuidesConfig->General->timeout ?? 30), |
141 | $ver, |
142 | $baseUrl, |
143 | $displayDescription |
144 | ); |
145 | $connector->setLogger($this->logger); |
146 | return $connector; |
147 | } |
148 | |
149 | /** |
150 | * Create the LibGuides query builder. |
151 | * |
152 | * @return QueryBuilder |
153 | */ |
154 | protected function createQueryBuilder() |
155 | { |
156 | $builder = new QueryBuilder(); |
157 | return $builder; |
158 | } |
159 | |
160 | /** |
161 | * Create the record collection factory |
162 | * |
163 | * @return RecordCollectionFactory |
164 | */ |
165 | protected function createRecordCollectionFactory() |
166 | { |
167 | $manager = $this->serviceLocator |
168 | ->get(\VuFind\RecordDriver\PluginManager::class); |
169 | $callback = function ($data) use ($manager) { |
170 | $driver = $manager->get($this->getServiceName()); |
171 | $driver->setRawData($data); |
172 | return $driver; |
173 | }; |
174 | return new RecordCollectionFactory($callback); |
175 | } |
176 | } |