Passed
Pull Request — main (#3451)
by Rafael
42:03
created

SiteHashService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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\Domain\Site;
19
20
use ApacheSolrForTypo3\Solr\System\Util\SiteUtility;
21
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
22
use Throwable;
23
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
24
use TYPO3\CMS\Core\Site\SiteFinder;
25
26
/**
27
 * SiteHashService
28
 *
29
 * Responsible to provide sitehash related service methods.
30
 *
31
 * @author Timo Hund <[email protected]>
32
 */
33
class SiteHashService
34
{
35
    /**
36
     * SiteFinder
37
     */
38
    protected SiteFinder $siteFinder;
39
40
    public function __construct(SiteFinder $siteFinder)
41
    {
42
        $this->siteFinder = $siteFinder;
43
    }
44
45
    /**
46
     * Resolves magic keywords in allowed sites configuration.
47 43
     * Supported keywords:
48
     *   __solr_current_site - The domain of the site the query has been started from
49
     *   __current_site - Same as __solr_current_site
50
     *   __all - Adds all domains as allowed sites
51 43
     *   * - Means all sites are allowed, same as no siteHash
52 2
     *
53
     * @param int $pageId A page ID that is then resolved to the site it belongs to
54 41
     * @param string|null $allowedSitesConfiguration TypoScript setting for allowed sites
55 2
     * @return string List of allowed sites/domains, magic keywords resolved
56
     * @throws DBALDriverException
57
     * @throws Throwable
58 39
     */
59 39
    public function getAllowedSitesForPageIdAndAllowedSitesConfiguration(
60
        int $pageId,
61
        ?string $allowedSitesConfiguration = ''
62
    ): string {
63
        if ($allowedSitesConfiguration === '__all') {
64
            return $this->getDomainListOfAllSites();
65
        }
66
        if ($allowedSitesConfiguration === '*') {
67
            return '*';
68 207
        }
69
        // we thread empty allowed site configurations as __solr_current_site since this is the default behaviour
70 207
        $allowedSitesConfiguration = empty($allowedSitesConfiguration) ? '__solr_current_site' : $allowedSitesConfiguration;
71 207
        return $this->getDomainByPageIdAndReplaceMarkers($pageId, $allowedSitesConfiguration);
72 114
    }
73
74
    /**
75 207
     * Gets the site hash for a domain
76 207
     *
77
     * @param string $domain Domain to calculate the site hash for.
78
     * @return string site hash for $domain
79
     */
80
    public function getSiteHashForDomain(string $domain): string
81
    {
82
        static $siteHashes = [];
83
        if (isset($siteHashes[$domain])) {
84
            return $siteHashes[$domain];
85
        }
86 2
87
        $siteHashes[$domain] = sha1($domain . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . 'tx_solr');
88 2
        return $siteHashes[$domain];
89 2
    }
90 2
91 2
    /**
92
     * Returns a comma separated list of all domains from all sites.
93
     *
94 2
     * @return string
95
     * @throws DBALDriverException
96
     * @throws Throwable
97
     */
98
    protected function getDomainListOfAllSites(): string
99
    {
100
        $sites = $this->siteFinder->getAllSites();
101
        $domains = [];
102
        foreach ($sites as $typo3Site) {
103
            $connections = SiteUtility::getAllSolrConnectionConfigurations($typo3Site);
104
            if (!empty($connections)) {
105 39
                $domains[] = $typo3Site->getBase()->getHost();
106
            }
107 39
        }
108 39
109 39
        return implode(',', $domains);
110
    }
111
112
    /**
113
     * Retrieves the domain of the site that belongs to the passed pageId and replaces their markers __solr_current_site
114
     * and __current_site.
115
     *
116
     * @param int $pageId
117 1
     * @param string $allowedSitesConfiguration
118
     * @return string
119 1
     */
120
    protected function getDomainByPageIdAndReplaceMarkers(int $pageId, string $allowedSitesConfiguration): string
121
    {
122
        try {
123
            $typo3Site = $this->siteFinder->getSiteByPageId($pageId);
124
            $domainOfPage = $typo3Site->getBase()->getHost();
125
        } catch (SiteNotFoundException $e) {
126 36
            return '';
127
        }
128 36
129
        $allowedSites = str_replace(['__solr_current_site', '__current_site'], $domainOfPage, $allowedSitesConfiguration);
130
        return (string)$allowedSites;
131
    }
132
}
133