Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.86% covered (warning)
67.86%
19 / 28
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SolrMarcRemote
67.86% covered (warning)
67.86%
19 / 28
75.00% covered (warning)
75.00%
3 / 4
15.02
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
 getMarcReader
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 verifyFullRecordIsAvailable
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 getRemoteFullrecord
40.00% covered (danger)
40.00%
6 / 15
0.00% covered (danger)
0.00%
0 / 1
10.40
1<?php
2
3/**
4 * Model for MARC records without a fullrecord in Solr. The fullrecord is being
5 * retrieved from an external source.
6 *
7 * PHP version 8
8 *
9 * Copyright (C) Leipzig University Library 2014.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 *
24 * @category VuFind
25 * @package  RecordDrivers
26 * @author   AndrĂ© Lahmann <lahmann@ub.uni-leipzig.de>
27 * @author   Ulf Seltmann <seltmann@ub.uni-leipzig.de>
28 * @author   Gregor Gawol <gawol@ub.uni-leipzig.de>
29 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
30 * @link     https://vufind.org/wiki/development:plugins:record_drivers Wiki
31 */
32
33namespace VuFind\RecordDriver;
34
35use Laminas\Log\LoggerAwareInterface as LoggerAwareInterface;
36use VuFindHttp\HttpServiceAwareInterface as HttpServiceAwareInterface;
37
38/**
39 * Model for MARC records without a fullrecord in Solr. The fullrecord is being
40 * retrieved from an external source.
41 *
42 * @category VuFind
43 * @package  RecordDrivers
44 * @author   AndrĂ© Lahmann <lahmann@ub.uni-leipzig.de>
45 * @author   Ulf Seltmann <seltmann@ub.uni-leipzig.de>
46 * @author   Gregor Gawol <gawol@ub.uni-leipzig.de>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org/wiki/development:plugins:record_drivers Wiki
49 * @link     https://vufind.org/wiki/configuration:remote_marc_records
50 */
51class SolrMarcRemote extends SolrMarc implements
52    HttpServiceAwareInterface,
53    LoggerAwareInterface
54{
55    use \VuFindHttp\HttpServiceAwareTrait;
56    use \VuFind\Log\LoggerAwareTrait;
57
58    /**
59     * Holds the URI-Pattern of the service that returns the marc binary blob by id
60     *
61     * @var string
62     */
63    protected $uriPattern = '';
64
65    /**
66     * Constructor
67     *
68     * @param \Laminas\Config\Config $mainConfig     VuFind main configuration (omit
69     * for built-in defaults)
70     * @param \Laminas\Config\Config $recordConfig   Record-specific configuration
71     * file (omit to use $mainConfig as $recordConfig)
72     * @param \Laminas\Config\Config $searchSettings Search-specific configuration
73     * file
74     *
75     * @throws \Exception
76     */
77    public function __construct(
78        $mainConfig = null,
79        $recordConfig = null,
80        $searchSettings = null
81    ) {
82        parent::__construct($mainConfig, $recordConfig, $searchSettings);
83
84        // get config values for remote fullrecord service
85        $this->uriPattern = $mainConfig->Record->remote_marc_url ?? null;
86        if (!$this->uriPattern) {
87            throw new \Exception('SolrMarcRemote baseUrl-setting missing.');
88        }
89    }
90
91    /**
92     * Get access to the MarcReader object.
93     *
94     * @return MarcReader
95     */
96    public function getMarcReader()
97    {
98        $this->verifyFullRecordIsAvailable();
99        return parent::getMarcReader();
100    }
101
102    /**
103     * Load the fullrecord field if not already loaded
104     *
105     * @return void
106     */
107    protected function verifyFullRecordIsAvailable()
108    {
109        // handle availability of fullrecord
110        if (!isset($this->fields['fullrecord'])) {
111            // retrieve fullrecord from external source
112            if (!isset($this->fields['id'])) {
113                throw new \Exception(
114                    'No unique id given for fullrecord retrieval'
115                );
116            }
117            $this->fields['fullrecord']
118                = $this->getRemoteFullrecord($this->fields['id']);
119        }
120    }
121
122    /**
123     * Retrieves the full Marcrecord from a remote service defined by uriPattern
124     *
125     * @param String $id - this record's unique identifier
126     *
127     * @return bool|string
128     * @throws \Exception
129     */
130    protected function getRemoteFullrecord($id)
131    {
132        if (empty($id)) {
133            throw new \Exception('empty id given');
134        }
135
136        if (empty($this->uriPattern)) {
137            throw new \Exception('no Marc-Server configured');
138        }
139
140        $url = sprintf($this->uriPattern, $id);
141
142        try {
143            $response = $this->httpService->get($url);
144        } catch (\Exception $e) {
145            throw new \Exception($e->getMessage());
146        }
147
148        if (!$response->isSuccess()) {
149            $this->debug(
150                'HTTP status ' . $response->getStatusCode() .
151                ' received, retrieving data for record: ' . $id
152            );
153            return false;
154        }
155
156        return $response->getBody();
157    }
158}