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

UnifiedConfiguration::isTrue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\System\Configuration;
19
20
use ApacheSolrForTypo3\Solr\System\Configuration\Exception\ConfigurationAlreadyMergedException;
21
use ApacheSolrForTypo3\Solr\System\Util\ArrayAccessor;
22
23
use function Webmozart\Assert\Tests\StaticAnalysis\boolean;
0 ignored issues
show
introduced by
The function Webmozart\Assert\Tests\StaticAnalysis\boolean was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
24
25
/**
26
 * This class wraps all configuration information and offers one interface
27
 *
28
 * @author Lars Tode <[email protected]>
29
 */
30
class UnifiedConfiguration extends ArrayAccessor
31
{
32
    /**
33
     * The root page ID
34
     *
35
     * @var int
36
     */
37
    protected $rootPageUid = 0;
38
39
    /**
40
     * The language uid
41
     *
42
     * @var int
43
     */
44
    protected $languageUid = 0;
45
46
    /**
47
     * Lists of sorted configurations
48
     *
49
     * @var string[]
50
     */
51
    protected $includedConfigurationClasses = [];
52
53
    /**
54
     * @var UnifyConfigurationInterface[]
55
     */
56
    protected $includedConfiguration = [];
57
58
    /**
59
     * This constructor contains all required parameters used for other
60
     *
61
     * @param int $pageUid
62
     * @param int $languageUid
63
     */
64
    public function __construct(int $pageUid = 0, int $languageUid = 0)
65
    {
66
        parent::__construct([], '.');
67
        $this->rootPageUid = $pageUid;
68
        $this->languageUid = $languageUid;
69
    }
70
71
    /**
72
     * Check if a class is included within this configuration
73
     *
74
     * @param string $className
75
     * @return bool
76
     */
77
    public function containsConfigurationClass(string $className): bool
78
    {
79
        return in_array($className, $this->includedConfigurationClasses);
80
    }
81
82
    /**
83
     * Merge another configuration into this one
84
     *
85
     * @param UnifyConfigurationInterface $configuration
86
     * @return $this
87
     * @throws ConfigurationAlreadyMergedException
88
     */
89
    public function mergeConfigurationByObject(UnifyConfigurationInterface $configuration): UnifiedConfiguration
90
    {
91
        $className = get_class($configuration);
92
        if (in_array($className, $this->includedConfigurationClasses)) {
93
            throw new ConfigurationAlreadyMergedException(
94
                'Configuration of type ' . $className . ' already merged. '.
95
                'Use method \'replaceConfigurationByObject()\' to replace this configuration.',
96
                409
97
            );
98
        }
99
100
        $data = $configuration->load()->getUnifiedArray();
101
        $this->includedConfiguration[$className] = $configuration;
102
        $this->includedConfigurationClasses[] = $className;
103
        $this->mergeArray($data);
104
105
        return $this;
106
    }
107
108
    /**
109
     * Merge another configuration into this one and execute a reloads
110
     *
111
     * @param UnifyConfigurationInterface $configuration
112
     * @return $this
113
     * @throws ConfigurationAlreadyMergedException
114
     */
115
    public function replaceConfigurationByObject(
116
        UnifyConfigurationInterface $configuration
117
    ): UnifiedConfiguration {
118
        $className = get_class($configuration);
119
        if (!$this->containsConfigurationClass($className)) {
120
            return $this->mergeConfigurationByObject($configuration);
121
        }
122
        $this->includedConfiguration[$className] = $configuration;
123
        $this->reload();
124
        return $this;
125
    }
126
127
    /**
128
     * @return $this
129
     */
130
    public function reload(): UnifiedConfiguration
131
    {
132
        $this->clear();
133
        foreach ($this->includedConfigurationClasses as $className) {
134
            if (!isset($this->includedConfiguration[$className])) {
135
                continue;
136
            }
137
            $data = $this->includedConfiguration[$className]->load()->getUnifiedArray();
138
            $this->mergeArray($data);
139
        }
140
141
        return $this;
142
    }
143
144
    /**
145
     * Returns a specific configuration from included configurations.
146
     *
147
     * @param string $className
148
     * @return UnifyConfigurationInterface|null
149
     */
150
    public function getConfigurationByClass(string $className): ?UnifyConfigurationInterface
151
    {
152
        if (isset($this->includedConfiguration[$className]) &&
153
            $this->includedConfiguration[$className] instanceof UnifyConfigurationInterface) {
154
            return $this->includedConfiguration[$className];
155
        }
156
157
        return null;
158
    }
159
160
    /**
161
     * Returns the current root page id
162
     *
163
     * @return int
164
     */
165
    public function getRootPageUid(): int
166
    {
167
        return $this->rootPageUid;
168
    }
169
170
    /**
171
     * Returns the current language uid
172
     *
173
     * @return int
174
     */
175
    public function getLanguageUid(): int
176
    {
177
        return $this->languageUid;
178
    }
179
180
    /**
181
     * Is Solr enabled?
182
     *
183
     * @return bool
184
     */
185
    public function isEnabled(): bool
186
    {
187
        $enabled = null;
188
        // @TODO: SiteConfiguration by language needs implementation
189
190
        // From SiteConfiguration
191
        if ($enabled === null) {
0 ignored issues
show
introduced by
The condition $enabled === null is always true.
Loading history...
192
            $enabled = $this->get('connection.read.enabled', null);
193
        }
194
        // From TypoScript
195
        if ($enabled === null) {
196
            $enabled = (bool)$this->get('enabled', true);
197
        }
198
199
        return $enabled;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $enabled could return the type array which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
200
    }
201
202
    /**
203
     * Check if the given path contains a true value
204
     *
205
     * @param string $path
206
     * @param bool $fallback
207
     * @return bool
208
     */
209
    public function isTrue(string $path, bool $fallback = false): bool
210
    {
211
        $value = $this->get($path);
212
        if ($value === null) {
213
            return $fallback;
214
        }
215
216
        return (bool)$value;
217
    }
218
219
    /**
220
     * Returns an integer by given configuration path.
221
     *
222
     * @param string $path
223
     * @param int $fallback
224
     * @return int
225
     */
226
    public function getInteger(string $path, int $fallback = 0): int
227
    {
228
        $value = $this->get($path);
229
        if ($value === null) {
230
            return $fallback;
231
        }
232
233
        return (int)$value;
234
    }
235
}
236