Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Solr
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fetchSingleRecord
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Solr record fallback loader
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2022.
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  Record
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\Record\FallbackLoader;
31
32use VuFind\Db\Service\ResourceServiceInterface;
33use VuFind\Record\RecordIdUpdater;
34use VuFindSearch\Command\SearchCommand;
35use VuFindSearch\Service;
36
37/**
38 * Solr record fallback loader
39 *
40 * @category VuFind
41 * @package  Record
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org Main Site
45 */
46class Solr extends AbstractFallbackLoader
47{
48    /**
49     * Record source
50     *
51     * @var string
52     */
53    protected $source = 'Solr';
54
55    /**
56     * Constructor
57     *
58     * @param ResourceServiceInterface $resourceService Resource database service
59     * @param RecordIdUpdater          $recordIdUpdater Record ID updater service
60     * @param Service                  $searchService   Search service
61     * @param ?string                  $legacyIdField   Solr field containing legacy IDs (null to
62     * disable lookups)
63     */
64    public function __construct(
65        ResourceServiceInterface $resourceService,
66        RecordIdUpdater $recordIdUpdater,
67        Service $searchService,
68        protected ?string $legacyIdField = 'previous_id_str_mv'
69    ) {
70        parent::__construct($resourceService, $recordIdUpdater, $searchService);
71    }
72
73    /**
74     * Fetch a single record (null if not found).
75     *
76     * @param string $id ID to load
77     *
78     * @return \VuFindSearch\Response\RecordCollectionInterface
79     */
80    protected function fetchSingleRecord($id)
81    {
82        // If there is no legacy ID field defined, short circuit the lookup:
83        if (null === $this->legacyIdField) {
84            return new \VuFindSearch\Backend\Solr\Response\Json\RecordCollection(
85                ['recordCount' => 0]
86            );
87        }
88        $query = new \VuFindSearch\Query\Query(
89            $this->legacyIdField . ':"' . addcslashes($id, '"') . '"'
90        );
91        $command = new SearchCommand('Solr', $query);
92        return $this->searchService->invoke($command)->getResult();
93    }
94}