Passed
Push — master ( 4e6543...4e73df )
by Rafael
09:16
created

SiteUtility::writeConnectionIsEnabled()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 2
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
 * @package ApacheSolrForTypo3\Solr\System\Util
38
 */
39
class SiteUtility
40
{
41
42
    /**
43
     * Determines if the site where the page belongs to is managed with the TYPO3 site management.
44
     *
45
     * @param int $pageId
46
     * @return boolean
47
     */
48
    public static function getIsSiteManagedSite(int $pageId): bool
49
    {
50
51
        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
52
        try {
53
            /* @var SiteFinder $siteFinder */
54
            $site = $siteFinder->getSiteByPageId($pageId);
55
        } catch (SiteNotFoundException $e) {
56
            return false;
57
        }
58
59
        return $site instanceof Site;
60
    }
61
62
    /**
63
     * This method is used to retrieve the connection configuration from the TYPO3 site configuration.
64
     *
65
     * Note: Language context properties have precedence over global settings.
66
     *
67
     * The configuration is done in the globals configuration of a site, and be extended in the language specific configuration
68
     * of a site.
69
     *
70
     * Typically everything except the core name is configured on the global level and the core name differs for each language.
71
     *
72
     * In addition every property can be defined for the ```read``` and ```write``` scope.
73
     *
74
     * The convention for property keys is "solr_{propertyName}_{scope}". With the configuration "solr_host_read" you define the host
75
     * for the solr read connection.
76
     *
77
     * @param Site $typo3Site
78
     * @param string $property
79
     * @param int $languageId
80
     * @param string $scope
81
     * @param string $defaultValue
82
     * @return string
83
     */
84
    public static function getConnectionProperty(Site $typo3Site, string $property, int $languageId, string $scope, string $defaultValue = null): string
85
    {
86
        $value = self::getConnectionPropertyOrFallback($typo3Site, $property, $languageId, $scope);
87
        if ($value === null) {
88
            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...
89
        }
90
        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...
91
    }
92
93
    /**
94
     * Resolves site configuration properties.
95
     * Language context properties have precedence over global settings.
96
     *
97
     * @param Site $typo3Site
98
     * @param string $property
99
     * @param int $languageId
100
     * @param string $scope
101
     * @return mixed
102
     */
103
    protected static function getConnectionPropertyOrFallback(Site $typo3Site, string $property, int $languageId, string $scope)
104
    {
105
        if ($scope === 'write' && !self::writeConnectionIsEnabled($typo3Site, $languageId)) {
106
            $scope = 'read';
107
        }
108
109
        // convention key solr_$property_$scope
110
        $keyToCheck = 'solr_' . $property . '_' . $scope;
111
112
        // convention fallback key solr_$property_read
113
        $fallbackKey = 'solr_' . $property . '_read';
114
115
        // try to find language specific setting if found return it
116
        $languageSpecificConfiguration = $typo3Site->getLanguageById($languageId)->toArray();
117
        $value = self::getValueOrFallback($languageSpecificConfiguration, $keyToCheck, $fallbackKey);
118
        if ($value !== null) {
119
            return $value;
120
        }
121
122
        // if not found check global configuration
123
        $siteBaseConfiguration = $typo3Site->getConfiguration();
124
        return self::getValueOrFallback($siteBaseConfiguration, $keyToCheck, $fallbackKey);
125
    }
126
127
    /**
128
     * Checks whether write connection is enabled.
129
     * Language context properties have precedence over global settings.
130
     *
131
     * @param Site $typo3Site
132
     * @param int $languageId
133
     * @return bool
134
     */
135
    protected static function writeConnectionIsEnabled(Site $typo3Site, int $languageId): bool
136
    {
137
        $languageSpecificConfiguration = $typo3Site->getLanguageById($languageId)->toArray();
138
        $value = self::getValueOrFallback($languageSpecificConfiguration, 'solr_use_write_connection', 'solr_use_write_connection');
139
        if ($value !== null) {
140
            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...
141
        }
142
143
        $siteBaseConfiguration = $typo3Site->getConfiguration();
144
        $value = self::getValueOrFallback($siteBaseConfiguration, 'solr_use_write_connection', 'solr_use_write_connection');
145
        if ($value !== null) {
146
            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...
147
        }
148
        return false;
149
    }
150
151
    /**
152
     * @param array $data
153
     * @param string $keyToCheck
154
     * @param string $fallbackKey
155
     * @return string|bool|null
156
     */
157
    protected static function getValueOrFallback(array $data, string $keyToCheck, string $fallbackKey)
158
    {
159
        $value = $data[$keyToCheck] ?? null;
160
        if (!empty($value)) {
161
            return $value;
162
        }
163
164
        return $data[$fallbackKey] ?? null;
165
    }
166
}
167