Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractMultiDriver
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
6 / 6
16
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 init
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getDriver
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 createDriver
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 getDriverConfig
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 driverSupportsMethod
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * Abstract Multi Driver.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) The National Library of Finland 2012-2021.
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  ILSdrivers
25 * @author   Ere Maijala <ere.maijala@helsinki.fi>
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @author   Thomas Wagener <wagener@hebis.uni-frankfurt.de>
28 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
29 * @link     https://vufind.org/wiki/development:plugins:ils_drivers Wiki
30 */
31
32namespace VuFind\ILS\Driver;
33
34use VuFind\Exception\ILS as ILSException;
35
36use function array_key_exists;
37use function is_callable;
38
39/**
40 * Abstract Multi Driver.
41 *
42 * This abstract driver defines some common methods for ILS drivers that use
43 * multiple other ILS drivers.
44 *
45 * @category VuFind
46 * @package  ILSdrivers
47 * @author   Ere Maijala <ere.maijala@helsinki.fi>
48 * @author   Demian Katz <demian.katz@villanova.edu>
49 * @author   Thomas Wagener <wagener@hebis.uni-frankfurt.de>
50 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
51 * @link     https://vufind.org/wiki/development:plugins:ils_drivers Wiki
52 */
53
54abstract class AbstractMultiDriver extends AbstractBase implements \Laminas\Log\LoggerAwareInterface
55{
56    use \VuFind\Log\LoggerAwareTrait {
57        logError as error;
58    }
59
60    /**
61     * The array of configured driver names.
62     *
63     * @var string[]
64     */
65    protected $drivers = [];
66
67    /**
68     * The path to the driver configurations relative to the config path
69     *
70     * @var string
71     */
72    protected $driversConfigPath;
73
74    /**
75     * The array of cached drivers
76     *
77     * @var object[]
78     */
79    protected $driverCache = [];
80
81    /**
82     * Configuration loader
83     *
84     * @var \VuFind\Config\PluginManager
85     */
86    protected $configLoader;
87
88    /**
89     * ILS driver manager
90     *
91     * @var PluginManager
92     */
93    protected $driverManager;
94
95    /**
96     * Constructor
97     *
98     * @param \VuFind\Config\PluginManager $configLoader Configuration loader
99     * @param PluginManager                $dm           ILS driver manager
100     */
101    public function __construct(
102        \VuFind\Config\PluginManager $configLoader,
103        PluginManager $dm
104    ) {
105        $this->configLoader = $configLoader;
106        $this->driverManager = $dm;
107    }
108
109    /**
110     * Initialize the driver.
111     *
112     * Validate configuration and perform all resource-intensive tasks needed to
113     * make the driver active.
114     *
115     * @throws ILSException
116     * @return void
117     */
118    public function init()
119    {
120        if (empty($this->config)) {
121            throw new ILSException('Configuration needs to be set.');
122        }
123        $this->drivers = $this->config['Drivers'];
124        $this->driversConfigPath
125            = $this->config['General']['drivers_config_path'] ?? null;
126    }
127
128    /**
129     * Find the correct driver for the correct configuration file with the given name
130     * and cache an initialized copy of it.
131     *
132     * @param string $name The name of the driver to get.
133     *
134     * @return mixed  On success a driver object, otherwise null.
135     */
136    protected function getDriver($name)
137    {
138        // Check for a cached driver
139        if (!array_key_exists($name, $this->driverCache)) {
140            // Create the driver
141            $this->driverCache[$name] = $this->createDriver($name);
142            if (null === $this->driverCache[$name]) {
143                $this->debug("Could not initialize driver '$name'");
144                return null;
145            }
146        }
147        return $this->driverCache[$name];
148    }
149
150    /**
151     * Create a driver with the given name.
152     *
153     * @param string $name Name of the driver.
154     *
155     * @return mixed On success a driver object, otherwise null.
156     */
157    protected function createDriver($name)
158    {
159        if (!isset($this->drivers[$name])) {
160            return null;
161        }
162        $driver = $this->drivers[$name];
163        $config = $this->getDriverConfig($name);
164        if (!$config) {
165            $this->error("No configuration found for driver '$name'");
166            return null;
167        }
168        $driverInst = clone $this->driverManager->get($driver);
169        $driverInst->setConfig($config);
170        $driverInst->init();
171        return $driverInst;
172    }
173
174    /**
175     * Get configuration for the ILS driver.  We will load an .ini file named
176     * after the driver class and number if it exists;
177     * otherwise we will return an empty array.
178     *
179     * @param string $name The $name to use for determining the
180     * configuration file
181     *
182     * @return array   The configuration of the driver
183     */
184    protected function getDriverConfig($name)
185    {
186        // Determine config file name based on class name:
187        try {
188            $path = empty($this->driversConfigPath)
189                ? $name
190                : $this->driversConfigPath . '/' . $name;
191            $config = $this->configLoader->get($path);
192        } catch (\Laminas\Config\Exception\RuntimeException $e) {
193            // Configuration loading failed; probably means file does not
194            // exist -- just return an empty array in that case:
195            $this->error("Could not load config for $name");
196            return [];
197        }
198        return $config->toArray();
199    }
200
201    /**
202     * Check whether the given driver supports the given method
203     *
204     * @param object $driver ILS Driver
205     * @param string $method Method name
206     * @param array  $params Array of passed parameters
207     *
208     * @return bool
209     */
210    protected function driverSupportsMethod($driver, $method, $params = null)
211    {
212        if (is_callable([$driver, $method])) {
213            if (method_exists($driver, 'supportsMethod')) {
214                return $driver->supportsMethod($method, $params ?: []);
215            }
216            return true;
217        }
218        return false;
219    }
220}