Passed
Pull Request — master (#2972)
by Rafael
33:49
created

getAvailablePluginNamespaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Configuration;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException;
18
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException;
19
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration as CoreExtensionConfiguration;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22
/**
23
 * This class encapsulates the access to the extension configuration.
24
 *
25
 * @author Timo Hund <[email protected]>
26
 */
27
class ExtensionConfiguration
28
{
29
    /**
30
     * Extension Configuration
31
     *
32
     * @var array
33
     */
34
    protected $configuration = [];
35
36
    /**
37
     * ExtensionConfiguration constructor.
38
     *
39
     * @param array $configurationToUse
40
     * @throws ExtensionConfigurationExtensionNotConfiguredException
41
     * @throws ExtensionConfigurationPathDoesNotExistException
42
     */
43 245
    public function __construct(array $configurationToUse = [])
44
    {
45 245
        if (empty($configurationToUse)) {
46 245
            $this->configuration = GeneralUtility::makeInstance(CoreExtensionConfiguration::class)->get('solr');
47
        } else {
48 6
            $this->configuration = $configurationToUse;
49
        }
50 245
    }
51
52
    /**
53
     * Get configuration for useConfigurationFromClosestTemplate
54
     *
55
     * @return bool
56
     */
57 140
    public function getIsUseConfigurationFromClosestTemplateEnabled()
58
    {
59 140
        return (bool)$this->getConfigurationOrDefaultValue('useConfigurationFromClosestTemplate', false);
60
    }
61
62
    /**
63
     * Get configuration for useConfigurationTrackRecordsOutsideSiteroot
64
     *
65
     * @return bool
66
     */
67 65
    public function getIsUseConfigurationTrackRecordsOutsideSiteroot()
68
    {
69 65
        return (bool)$this->getConfigurationOrDefaultValue('useConfigurationTrackRecordsOutsideSiteroot', true);
70
    }
71
72
    /**
73
     * Get configuration for allowSelfSignedCertificates
74
     *
75
     * @return bool
76
     */
77 1
    public function getIsSelfSignedCertificatesEnabled()
78
    {
79 1
        return (bool)$this->getConfigurationOrDefaultValue('allowSelfSignedCertificates', false);
80
    }
81
82
    /**
83
     * Get configuration for useConfigurationMonitorTables
84
     *
85
     * @return array of table names
86
     */
87 44
    public function getIsUseConfigurationMonitorTables(): array
88
    {
89 44
        $monitorTables = [];
90 44
        $monitorTablesList = $this->getConfigurationOrDefaultValue('useConfigurationMonitorTables', '');
91
92 44
        if (empty($monitorTablesList)) {
93 42
            return $monitorTables;
94
        }
95
96 4
        return GeneralUtility::trimExplode(',', $monitorTablesList);
97
    }
98
99
    /**
100
     * Returns a list of available/whitelisted EXT:solr plugin namespaces.
101
     * Builds from "pluginNamespaces" extension configuration setting.
102
     *
103
     * @return array
104
     */
105 224
    public function getAvailablePluginNamespaces(): array
106
    {
107 224
        $pluginNamespacesList = 'tx_solr,' . $this->getConfigurationOrDefaultValue(
108 224
                'pluginNamespaces'
109
            );
110 224
        return array_unique(GeneralUtility::trimExplode(',', $pluginNamespacesList));
111
    }
112
113
    /**
114
     * Returns a list of cacheHash-excludedParameters matching the EXT:solr plugin namespaces.
115
     *
116
     * Builds from "pluginNamespaces" and takes "includeGlobalQParameterInCacheHash"
117
     * extension configuration settings into account.
118
     *
119
     * @return array
120
     */
121 224
    public function getCacheHashExcludedParameters(): array
122
    {
123 224
        $pluginNamespaces = array_map(
124
            function ($pluginNamespace) {
125 224
                return '^' . $pluginNamespace . '[';
126 224
            },
127 224
            $this->getAvailablePluginNamespaces()
128
        );
129 224
        if (false === $this->getIncludeGlobalQParameterInCacheHash()) {
130 224
            $pluginNamespaces[] = 'q';
131
        }
132 224
        return array_combine($pluginNamespaces, $pluginNamespaces);
133
    }
134
135
    /**
136
     * Returns the "includeGlobalQParameterInCacheHash" extension configuration setting.
137
     *
138
     * @return bool
139
     */
140 224
    public function getIncludeGlobalQParameterInCacheHash(): bool
141
    {
142 224
        return (bool)$this->getConfigurationOrDefaultValue('includeGlobalQParameterInCacheHash', false);
143
    }
144
145
    /**
146
     * @param string $key
147
     * @param mixed $defaultValue
148
     * @return mixed|null
149
     */
150 235
    protected function getConfigurationOrDefaultValue(string $key, $defaultValue = null)
151
    {
152 235
        return $this->configuration[$key] ?? $defaultValue;
153
    }
154
155
156
}
157