Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
38.89% covered (danger)
38.89%
7 / 18
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Connection
38.89% covered (danger)
38.89%
7 / 18
20.00% covered (danger)
20.00%
1 / 5
38.62
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 disableCache
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 enableCache
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 fetchLinks
37.50% covered (danger)
37.50%
3 / 8
0.00% covered (danger)
0.00%
0 / 1
5.20
 __call
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
1<?php
2
3/**
4 * Link Resolver Driver Wrapper
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;
34
35use function call_user_func_array;
36use function is_callable;
37
38/**
39 * Resolver Connection Class
40 *
41 * This abstract class defines the signature for the available methods for
42 * interacting with the local OpenURL Resolver. It is a cutdown version
43 * of the CatalogConnection class.
44 *
45 * Required functions in implementing Drivers are listed in Interface.php
46 *
47 * @category VuFind
48 * @package  Resolver_Drivers
49 * @author   Graham Seaman <Graham.Seaman@rhul.ac.uk>
50 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
51 * @link     https://vufind.org/wiki/development:plugins:link_resolver_drivers Wiki
52 */
53class Connection
54{
55    /**
56     * The object of the appropriate driver.
57     *
58     * @var object
59     */
60    protected $driver = false;
61
62    /**
63     * The path to the resolver cache, if any (empty string for no caching)
64     *
65     * @var string
66     */
67    protected $cachePath = '';
68
69    /**
70     * Constructor
71     *
72     * This is responsible for instantiating the driver that has been specified.
73     *
74     * @param \VuFind\Resolver\Driver\DriverInterface $driver The driver to use
75     */
76    public function __construct(\VuFind\Resolver\Driver\DriverInterface $driver)
77    {
78        $this->driver = $driver;
79    }
80
81    /**
82     * Disable caching.
83     *
84     * @return void
85     */
86    public function disableCache()
87    {
88        $this->cachePath = '';
89    }
90
91    /**
92     * Enable caching.
93     *
94     * @param string $cacheDir Directory to use for cache.
95     *
96     * @return void
97     */
98    public function enableCache($cacheDir)
99    {
100        if (is_dir($cacheDir) && is_writable($cacheDir)) {
101            $this->cachePath = $cacheDir;
102            if (!str_ends_with($this->cachePath, '/')) {
103                $this->cachePath .= '/';
104            }
105        }
106    }
107
108    /**
109     * Fetch Links
110     *
111     * This is responsible for retrieving the valid links for a
112     * particular OpenURL. The links may be cached or fetched remotely.
113     *
114     * If an error occurs, throw exception
115     *
116     * @param string $openURL The OpenURL to use
117     *
118     * @return array          An associative array with the following keys:
119     * linktype, aval, href, coverage
120     */
121    public function fetchLinks($openURL)
122    {
123        if (!empty($this->cachePath)) {
124            $hashedURL = md5($openURL);
125            if (file_exists($this->cachePath . $hashedURL)) {
126                $links = file_get_contents($this->cachePath . $hashedURL);
127            } else {
128                $links = $this->driver->fetchLinks($openURL);
129                file_put_contents($this->cachePath . $hashedURL, $links);
130            }
131        } else {
132            $links = $this->driver->fetchLinks($openURL);
133        }
134        return $this->driver->parseLinks($links);
135    }
136
137    /**
138     * Default method -- pass along calls to the driver if available; return
139     * false otherwise. This allows custom functions to be implemented in
140     * the driver without constant modification to the connection class.
141     *
142     * @param string $methodName The name of the called method.
143     * @param array  $params     Array of passed parameters.
144     *
145     * @return mixed             Varies by method (false if undefined method)
146     */
147    public function __call($methodName, $params)
148    {
149        $method = [$this->driver, $methodName];
150        if (is_callable($method)) {
151            return call_user_func_array($method, $params);
152        }
153        return false;
154    }
155}