Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.00% covered (success)
96.00%
24 / 25
88.89% covered (warning)
88.89%
8 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Backend
96.00% covered (success)
96.00%
24 / 25
88.89% covered (warning)
88.89%
8 / 9
13
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 search
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 retrieve
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 setQueryBuilder
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getQueryBuilder
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getRecordCollectionFactory
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getHoldings
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getConnector
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createRecordCollection
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * WorldCat backend.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
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
28 */
29
30namespace VuFindSearch\Backend\WorldCat;
31
32use VuFindSearch\Backend\AbstractBackend;
33use VuFindSearch\ParamBag;
34use VuFindSearch\Query\AbstractQuery;
35use VuFindSearch\Response\RecordCollectionFactoryInterface;
36use VuFindSearch\Response\RecordCollectionInterface;
37
38/**
39 * WorldCat backend.
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
46 */
47class Backend extends AbstractBackend
48{
49    /**
50     * Connector.
51     *
52     * @var Connector
53     */
54    protected $connector;
55
56    /**
57     * Query builder.
58     *
59     * @var QueryBuilder
60     */
61    protected $queryBuilder = null;
62
63    /**
64     * Constructor.
65     *
66     * @param Connector                        $connector WorldCat connector
67     * @param RecordCollectionFactoryInterface $factory   Record collection factory
68     * (null for default)
69     *
70     * @return void
71     */
72    public function __construct(
73        Connector $connector,
74        RecordCollectionFactoryInterface $factory = null
75    ) {
76        if (null !== $factory) {
77            $this->setRecordCollectionFactory($factory);
78        }
79        $this->connector    = $connector;
80        $this->identifier   = null;
81    }
82
83    /**
84     * Perform a search and return record collection.
85     *
86     * @param AbstractQuery $query  Search query
87     * @param int           $offset Search offset
88     * @param int           $limit  Search limit
89     * @param ParamBag      $params Search backend parameters
90     *
91     * @return RecordCollectionInterface
92     */
93    public function search(
94        AbstractQuery $query,
95        $offset,
96        $limit,
97        ParamBag $params = null
98    ) {
99        if (null === $params) {
100            $params = new ParamBag();
101        }
102        $params->mergeWith($this->getQueryBuilder()->build($query));
103        $response   = $this->connector->search($params, $offset, $limit);
104        $collection = $this->createRecordCollection($response);
105        $this->injectSourceIdentifier($collection);
106        return $collection;
107    }
108
109    /**
110     * Retrieve a single document.
111     *
112     * @param string   $id     Document identifier
113     * @param ParamBag $params Search backend parameters
114     *
115     * @return RecordCollectionInterface
116     */
117    public function retrieve($id, ParamBag $params = null)
118    {
119        $response   = $this->connector->getRecord($id, $params);
120        $collection = $this->createRecordCollection($response);
121        $this->injectSourceIdentifier($collection);
122        return $collection;
123    }
124
125    /**
126     * Set the query builder.
127     *
128     * @param QueryBuilder $queryBuilder Query builder
129     *
130     * @return void
131     */
132    public function setQueryBuilder(QueryBuilder $queryBuilder)
133    {
134        $this->queryBuilder = $queryBuilder;
135    }
136
137    /**
138     * Return query builder.
139     *
140     * Lazy loads an empty QueryBuilder if none was set.
141     *
142     * @return QueryBuilder
143     */
144    public function getQueryBuilder()
145    {
146        if (!$this->queryBuilder) {
147            $this->queryBuilder = new QueryBuilder();
148        }
149        return $this->queryBuilder;
150    }
151
152    /**
153     * Return the record collection factory.
154     *
155     * Lazy loads a generic collection factory.
156     *
157     * @return RecordCollectionFactoryInterface
158     */
159    public function getRecordCollectionFactory()
160    {
161        if ($this->collectionFactory === null) {
162            $this->collectionFactory = new Response\XML\RecordCollectionFactory();
163        }
164        return $this->collectionFactory;
165    }
166
167    /**
168     * Get holdings information for the specified record.
169     *
170     * @param string $id Record to obtain holdings for.
171     *
172     * @throws \Exception
173     * @return \SimpleXMLElement
174     */
175    public function getHoldings($id)
176    {
177        return $this->getConnector()->getHoldings($id);
178    }
179
180    /**
181     * Return the WorldCat connector.
182     *
183     * @return Connector
184     */
185    public function getConnector()
186    {
187        return $this->connector;
188    }
189
190    /// Internal API
191
192    /**
193     * Create record collection.
194     *
195     * @param array $records Records to process
196     *
197     * @return RecordCollectionInterface
198     */
199    protected function createRecordCollection($records)
200    {
201        return $this->getRecordCollectionFactory()->factory($records);
202    }
203}