Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
IlsAwareDelegatorFactory
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
1 / 1
 __invoke
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 getIlsBackends
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * ILS aware delegator factory
5 *
6 * Copyright (C) Villanova University 2018.
7 *
8 * PHP version 8
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  RecordDrivers
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/wiki/development:plugins:session_handlers Wiki
28 */
29
30namespace VuFind\RecordDriver;
31
32use Laminas\ServiceManager\Factory\DelegatorFactoryInterface;
33use Psr\Container\ContainerInterface;
34
35use function call_user_func;
36use function is_array;
37
38/**
39 * ILS aware delegator factory
40 *
41 * @category VuFind
42 * @package  RecordDrivers
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/wiki/development:plugins:session_handlers Wiki
46 */
47class IlsAwareDelegatorFactory implements DelegatorFactoryInterface
48{
49    /**
50     * Invokes this factory.
51     *
52     * @param ContainerInterface $container Service container
53     * @param string             $name      Service name
54     * @param callable           $callback  Service callback
55     * @param array|null         $options   Service options
56     *
57     * @return AbstractBase
58     *
59     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
60     */
61    public function __invoke(
62        ContainerInterface $container,
63        $name,
64        callable $callback,
65        array $options = null
66    ) {
67        $driver = call_user_func($callback);
68
69        // Attach the ILS if at least one backend supports it:
70        $ilsBackends = $this->getIlsBackends($container);
71        if (!empty($ilsBackends) && $container->has(\VuFind\ILS\Connection::class)) {
72            $driver->attachILS(
73                $container->get(\VuFind\ILS\Connection::class),
74                $container->get(\VuFind\ILS\Logic\Holds::class),
75                $container->get(\VuFind\ILS\Logic\TitleHolds::class)
76            );
77            $driver->setIlsBackends($ilsBackends);
78        }
79
80        return $driver;
81    }
82
83    /**
84     * Get the ILS backend configuration.
85     *
86     * @param ContainerInterface $container Service container
87     *
88     * @return string[]
89     */
90    protected function getIlsBackends(ContainerInterface $container)
91    {
92        // Get a list of ILS-compatible backends.
93        static $ilsBackends = null;
94        if (!is_array($ilsBackends)) {
95            $config = $container->get(\VuFind\Config\PluginManager::class)
96                ->get('config');
97            $settings = isset($config->Catalog) ? $config->Catalog->toArray() : [];
98
99            // If the setting is missing, default to the default backend; if it
100            // is present but empty, don't put an empty string in the final array!
101            $rawSetting = $settings['ilsBackends'] ?? [DEFAULT_SEARCH_BACKEND];
102            $ilsBackends = empty($rawSetting) ? [] : (array)$rawSetting;
103        }
104        return $ilsBackends;
105    }
106}