Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
WorldCatBackendFactory
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 5
56
0.00% covered (danger)
0.00%
0 / 1
 __invoke
0.00% covered (danger)
0.00%
0 / 11
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 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 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 WorldCat 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
30namespace VuFind\Search\Factory;
31
32use Psr\Container\ContainerInterface;
33use VuFindSearch\Backend\WorldCat\Backend;
34use VuFindSearch\Backend\WorldCat\Connector;
35use VuFindSearch\Backend\WorldCat\QueryBuilder;
36use VuFindSearch\Backend\WorldCat\Response\XML\RecordCollectionFactory;
37
38/**
39 * Factory for WorldCat 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 */
47class WorldCatBackendFactory extends AbstractBackendFactory
48{
49    /**
50     * Logger.
51     *
52     * @var \Laminas\Log\LoggerInterface
53     */
54    protected $logger;
55
56    /**
57     * VuFind configuration
58     *
59     * @var \Laminas\Config\Config
60     */
61    protected $config;
62
63    /**
64     * WorldCat configuration
65     *
66     * @var \Laminas\Config\Config
67     */
68    protected $wcConfig;
69
70    /**
71     * Create service
72     *
73     * @param ContainerInterface $sm      Service manager
74     * @param string             $name    Requested service name (unused)
75     * @param array              $options Extra options (unused)
76     *
77     * @return Backend
78     *
79     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
80     */
81    public function __invoke(ContainerInterface $sm, $name, array $options = null)
82    {
83        $this->setup($sm);
84        $this->config = $this->serviceLocator
85            ->get(\VuFind\Config\PluginManager::class)
86            ->get('config');
87        $this->wcConfig = $this->serviceLocator
88            ->get(\VuFind\Config\PluginManager::class)->get('WorldCat');
89        if ($this->serviceLocator->has(\VuFind\Log\Logger::class)) {
90            $this->logger = $this->serviceLocator->get(\VuFind\Log\Logger::class);
91        }
92        $connector = $this->createConnector();
93        $backend   = $this->createBackend($connector);
94        return $backend;
95    }
96
97    /**
98     * Create the WorldCat backend.
99     *
100     * @param Connector $connector Connector
101     *
102     * @return Backend
103     */
104    protected function createBackend(Connector $connector)
105    {
106        $backend = new Backend($connector, $this->createRecordCollectionFactory());
107        $backend->setLogger($this->logger);
108        $backend->setQueryBuilder($this->createQueryBuilder());
109        return $backend;
110    }
111
112    /**
113     * Create the WorldCat connector.
114     *
115     * @return Connector
116     */
117    protected function createConnector()
118    {
119        $wsKey = $this->config->WorldCat->apiKey ?? null;
120        $connectorOptions = isset($this->wcConfig->Connector)
121            ? $this->wcConfig->Connector->toArray() : [];
122        $connector = new Connector(
123            $wsKey,
124            $this->createHttpClient(),
125            $connectorOptions
126        );
127        $connector->setLogger($this->logger);
128        return $connector;
129    }
130
131    /**
132     * Create the WorldCat query builder.
133     *
134     * @return QueryBuilder
135     */
136    protected function createQueryBuilder()
137    {
138        $exclude = $this->config->WorldCat->OCLCCode ?? null;
139        return new QueryBuilder($exclude);
140    }
141
142    /**
143     * Create the record collection factory
144     *
145     * @return RecordCollectionFactory
146     */
147    protected function createRecordCollectionFactory()
148    {
149        $manager = $this->serviceLocator
150            ->get(\VuFind\RecordDriver\PluginManager::class);
151        $callback = function ($data) use ($manager) {
152            $driver = $manager->get('WorldCat');
153            $driver->setRawData($data);
154            return $driver;
155        };
156        return new RecordCollectionFactory($callback);
157    }
158}