Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FacetCacheFactory
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 2
12
0.00% covered (danger)
0.00%
0 / 1
 getResults
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 __invoke
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Abstract FacetCache Factory.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2018.
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_Base
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 Wiki
28 */
29
30namespace VuFind\Search\Base;
31
32use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
33use Laminas\ServiceManager\Exception\ServiceNotFoundException;
34use Laminas\ServiceManager\Factory\FactoryInterface;
35use Psr\Container\ContainerExceptionInterface as ContainerException;
36use Psr\Container\ContainerInterface;
37use VuFind\I18n\Locale\LocaleSettings;
38
39use function count;
40
41/**
42 * Abstract FacetCache Factory.
43 *
44 * @category VuFind
45 * @package  Search_Base
46 * @author   Demian Katz <demian.katz@villanova.edu>
47 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
48 * @link     https://vufind.org/wiki/development Wiki
49 */
50class FacetCacheFactory implements FactoryInterface
51{
52    /**
53     * Create a results object.
54     *
55     * @param ContainerInterface $container Service manager
56     * @param string             $name      Name of results object to load (based
57     * on name of FacetCache service name)
58     *
59     * @return Results
60     */
61    protected function getResults(ContainerInterface $container, $name)
62    {
63        return $container->get(\VuFind\Search\Results\PluginManager::class)
64            ->get($name);
65    }
66
67    /**
68     * Create an object
69     *
70     * @param ContainerInterface $container     Service manager
71     * @param string             $requestedName Service being created
72     * @param null|array         $options       Extra options (optional)
73     *
74     * @return object
75     *
76     * @throws ServiceNotFoundException if unable to resolve the service.
77     * @throws ServiceNotCreatedException if an exception is raised when
78     * creating a service.
79     * @throws ContainerException&\Throwable if any other error occurs
80     */
81    public function __invoke(
82        ContainerInterface $container,
83        $requestedName,
84        array $options = null
85    ) {
86        if (!empty($options)) {
87            throw new \Exception('Unexpected options sent to factory.');
88        }
89        $parts = explode('\\', $requestedName);
90        $requestedNamespace = $parts[count($parts) - 2];
91        $results = $this->getResults($container, $requestedNamespace);
92        $cacheManager = $container->get(\VuFind\Cache\Manager::class);
93        $language = $container->get(LocaleSettings::class)->getUserLocale();
94        return new $requestedName($results, $cacheManager, $language);
95    }
96}