Passed
Pull Request — master (#2809)
by
unknown
34:29
created

ConfigurationManager   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Test Coverage

Coverage 88.46%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
eloc 49
c 1
b 0
f 0
dl 0
loc 183
ccs 23
cts 26
cp 0.8846
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A injectEventDispatcher() 0 3 1
A reset() 0 3 1
A getTypoScriptConfigurationInstance() 0 8 1
B getTypoScriptConfiguration() 0 39 10
A getTypoScriptConfigurationByPageAndLanguage() 0 8 1
A getExtensionConfiguration() 0 3 1
A getSiteConfiguration() 0 8 2
A getGlobalConfiguration() 0 3 1
A getUnifiedConfiguration() 0 19 4
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 ApacheSolrForTypo3\Solr\Event\UnifiedConfigurationEvent;
18
use Psr\EventDispatcher\EventDispatcherInterface;
19
use TYPO3\CMS\Core\SingletonInterface;
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22
/**
23
 * Configuration manager old the configuration instance.
24
 * Singleton
25
 *
26
 * @author Timo Schmidt <[email protected]>
27
 * @copyright (c) 2010-2016 Timo Schmidt <[email protected]
28
 */
29
class ConfigurationManager implements SingletonInterface
30
{
31
    /**
32
     * @var EventDispatcherInterface
33
     */
34
    protected $eventDispatcher;
35
36
    /**
37
     * TypoScript Configurations
38
     *
39
     * @var TypoScriptConfiguration|UnifiedConfiguration[]
40
     */
41
    protected $typoScriptConfigurations = [];
42
43
    /**
44
     * @param EventDispatcherInterface $eventDispatcher
45
     */
46
    public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher)
47
    {
48
        $this->eventDispatcher = $eventDispatcher;
49
    }
50
51
    /**
52
     * Resets the state of the configuration manager.
53
     *
54
     * @return void
55
     */
56
    public function reset()
57
    {
58
        $this->typoScriptConfigurations = [];
59
    }
60
61
    /**
62
     * Build a unified configuration in order to have all configuration settings in one place
63
     *
64
     * @param int $pageUid
65 231
     * @param int $languageUid
66
     * @return UnifiedConfiguration
67 231
     */
68 162
    public function getUnifiedConfiguration(int $pageUid = 0, int $languageUid = 0): UnifiedConfiguration
69 52
    {
70
        if ($pageUid === 0 && !empty($GLOBALS['TSFE']->id)) {
71 159
            $pageUid = (int)$GLOBALS['TSFE']->id;
72 53
        }
73 53
74
        $hash = md5(UnifiedConfiguration::class . '-' . $pageUid . '-' . $languageUid);
75
        if (isset($this->typoScriptConfigurations[$hash])) {
76
            return $this->typoScriptConfigurations[$hash];
77
        }
78 231
79 122
        $unifiedConfiguration = new UnifiedConfiguration($pageUid, $languageUid);
80
        // Requires TYPO3 10 LTS
81
        $event = new UnifiedConfigurationEvent($unifiedConfiguration);
82 231
        $this->eventDispatcher->dispatch($event);
83 147
84
        $this->typoScriptConfigurations[$hash] = $unifiedConfiguration;
85
86 231
        return $unifiedConfiguration;
87 50
    }
88
89
    /**
90 231
     * Returns instance of the global configuration
91 231
     *
92 143
     * @return GlobalConfiguration
93
     */
94
    public function getGlobalConfiguration(): GlobalConfiguration
95 153
    {
96 153
        return new GlobalConfiguration();
97
    }
98
99
    /**
100
     * Returns instance of the global configuration
101
     *
102
     * @return ExtensionConfiguration
103
     */
104
    public function getExtensionConfiguration(): ExtensionConfiguration
105
    {
106 153
        return new ExtensionConfiguration();
107
    }
108 153
109 153
    /**
110 153
     * Returns the site configuration by given page uid and language
111 153
     *
112
     * @param int $pageUid
113
     * @param int $languageUid
114
     * @return SiteConfiguration
115
     */
116
    public function getSiteConfiguration(int $pageUid = 0, int $languageUid = 0): SiteConfiguration
117
    {
118
        $hash = md5(SiteConfiguration::class . '-' . $pageUid . '-' . $languageUid);
119
        if (!isset($this->typoScriptConfigurations[$hash])) {
120
            $this->typoScriptConfigurations[$hash] = new SiteConfiguration($pageUid, $languageUid);
121
        }
122
123
        return $this->typoScriptConfigurations[$hash];
124
    }
125
126
    /**
127
     * Returns the TypoScript configuration by given page and language uid.
128
     *
129
     * @see ConfigurationManager:getTypoScriptConfiguration
130
     *
131
     * @param int $pageUid
132
     * @param int $languageUid
133
     * @return TypoScriptConfiguration
134
     */
135
    public function getTypoScriptConfigurationByPageAndLanguage(
136
        int $pageUid,
137
        int $languageUid = 0
138
    ): TypoScriptConfiguration {
139
        return $this->getTypoScriptConfiguration(
140
            null,
141
            $pageUid,
142
            $languageUid
143
        );
144
    }
145
146
    /**
147
     * Retrieves the TypoScriptConfiguration object from an configuration array, pageId, languageId and TypoScript
148
     * path that is used in in the current context.
149
     *
150
     * @param array $configurationArray
151
     * @param int $contextPageId
152
     * @param int $contextLanguageId
153
     * @param string $contextTypoScriptPath
154
     * @return TypoScriptConfiguration
155
     */
156
    public function getTypoScriptConfiguration(
157
        array $configurationArray = null,
158
        $contextPageId = null,
159
        $contextLanguageId = 0,
160
        $contextTypoScriptPath = ''
161
    ) {
162
        if ($configurationArray == null) {
163
            if (isset($this->typoScriptConfigurations['default'])) {
164
                $configurationArray = $this->typoScriptConfigurations['default'];
165
            } else {
166
                if (!empty($GLOBALS['TSFE']->tmpl->setup) && is_array($GLOBALS['TSFE']->tmpl->setup)) {
167
                    $configurationArray = $GLOBALS['TSFE']->tmpl->setup;
168
                    $this->typoScriptConfigurations['default'] = $configurationArray;
169
                }
170
            }
171
        }
172
173
        if (!is_array($configurationArray)) {
174
            $configurationArray = [];
175
        }
176
177
        if (!isset($configurationArray['plugin.']['tx_solr.'])) {
178
            $configurationArray['plugin.']['tx_solr.'] = [];
179
        }
180
181
        if ($contextPageId === null && !empty($GLOBALS['TSFE']->id)) {
182
            $contextPageId = $GLOBALS['TSFE']->id;
183
        }
184
185
        $hash = md5(serialize($configurationArray)) . '-' . $contextPageId . '-' . $contextLanguageId . '-' . $contextTypoScriptPath;
186
        if (isset($this->typoScriptConfigurations[$hash])) {
187
            return $this->typoScriptConfigurations[$hash];
188
        }
189
190
        $this->typoScriptConfigurations[$hash] = $this->getTypoScriptConfigurationInstance(
191
            $configurationArray,
192
            (int)$contextPageId
193
        );
194
        return $this->typoScriptConfigurations[$hash];
195
    }
196
197
    /**
198
     * This method is used to build the TypoScriptConfiguration.
199
     *
200
     * @param array $configurationArray
201
     * @param int $contextPageId
202
     * @return TypoScriptConfiguration
203
     */
204
    protected function getTypoScriptConfigurationInstance(
205
        array $configurationArray = null,
206
        int $contextPageId = 0
207
    ): TypoScriptConfiguration {
208
        return GeneralUtility::makeInstance(
209
            TypoScriptConfiguration::class,
210
            /** @scrutinizer ignore-type */ $configurationArray,
211
            /** @scrutinizer ignore-type */ (int)$contextPageId
212
        );
213
    }
214
}
215