Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
BrowZineBackendFactory
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 5
72
0.00% covered (danger)
0.00%
0 / 1
 __invoke
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 createBackend
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 createConnector
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 createQueryBuilder
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 createRecordCollectionFactory
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Factory for 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 Main Site
28 */
29
30namespace VuFind\Search\Factory;
31
32use Psr\Container\ContainerInterface;
33use VuFindSearch\Backend\BrowZine\Backend;
34use VuFindSearch\Backend\BrowZine\Connector;
35use VuFindSearch\Backend\BrowZine\QueryBuilder;
36use VuFindSearch\Backend\BrowZine\Response\RecordCollectionFactory;
37
38/**
39 * Factory for BrowZine backend.
40 *
41 * @category VuFind
42 * @package  Search
43 * @author   Demian Katz <demian.katz@villanova.edu>
44 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
45 * @link     https://vufind.org Main Site
46 */
47class BrowZineBackendFactory extends AbstractBackendFactory
48{
49    /**
50     * Logger.
51     *
52     * @var \Laminas\Log\LoggerInterface
53     */
54    protected $logger;
55
56    /**
57     * BrowZine configuration
58     *
59     * @var \Laminas\Config\Config
60     */
61    protected $browzineConfig;
62
63    /**
64     * Create service
65     *
66     * @param ContainerInterface $sm      Service manager
67     * @param string             $name    Requested service name (unused)
68     * @param array              $options Extra options (unused)
69     *
70     * @return Backend
71     *
72     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
73     */
74    public function __invoke(ContainerInterface $sm, $name, array $options = null)
75    {
76        $this->setup($sm);
77        $configReader = $this->serviceLocator
78            ->get(\VuFind\Config\PluginManager::class);
79        $this->browzineConfig = $configReader->get('BrowZine');
80        if ($this->serviceLocator->has(\VuFind\Log\Logger::class)) {
81            $this->logger = $this->serviceLocator->get(\VuFind\Log\Logger::class);
82        }
83
84        $connector = $this->createConnector();
85        $backend   = $this->createBackend($connector);
86
87        return $backend;
88    }
89
90    /**
91     * Create the Primo Central backend.
92     *
93     * @param Connector $connector Connector
94     *
95     * @return Backend
96     */
97    protected function createBackend(Connector $connector)
98    {
99        $backend = new Backend($connector, $this->createRecordCollectionFactory());
100        $backend->setLogger($this->logger);
101        $backend->setQueryBuilder($this->createQueryBuilder());
102        return $backend;
103    }
104
105    /**
106     * Create the Primo Central connector.
107     *
108     * @return Connector
109     */
110    protected function createConnector()
111    {
112        // Validate configuration:
113        if (empty($this->browzineConfig->General->access_token)) {
114            throw new \Exception('Missing access token in BrowZine.ini');
115        }
116        if (empty($this->browzineConfig->General->library_id)) {
117            throw new \Exception('Missing library ID in BrowZine.ini');
118        }
119
120        // Create connector:
121        $connector = new Connector(
122            $this->createHttpClient($this->browzineConfig->General->timeout ?? 30),
123            $this->browzineConfig->General->access_token,
124            $this->browzineConfig->General->library_id
125        );
126        $connector->setLogger($this->logger);
127        return $connector;
128    }
129
130    /**
131     * Create the Primo query builder.
132     *
133     * @return QueryBuilder
134     */
135    protected function createQueryBuilder()
136    {
137        $builder = new QueryBuilder();
138        return $builder;
139    }
140
141    /**
142     * Create the record collection factory
143     *
144     * @return RecordCollectionFactory
145     */
146    protected function createRecordCollectionFactory()
147    {
148        $manager = $this->serviceLocator
149            ->get(\VuFind\RecordDriver\PluginManager::class);
150        $callback = function ($data) use ($manager) {
151            $driver = $manager->get('BrowZine');
152            $driver->setRawData($data);
153            return $driver;
154        };
155        return new RecordCollectionFactory($callback);
156    }
157}