Passed
Pull Request — release-11.0.x (#2994)
by Rafael
31:30 queued 28:16
created

SiteUtility::evaluateConfigurationData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
ccs 4
cts 6
cp 0.6667
rs 10
cc 3
nc 3
nop 1
crap 3.3332
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\System\Util;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2019 Timo Hund <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
29
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
30
use TYPO3\CMS\Core\Site\Entity\Site;
31
use TYPO3\CMS\Core\Site\SiteFinder;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
34
/**
35
 * This class contains related functions for the new site management that was introduced with TYPO3 9.
36
 */
37
class SiteUtility
38
{
39
40
    /** @var array */
41
    public static $languages = [];
42
43
    /**
44
     * Determines if the site where the page belongs to is managed with the TYPO3 site management.
45
     *
46
     * @param int $pageId
47
     * @return boolean
48
     */
49 36
    public static function getIsSiteManagedSite(int $pageId): bool
50
    {
51
52 36
        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
53
        try {
54
            /* @var SiteFinder $siteFinder */
55 36
            $site = $siteFinder->getSiteByPageId($pageId);
56
        } catch (SiteNotFoundException $e) {
57
            return false;
58
        }
59
60 36
        return $site instanceof Site;
61
    }
62
63
    /**
64
     * This method is used to retrieve the connection configuration from the TYPO3 site configuration.
65
     *
66
     * Note: Language context properties have precedence over global settings.
67
     *
68
     * The configuration is done in the globals configuration of a site, and be extended in the language specific configuration
69
     * of a site.
70
     *
71
     * Typically everything except the core name is configured on the global level and the core name differs for each language.
72
     *
73
     * In addition every property can be defined for the ```read``` and ```write``` scope.
74
     *
75
     * The convention for property keys is "solr_{propertyName}_{scope}". With the configuration "solr_host_read" you define the host
76
     * for the solr read connection.
77
     *
78
     * @param Site $typo3Site
79
     * @param string $property
80
     * @param int $languageId
81
     * @param string $scope
82
     * @param string $defaultValue
83
     * @return string
84
     */
85 167
    public static function getConnectionProperty(Site $typo3Site, string $property, int $languageId, string $scope, string $defaultValue = null): string
86
    {
87 167
        $value = self::getConnectionPropertyOrFallback($typo3Site, $property, $languageId, $scope);
88 167
        if ($value === null) {
89 167
            return $defaultValue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $defaultValue could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
90
        }
91 167
        return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value could return the type boolean which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
92
    }
93
94
    /**
95
     * Resolves site configuration properties.
96
     * Language context properties have precedence over global settings.
97
     *
98
     * @param Site $typo3Site
99
     * @param string $property
100
     * @param int $languageId
101
     * @param string $scope
102
     * @return mixed
103
     */
104 167
    protected static function getConnectionPropertyOrFallback(Site $typo3Site, string $property, int $languageId, string $scope)
105
    {
106 167
        if ($scope === 'write' && !self::writeConnectionIsEnabled($typo3Site, $languageId)) {
107 167
            $scope = 'read';
108
        }
109
110
        // convention key solr_$property_$scope
111 167
        $keyToCheck = 'solr_' . $property . '_' . $scope;
112
113
        // convention fallback key solr_$property_read
114 167
        $fallbackKey = 'solr_' . $property . '_read';
115
116
        // try to find language specific setting if found return it
117 167
        if (isset(self::$languages[$languageId]) === false) {
118 167
            self::$languages[$languageId] = $typo3Site->getLanguageById($languageId)->toArray();
119
        }
120 167
        $value = self::getValueOrFallback(self::$languages[$languageId], $keyToCheck, $fallbackKey);
121 167
        if ($value !== null) {
122 167
            return $value;
123
        }
124
125
        // if not found check global configuration
126 167
        $siteBaseConfiguration = $typo3Site->getConfiguration();
127 167
        return self::getValueOrFallback($siteBaseConfiguration, $keyToCheck, $fallbackKey);
128
    }
129
130
    /**
131
     * Checks whether write connection is enabled.
132
     * Language context properties have precedence over global settings.
133
     *
134
     * @param Site $typo3Site
135
     * @param int $languageId
136
     * @return bool
137
     */
138 167
    protected static function writeConnectionIsEnabled(Site $typo3Site, int $languageId): bool
139
    {
140 167
        if (isset(self::$languages[$languageId]) === false) {
141
            self::$languages[$languageId] = $typo3Site->getLanguageById($languageId)->toArray();
142
        }
143 167
        $value = self::getValueOrFallback(self::$languages[$languageId], 'solr_use_write_connection', 'solr_use_write_connection');
144 167
        if ($value !== null) {
145
            return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value could return the type string which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
146
        }
147
148 167
        $siteBaseConfiguration = $typo3Site->getConfiguration();
149 167
        $value = self::getValueOrFallback($siteBaseConfiguration, 'solr_use_write_connection', 'solr_use_write_connection');
150 167
        if ($value !== null) {
151 167
            return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value could return the type string which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
152
        }
153
        return false;
154
    }
155
156
    /**
157
     * @param array $data
158
     * @param string $keyToCheck
159
     * @param string $fallbackKey
160
     * @return string|bool|null
161
     */
162 167
    protected static function getValueOrFallback(array $data, string $keyToCheck, string $fallbackKey)
163
    {
164 167
        $value = $data[$keyToCheck] ?? null;
165 167
        if ($value === '0' || $value === 0 || !empty($value)) {
166 167
            return self::evaluateConfigurationData($value);
167
        }
168
169 167
        return self::evaluateConfigurationData($data[$fallbackKey] ?? null);
170
    }
171
172
    /**
173
     * Evaluate configuration data
174
     *
175
     * Setting boolean values via environment variables
176
     * results in strings like 'false' that may be misinterpreted
177
     * thus we check for boolean values in strings.
178
     *
179
     * @param string|bool|null $value
180
     * @return string|bool|null
181
     */
182 167
    protected static function evaluateConfigurationData($value)
183
    {
184 167
        if ($value === 'true') {
185
            return true;
186 167
        } elseif ($value === 'false') {
187
            return false;
188
        }
189
190 167
        return $value;
191
    }
192
}
193