Passed
Push — release-11.5.x ( 385fe8...cd49eb )
by Rafael
53:22 queued 14:05
created

getDomainByPageIdAndReplaceMarkers()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
ccs 5
cts 7
cp 0.7143
rs 10
c 0
b 0
f 0
cc 2
nc 3
nop 2
crap 2.0932
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 213
    public function __construct(SiteFinder $siteFinder)
42
    {
43 213
        $this->siteFinder = $siteFinder;
44
    }
45
46
    /**
47
     * 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
     *   __all - Adds all domains as allowed sites
52
     *   * - Means all sites are allowed, same as no siteHash
53
     *
54
     * @param int $pageId A page ID that is then resolved to the site it belongs to
55
     * @param string|null $allowedSitesConfiguration TypoScript setting for allowed sites
56
     * @return string List of allowed sites/domains, magic keywords resolved
57
     * @throws DBALDriverException
58
     * @throws Throwable
59
     */
60 43
    public function getAllowedSitesForPageIdAndAllowedSitesConfiguration(
61
        int $pageId,
62
        ?string $allowedSitesConfiguration = ''
63
    ): string {
64 43
        if ($allowedSitesConfiguration === '__all') {
65 2
            return $this->getDomainListOfAllSites();
66
        }
67 41
        if ($allowedSitesConfiguration === '*') {
68 2
            return '*';
69
        }
70
        // we thread empty allowed site configurations as __solr_current_site since this is the default behaviour
71 39
        $allowedSitesConfiguration = empty($allowedSitesConfiguration) ? '__solr_current_site' : $allowedSitesConfiguration;
72 39
        return $this->getDomainByPageIdAndReplaceMarkers($pageId, $allowedSitesConfiguration);
73
    }
74
75
    /**
76
     * 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 198
    public function getSiteHashForDomain(string $domain): string
82
    {
83 198
        static $siteHashes = [];
84 198
        if (isset($siteHashes[$domain])) {
85 114
            return $siteHashes[$domain];
86
        }
87
88 198
        $siteHashes[$domain] = sha1($domain . $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . 'tx_solr');
89 198
        return $siteHashes[$domain];
90
    }
91
92
    /**
93
     * Returns a comma separated list of all domains from all sites.
94
     *
95
     * @return string
96
     * @throws DBALDriverException
97
     * @throws Throwable
98
     */
99 2
    protected function getDomainListOfAllSites(): string
100
    {
101 2
        $sites = $this->siteFinder->getAllSites();
102 2
        $domains = [];
103 2
        foreach ($sites as $typo3Site) {
104 2
            $connections = SiteUtility::getAllSolrConnectionConfigurations($typo3Site);
105 2
            if (!empty($connections)) {
106 2
                $domains[] = $typo3Site->getBase()->getHost();
107
            }
108
        }
109
110 2
        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
     * @param int $pageId
118
     * @param string $allowedSitesConfiguration
119
     * @return string
120
     */
121 39
    protected function getDomainByPageIdAndReplaceMarkers(int $pageId, string $allowedSitesConfiguration): string
122
    {
123
        try {
124 39
            $typo3Site = $this->siteFinder->getSiteByPageId($pageId);
125 39
            $domainOfPage = $typo3Site->getBase()->getHost();
126
        } catch (SiteNotFoundException $e) {
127
            return '';
128
        }
129
130 39
        $allowedSites = str_replace(['__solr_current_site', '__current_site'], $domainOfPage, $allowedSitesConfiguration);
131 39
        return (string)$allowedSites;
132
    }
133
134
    /**
135
     * @return Site[]
136
     * @throws DBALDriverException
137
     * @throws Throwable
138
     * @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