Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExplanationFactory
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 __invoke
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3/**
4 * Generic factory for explanation objects.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Hebis Verbundzentrale 2023.
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  Search
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @author   Dennis Schrittenlocher <Dennis.Schrittenlocher@outlook.de>
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 Wiki
30 */
31
32namespace VuFind\Search\Explanation;
33
34use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
35use Laminas\ServiceManager\Exception\ServiceNotFoundException;
36use Laminas\ServiceManager\Factory\FactoryInterface;
37use Psr\Container\ContainerExceptionInterface as ContainerException;
38use Psr\Container\ContainerInterface;
39
40/**
41 * Generic factory for explanation objects.
42 *
43 * @category VuFind
44 * @package  Search
45 * @author   Demian Katz <demian.katz@villanova.edu>
46 * @author   Dennis Schrittenlocher <Dennis.Schrittenlocher@outlook.de>
47 * @author   Thomas Wagener <wagener@hebis.uni-frankfurt.de>
48 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
49 * @link     https://vufind.org/wiki/development Wiki
50 */
51class ExplanationFactory implements FactoryInterface
52{
53    /**
54     * Create an object
55     *
56     * @param ContainerInterface $container     Service manager
57     * @param string             $requestedName Service being created
58     * @param null|array         $options       Extra options (optional)
59     *
60     * @return object
61     *
62     * @throws ServiceNotFoundException if unable to resolve the service.
63     * @throws ServiceNotCreatedException if an exception is raised when
64     * creating a service.
65     * @throws ContainerException&\Throwable if any other error occurs
66     */
67    public function __invoke(
68        ContainerInterface $container,
69        $requestedName,
70        array $options = null
71    ) {
72        // Replace trailing "Explanation" with "Params" to get the params service:
73        $paramsService = preg_replace('/Explanation$/', 'Params', $requestedName);
74        // Replace leading namespace with "VuFind" if service is not available:
75        $paramsServiceAvailable = $container
76            ->get(\VuFind\Search\Params\PluginManager::class)->has($paramsService);
77        if (!$paramsServiceAvailable) {
78            $paramsService = preg_replace('/^[^\\\]+/', 'VuFind', $paramsService);
79        }
80        $params = $container->get(\VuFind\Search\Params\PluginManager::class)
81            ->get($paramsService);
82        return new $requestedName(
83            $params,
84            $container->get(\VuFindSearch\Service::class),
85            $container->get(\VuFind\Config\PluginManager::class),
86            ...($options ?: [])
87        );
88    }
89}