Completed
Branch master (a6ebf8)
by Timo
03:27
created

SiteUtility::getIsSiteManagedSite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 10
cc 2
nc 2
nop 1
crap 2.0185
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
use ApacheSolrForTypo3\Solr\Util;
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
     * @return boolean
46
     */
47 159
    public static function getIsSiteManagedSite($pageId)
48
    {
49
50 159
        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
51
        try {
52 159
            $site = $siteFinder->getSiteByPageId($pageId);
53 159
        } catch (SiteNotFoundException $e) {
54 159
            return false;
55
        }
56
57
        return $site instanceof Site;
58
    }
59
60
    /**
61
     * This method is used to retrieve the connection configuration from the TYPO3 site configuration.
62
     *
63
     * The configuration is done in the globals configuration of a site, and be extended in the language specific configuration
64
     * of a site.
65
     *
66
     * Typically everything except the core name is configured on the global level and the core name differs for each language.
67
     *
68
     * In addition every property can be defined for the ```read``` and ```write``` scope.
69
     *
70
     * The convension for propery keys is "solr_{propertyName}_{scope}". With the configuration "solr_host_read" you define the host
71
     * for the solr read connection.
72
     *
73
     * @param Site $typo3Site
74
     * @param $property
75
     * @param $languageId
76
     * @param $scope
77
     * @param null $defaultValue
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $defaultValue is correct as it would always require null to be passed?
Loading history...
78
     * @return string
79
     */
80 3
    public static function getConnectionProperty(Site $typo3Site, $property, $languageId, $scope, $defaultValue = null): string
81
    {
82
83
        // convention kez solr_$property_$scope
84 3
        $keyToCheck = 'solr_' . $property . '_' . $scope;
85
86
        // convention fallback key solr_$property_read
87 3
        $fallbackKey = 'solr_' . $property . '_read';
88
89
        // try to find language specific setting if found return it
90 3
        $languageSpecificConfiguration = $typo3Site->getLanguageById($languageId)->toArray();
91 3
        $value = self::getValueOrFallback($languageSpecificConfiguration, $keyToCheck, $fallbackKey);
92
93 3
        if (!empty($value)) {
94 2
            return $value;
95
        }
96
97
        // if not found check global configuration
98 1
        $siteBaseConfiguration = $typo3Site->getConfiguration();
99
100 1
        return self::getValueOrFallback($siteBaseConfiguration, $keyToCheck, $fallbackKey) ?: $defaultValue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::getValueOrF...ckKey) ?: $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...
101
    }
102
103
    /**
104
     * @param array $data
105
     * @param string $keyToCheck
106
     * @param string $fallbackKey
107
     * @return string|null
108
     */
109 3
    protected static function getValueOrFallback(array $data, string $keyToCheck, string $fallbackKey)
110
    {
111 3
        $value = $data[$keyToCheck] ?? null;
112 3
        if (!empty($value)) {
113 2
            return $value;
114
        }
115
116 2
        return $data[$fallbackKey] ?? null;
117
    }
118
}