Passed
Push — release-11.5.x ( 1520e1...01d568 )
by Rafael
43:11
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
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27
/**
28
 * SiteHashService
29
 *
30
 * Responsible to provide sitehash related service methods.
31
 *
32
 * @author Timo Hund <[email protected]>
33
 */
34
class SiteHashService
35
{
36
    /**
37
     * SiteFinder
38
     */
39
    protected SiteFinder $siteFinder;
40
41
    public function __construct(SiteFinder $siteFinder)
42
    {
43
        $this->siteFinder = $siteFinder;
44
    }
45
46
    /**
47 43
     * Resolves magic keywords in allowed sites configuration.
48
     * Supported keywords:
49
     *   __solr_current_site - The domain of the site the query has been started from
50
     *   __current_site - Same as __solr_current_site
51 43
     *   __all - Adds all domains as allowed sites
52 2
     *   * - Means all sites are allowed, same as no siteHash
53
     *
54 41
     * @param int $pageId A page ID that is then resolved to the site it belongs to
55 2
     * @param string|null $allowedSitesConfiguration TypoScript setting for allowed sites
56
     * @return string List of allowed sites/domains, magic keywords resolved
57
     * @throws DBALDriverException
58 39
     * @throws Throwable
59 39
     */
60
    public function getAllowedSitesForPageIdAndAllowedSitesConfiguration(
61
        int $pageId,
62
        ?string $allowedSitesConfiguration = ''
63
    ): string {
64
        if ($allowedSitesConfiguration === '__all') {
65
            return $this->getDomainListOfAllSites();
66
        }
67
        if ($allowedSitesConfiguration === '*') {
68 207
            return '*';
69
        }
70 207
        // we thread empty allowed site configurations as __solr_current_site since this is the default behaviour
71 207
        $allowedSitesConfiguration = empty($allowedSitesConfiguration) ? '__solr_current_site' : $allowedSitesConfiguration;
72 114
        return $this->getDomainByPageIdAndReplaceMarkers($pageId, $allowedSitesConfiguration);
73
    }
74
75 207
    /**
76 207
     * Gets the site hash for a domain
77
     *
78
     * @param string $domain Domain to calculate the site hash for.
79
     * @return string site hash for $domain
80
     */
81
    public function getSiteHashForDomain(string $domain): string
82
    {
83
        static $siteHashes = [];
84
        if (isset($siteHashes[$domain])) {
85
            return $siteHashes[$domain];
86 2
        }
87
88 2
        $siteHashes[$domain] = sha1($domain . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . 'tx_solr');
89 2
        return $siteHashes[$domain];
90 2
    }
91 2
92
    /**
93
     * Returns a comma separated list of all domains from all sites.
94 2
     *
95
     * @return string
96
     * @throws DBALDriverException
97
     * @throws Throwable
98
     */
99
    protected function getDomainListOfAllSites(): string
100
    {
101
        $sites = $this->siteFinder->getAllSites();
102
        $domains = [];
103
        foreach ($sites as $typo3Site) {
104
            $connections = SiteUtility::getAllSolrConnectionConfigurations($typo3Site);
105 39
            if (!empty($connections)) {
106
                $domains[] = $typo3Site->getBase()->getHost();
107 39
            }
108 39
        }
109 39
110
        return implode(',', $domains);
111
    }
112
113
    /**
114
     * Retrieves the domain of the site that belongs to the passed pageId and replaces their markers __solr_current_site
115
     * and __current_site.
116
     *
117 1
     * @param int $pageId
118
     * @param string $allowedSitesConfiguration
119 1
     * @return string
120
     */
121
    protected function getDomainByPageIdAndReplaceMarkers(int $pageId, string $allowedSitesConfiguration): string
122
    {
123
        try {
124
            $typo3Site = $this->siteFinder->getSiteByPageId($pageId);
125
            $domainOfPage = $typo3Site->getBase()->getHost();
126 36
        } catch (SiteNotFoundException $e) {
127
            return '';
128 36
        }
129
130
        $allowedSites = str_replace(['__solr_current_site', '__current_site'], $domainOfPage, $allowedSitesConfiguration);
131
        return (string)$allowedSites;
132
    }
133
134
    /**
135
     * @return Site[]
136 37
     * @throws DBALDriverException
137
     * @throws Throwable
138 37
     * @deprecated since v11.5 and will be removed in v11.6, SiteHashService no longer requires/uses Solr sites
139
     */
140
    protected function getAvailableSites(): array
141
    {
142
        trigger_error(
143
            'SiteHashService no longer requires/uses Solr sites ' . __METHOD__ . ' of class ' . __CLASS__ . ' is deprecated since v11.5 and will be removed in v11.6. Use SiteFinder instead or initizalize own objects',
144
            E_USER_DEPRECATED
145
        );
146
147
        return $this->getSiteRepository()->getAvailableSites();
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...ce::getSiteRepository() has been deprecated: since v11.5 and will be removed in v11.6, SiteHashService no longer requires/uses Solr sites ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

147
        return /** @scrutinizer ignore-deprecated */ $this->getSiteRepository()->getAvailableSites();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
148
    }
149
150
    /**
151
     * @param int $pageId
152
     * @return SiteInterface
153
     * @deprecated since v11.5 and will be removed in v11.6, SiteHashService no longer requires/uses Solr sites
154
     */
155
    protected function getSiteByPageId(int $pageId): SiteInterface
156
    {
157
        trigger_error(
158
            'SiteHashService no longer requires/uses Solr sites ' . __METHOD__ . ' of class ' . __CLASS__ . ' is deprecated since v11.5 and will be removed in v11.6. Use SiteFinder instead or initizalize own objects',
159
            E_USER_DEPRECATED
160
        );
161
162
        return $this->getSiteRepository()->getSiteByPageId($pageId);
0 ignored issues
show
Deprecated Code introduced by
The function ApacheSolrForTypo3\Solr\...ce::getSiteRepository() has been deprecated: since v11.5 and will be removed in v11.6, SiteHashService no longer requires/uses Solr sites ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

162
        return /** @scrutinizer ignore-deprecated */ $this->getSiteRepository()->getSiteByPageId($pageId);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
163
    }
164
165
    /**
166
     * Get a reference to SiteRepository
167
     *
168
     * @return SiteRepository
169
     * @deprecated since v11.5 and will be removed in v11.6, SiteHashService no longer requires/uses Solr sites
170
     */
171
    protected function getSiteRepository(): SiteRepository
172
    {
173
        trigger_error(
174
            'SiteHashService no longer requires/uses Solr sites ' . __METHOD__ . ' of class ' . __CLASS__ . ' is deprecated since v11.5 and will be removed in v11.6. Use SiteFinder instead or initizalize own objects',
175
            E_USER_DEPRECATED
176
        );
177
178
        return GeneralUtility::makeInstance(SiteRepository::class);
179
    }
180
}
181