Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractFallbackLoader
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
5
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
 load
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 fetchSingleRecord
n/a
0 / 0
n/a
0 / 0
0
 updateRecord
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Abstract base class for fallback loaders
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 VuFind\RecordDriver\AbstractBase as RecordDriver;
35use VuFind\RecordDriver\Feature\PreviousUniqueIdInterface;
36use VuFindSearch\Service;
37
38/**
39 * Abstract base class for fallback loaders
40 *
41 * @category VuFind
42 * @package  Record
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 */
47abstract class AbstractFallbackLoader implements FallbackLoaderInterface
48{
49    /**
50     * Record source
51     *
52     * @var string
53     */
54    protected $source = DEFAULT_SEARCH_BACKEND;
55
56    /**
57     * Constructor
58     *
59     * @param ResourceServiceInterface $resourceService Resource database service
60     * @param RecordIdUpdater          $recordIdUpdater Record ID updater service
61     * @param Service                  $searchService   Search service
62     */
63    public function __construct(
64        protected ResourceServiceInterface $resourceService,
65        protected RecordIdUpdater $recordIdUpdater,
66        protected Service $searchService
67    ) {
68    }
69
70    /**
71     * Given an array of IDs that failed to load, try to find them using a
72     * fallback mechanism.
73     *
74     * @param array $ids IDs to load
75     *
76     * @return array
77     */
78    public function load($ids)
79    {
80        $retVal = [];
81        foreach ($ids as $id) {
82            foreach ($this->fetchSingleRecord($id) as $record) {
83                $this->updateRecord($record, $id);
84                $retVal[] = $record;
85            }
86        }
87        return $retVal;
88    }
89
90    /**
91     * Fetch a single record (null if not found).
92     *
93     * @param string $id ID to load
94     *
95     * @return \VuFindSearch\Response\RecordCollectionInterface
96     */
97    abstract protected function fetchSingleRecord($id);
98
99    /**
100     * When a record ID has changed, update the record driver and database to
101     * reflect the changes.
102     *
103     * @param RecordDriver&PreviousUniqueIdInterface $record     Record to update
104     * @param string                                 $previousId Old ID of record
105     *
106     * @return void
107     */
108    protected function updateRecord($record, $previousId)
109    {
110        // Update the record driver with knowledge of the previous identifier...
111        $record->setPreviousUniqueId($previousId);
112
113        // Update the database to replace the obsolete identifier...
114        $this->recordIdUpdater->updateRecordId($previousId, $record->getUniqueId(), $this->source);
115    }
116}