Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractBaseFactory
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 1
 applyPermissions
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
42
 __invoke
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Generic controller 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  Controller
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\Controller;
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;
37
38/**
39 * Generic controller factory.
40 *
41 * @category VuFind
42 * @package  Controller
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 Wiki
46 */
47class AbstractBaseFactory implements FactoryInterface
48{
49    /**
50     * Apply permission settings to the controller.
51     *
52     * @param ContainerInterface $container  Service manager
53     * @param AbstractBase       $controller Controller to configure
54     *
55     * @return AbstractBase
56     */
57    protected function applyPermissions($container, $controller)
58    {
59        $config = $container->get(\VuFind\Config\PluginManager::class)
60            ->get('permissionBehavior');
61        $permissions = $config->global->controllerAccess ?? [];
62
63        if (!empty($permissions) && $controller instanceof Feature\AccessPermissionInterface) {
64            // Iterate through parent classes until we find the most specific
65            // class access permission defined (if any):
66            $class = $controller::class;
67            do {
68                if (isset($permissions[$class])) {
69                    $controller->setAccessPermission($permissions[$class]);
70                    break;
71                }
72                $class = get_parent_class($class);
73            } while ($class);
74
75            // If the controller's current permission is null (as opposed to false
76            // or a string), that means it has no internally configured default, and
77            // setAccessPermission was not called above; thus, we should apply the
78            // default value:
79            if (
80                isset($permissions['*'])
81                && $controller->getAccessPermission() === null
82            ) {
83                $controller->setAccessPermission($permissions['*']);
84            }
85        }
86
87        return $controller;
88    }
89
90    /**
91     * Create an object
92     *
93     * @param ContainerInterface $container     Service manager
94     * @param string             $requestedName Service being created
95     * @param null|array         $options       Extra options (optional)
96     *
97     * @return object
98     *
99     * @throws ServiceNotFoundException if unable to resolve the service.
100     * @throws ServiceNotCreatedException if an exception is raised when
101     * creating a service.
102     * @throws ContainerException&\Throwable if any other error occurs
103     */
104    public function __invoke(
105        ContainerInterface $container,
106        $requestedName,
107        array $options = null
108    ) {
109        return $this->applyPermissions(
110            $container,
111            new $requestedName($container, ...($options ?: []))
112        );
113    }
114}