Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractContentFactory
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
110
0.00% covered (danger)
0.00%
0 / 1
 __invoke
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getHideSetting
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3/**
4 * Abstract factory for building AbstractContent tabs.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2019.
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  RecordTabs
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\RecordTab;
31
32use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
33use Laminas\ServiceManager\Exception\ServiceNotFoundException;
34use Psr\Container\ContainerExceptionInterface as ContainerException;
35use Psr\Container\ContainerInterface;
36use VuFind\Config\PluginManager as ConfigManager;
37use VuFind\Content\PluginManager as ContentManager;
38
39use function in_array;
40
41/**
42 * Abstract factory for building AbstractContent tabs.
43 *
44 * @category VuFind
45 * @package  RecordTabs
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 */
50abstract class AbstractContentFactory implements \Laminas\ServiceManager\Factory\FactoryInterface
51{
52    /**
53     * The name of the tab being constructed.
54     *
55     * @var string
56     */
57    protected $tabName;
58
59    /**
60     * Create an object
61     *
62     * @param ContainerInterface $container     Service manager
63     * @param string             $requestedName Service being created
64     * @param null|array         $options       Extra options (optional)
65     *
66     * @return object
67     *
68     * @throws ServiceNotFoundException if unable to resolve the service.
69     * @throws ServiceNotCreatedException if an exception is raised when
70     * creating a service.
71     * @throws ContainerException&\Throwable if any other error occurs
72     *
73     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
74     */
75    public function __invoke(
76        ContainerInterface $container,
77        $requestedName,
78        array $options = null
79    ) {
80        if (!empty($options)) {
81            throw new \Exception('Unexpected options passed to factory.');
82        }
83        $config = $container->get(ConfigManager::class)->get('config');
84        // Only instantiate the loader if the feature is enabled:
85        $loader = isset($config->Content->{$this->tabName})
86            ? $container->get(ContentManager::class)->get($this->tabName)
87            : null;
88        return new $requestedName($loader, $this->getHideSetting($config));
89    }
90
91    /**
92     * Support method for construction of AbstractContent objects -- should we
93     * hide this tab if it is empty?
94     *
95     * @param \Laminas\Config\Config $config VuFind configuration
96     *
97     * @return bool
98     */
99    protected function getHideSetting(\Laminas\Config\Config $config)
100    {
101        $setting = $config->Content->hide_if_empty ?? false;
102        if (
103            $setting === true || $setting === false
104            || $setting === 1 || $setting === 0
105        ) {
106            return (bool)$setting;
107        }
108        if ($setting === 'true' || $setting === '1') {
109            return true;
110        }
111        $hide = array_map('trim', array_map('strtolower', explode(',', $setting)));
112        return in_array(strtolower($this->tabName), $hide);
113    }
114}