Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Sfx
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 fetchLinks
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 parseLinks
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3/**
4 * SFX Link Resolver Driver
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Royal Holloway, University of London
9 *
10 * last update: 2010-10-11
11 * tested with X-Server SFX 3.2
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2,
15 * as published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
25 *
26 * @category VuFind
27 * @package  Resolver_Drivers
28 * @author   Graham Seaman <Graham.Seaman@rhul.ac.uk>
29 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
30 * @link     https://vufind.org/wiki/development:plugins:link_resolver_drivers Wiki
31 */
32
33namespace VuFind\Resolver\Driver;
34
35/**
36 * SFX Link Resolver Driver
37 *
38 * @category VuFind
39 * @package  Resolver_Drivers
40 * @author   Graham Seaman <Graham.Seaman@rhul.ac.uk>
41 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
42 * @link     https://vufind.org/wiki/development:plugins:link_resolver_drivers Wiki
43 */
44class Sfx extends AbstractBase
45{
46    /**
47     * HTTP client
48     *
49     * @var \Laminas\Http\Client
50     */
51    protected $httpClient;
52
53    /**
54     * Constructor
55     *
56     * @param string               $baseUrl    Base URL for link resolver
57     * @param \Laminas\Http\Client $httpClient HTTP client
58     */
59    public function __construct($baseUrl, \Laminas\Http\Client $httpClient)
60    {
61        parent::__construct($baseUrl);
62        $this->httpClient = $httpClient;
63    }
64
65    /**
66     * Fetch Links
67     *
68     * Fetches a set of links corresponding to an OpenURL
69     *
70     * @param string $openURL openURL (url-encoded)
71     *
72     * @return string         raw XML returned by resolver
73     */
74    public function fetchLinks($openURL)
75    {
76        // Make the call to SFX and load results
77        $url = $this->getResolverUrl(
78            'sfx.response_type=multi_obj_detailed_xml&svc.fulltext=yes&' . $openURL
79        );
80        $feed = $this->httpClient->setUri($url)->send()->getBody();
81        return $feed;
82    }
83
84    /**
85     * Parse Links
86     *
87     * Parses an XML file returned by a link resolver
88     * and converts it to a standardised format for display
89     *
90     * @param string $xmlstr Raw XML returned by resolver
91     *
92     * @return array         Array of values
93     */
94    public function parseLinks($xmlstr)
95    {
96        $records = []; // array to return
97        try {
98            $xml = new \SimpleXmlElement($xmlstr);
99        } catch (\Exception $e) {
100            return $records;
101        }
102
103        $root = $xml->xpath('//ctx_obj_targets');
104        $xml = $root[0];
105        foreach ($xml->children() as $target) {
106            $record = [];
107            $record['title'] = (string)$target->target_public_name;
108            $record['href'] = (string)$target->target_url;
109            $record['service_type'] = (string)$target->service_type;
110            if (isset($target->coverage->coverage_text)) {
111                $coverageText = & $target->coverage->coverage_text;
112                $record['coverage'] = (string)$coverageText
113                    ->threshold_text->coverage_statement;
114                if (isset($coverageText->embargo_text->embargo_statement)) {
115                    $record['coverage'] .= ' ' . (string)$coverageText
116                        ->embargo_text->embargo_statement;
117                }
118            }
119            array_push($records, $record);
120        }
121        return $records;
122    }
123}