Failed Conditions
Push — master ( 9a88de...ee0235 )
by Timo
23:42
created

Site::getSiteHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\ConfigurationAwareRecordService;
28
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
29
use ApacheSolrForTypo3\Solr\System\Records\Pages\PagesRepository;
30
use TYPO3\CMS\Backend\Utility\BackendUtility;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Backend\Utility\BackendUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * A site is a branch in a TYPO3 installation. Each site's root page is marked
35
 * by the "Use as Root Page" flag.
36
 *
37
 * @author Ingo Renner <[email protected]>
38
 */
39
class Site
40
{
41
42
    /**
43
     * @var TypoScriptConfiguration
44
     */
45
    protected $configuration;
46
47
    /**
48
     * Cache for ApacheSolrForTypo3\Solr\Site objects
49
     *
50
     * @var array
51
     */
52
    protected static $sitesCache = [];
53
54
    /**
55
     * Small cache for the list of pages in a site, so that the results of this
56
     * rather expensive operation can be used by all initializers without having
57
     * each initializer do it again.
58
     *
59
     * TODO Move to caching framework once TYPO3 4.6 is the minimum required
60
     * version.
61
     *
62
     * @var array
63
     */
64
    protected static $sitePagesCache = [];
65
66
    /**
67
     * Root page record.
68
     *
69
     * @var array
70
     */
71
    protected $rootPage = [];
72
73
    /**
74
     * The site's sys_language_mode
75
     *
76
     * @var string
77
     */
78
    protected $sysLanguageMode = null;
79
80
    /**
81
     * @var string
82
     */
83
    protected $domain;
84
85
    /**
86
     * @var string
87
     */
88
    protected $siteHash;
89
90
    /**
91
     * @var PagesRepository
92
     */
93
    protected $pagesRepository;
94
95
    /**
96
     * Constructor.
97
     *
98
     * @param TypoScriptConfiguration $configuration
99
     * @param array $page Site root page ID (uid). The page must be marked as site root ("Use as Root Page" flag).
100
     * @param string $domain The domain record used by this Site
101
     * @param string $siteHash The site hash used by this site
102
     * @param PagesRepository $pagesRepository
103
     */
104 137
    public function __construct(TypoScriptConfiguration $configuration, array $page, $domain, $siteHash, PagesRepository $pagesRepository = null)
105
    {
106 137
        $this->configuration = $configuration;
107 137
        $this->rootPage = $page;
108 137
        $this->domain = $domain;
109 137
        $this->siteHash = $siteHash;
110 137
        $this->pagesRepository = isset($pagesRepository) ? $pagesRepository : GeneralUtility::makeInstance(PagesRepository::class);
111 137
    }
112
113
    /**
114
     * Clears the $sitePagesCache
115
     *
116
     */
117
    public static function clearSitePagesCache()
118
    {
119
        self::$sitePagesCache = [];
120
    }
121
122
    /**
123
     * Takes an pagerecord and checks whether the page is marked as root page.
124
     *
125
     * @param array $page pagerecord
126
     * @return bool true if the page is marked as root page, false otherwise
127
     */
128 157
    public static function isRootPage($page)
129
    {
130 157
        if ($page['is_siteroot']) {
131 156
            return true;
132
        }
133
134 83
        return false;
135
    }
136
137
    /**
138
     * Gets the site's root page ID (uid).
139
     *
140
     * @return int The site's root page ID.
141
     */
142 50
    public function getRootPageId()
143
    {
144 50
        return (int)$this->rootPage['uid'];
145
    }
146
147
    /**
148
     * Gets the site's label. The label is build from the the site title and root
149
     * page ID (uid).
150
     *
151
     * @return string The site's label.
152
     */
153 14
    public function getLabel()
154
    {
155 14
        $rootlineTitles = [];
156 14
        $rootLine = BackendUtility::BEgetRootLine($this->rootPage['uid']);
157
        // Remove last
158 14
        array_pop($rootLine);
159 14
        $rootLine = array_reverse($rootLine);
160 14
        foreach ($rootLine as $rootLineItem) {
161 14
            $rootlineTitles[] = $rootLineItem['title'];
162
        }
163 14
        return implode(' - ', $rootlineTitles) . ', Root Page ID: ' . $this->rootPage['uid'];
164
    }
165
166
    /**
167
     * Gets the site's Solr TypoScript configuration (plugin.tx_solr.*)
168
     *
169
     * @return  \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration The Solr TypoScript configuration
170
     */
171 58
    public function getSolrConfiguration()
172
    {
173 58
        return $this->configuration;
174
    }
175
176
    /**
177
     * Gets the site's default language as configured in
178
     * config.sys_language_uid. If sys_language_uid is not set, 0 is assumed to
179
     * be the default.
180
     *
181
     * @return int The site's default language.
182
     */
183 1
    public function getDefaultLanguage()
184
    {
185 1
        $siteDefaultLanguage = 0;
186
187 1
        $configuration = Util::getConfigurationFromPageId(
188 1
            $this->rootPage['uid'],
189 1
            'config'
190
        );
191
192 1
        $siteDefaultLanguage = $configuration->getValueByPathOrDefaultValue('sys_language_uid', $siteDefaultLanguage);
193
        // default language is set through default L GET parameter -> overruling config.sys_language_uid
194 1
        $siteDefaultLanguage = $configuration->getValueByPathOrDefaultValue('defaultGetVars.L', $siteDefaultLanguage);
195
196 1
        return $siteDefaultLanguage;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $siteDefaultLanguage also could return the type array which is incompatible with the documented return type integer.
Loading history...
197
    }
198
199
    /**
200
     * Generates a list of page IDs in this site. Attention, this includes
201
     * all page types! Deleted pages are not included.
202
     *
203
     * @param int|string $rootPageId Page ID from where to start collection sub pages
204
     * @param int $maxDepth Maximum depth to descend into the site tree
205
     * @return array Array of pages (IDs) in this site
206
     */
207 12
    public function getPages($rootPageId = 'SITE_ROOT', $maxDepth = 999)
208
    {
209 12
        $pageIds = [];
210 12
        if ($rootPageId === 'SITE_ROOT') {
211 12
            $rootPageId = (int)$this->rootPage['uid'];
212 12
            $pageIds[] = $rootPageId;
213
        }
214
215 12
        $configurationAwareRecordService = GeneralUtility::makeInstance(ConfigurationAwareRecordService::class);
216
        // Fetch configuration in order to be able to read initialPagesAdditionalWhereClause
217 12
        $solrConfiguration = $this->getSolrConfiguration();
218 12
        $indexQueueConfigurationName = $configurationAwareRecordService->getIndexingConfigurationName('pages', $this->rootPage['uid'], $solrConfiguration);
219 12
        $initialPagesAdditionalWhereClause = $solrConfiguration->getInitialPagesAdditionalWhereClause($indexQueueConfigurationName);
220
221 12
        return array_merge($pageIds, $this->pagesRepository->findAllSubPageIdsByRootPage($rootPageId, $maxDepth, $initialPagesAdditionalWhereClause));
0 ignored issues
show
Bug introduced by
It seems like $rootPageId can also be of type string; however, parameter $rootPageId of ApacheSolrForTypo3\Solr\...lSubPageIdsByRootPage() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

221
        return array_merge($pageIds, $this->pagesRepository->findAllSubPageIdsByRootPage(/** @scrutinizer ignore-type */ $rootPageId, $maxDepth, $initialPagesAdditionalWhereClause));
Loading history...
222
    }
223
224
    /**
225
     * Generates the site's unique Site Hash.
226
     *
227
     * The Site Hash is build from the site's main domain, the system encryption
228
     * key, and the extension "tx_solr". These components are concatenated and
229
     * sha1-hashed.
230
     *
231
     * @return string Site Hash.
232
     */
233 86
    public function getSiteHash()
234
    {
235 86
        return $this->siteHash;
236
    }
237
238
    /**
239
     * Gets the site's main domain. More specifically the first domain record in
240
     * the site tree.
241
     *
242
     * @return string The site's main domain.
243
     */
244 91
    public function getDomain()
245
    {
246 91
        return $this->domain;
247
    }
248
249
    /**
250
     * Gets the site's root page.
251
     *
252
     * @return array The site's root page.
253
     */
254 21
    public function getRootPage()
255
    {
256 21
        return $this->rootPage;
257
    }
258
259
    /**
260
     * Gets the site's root page's title.
261
     *
262
     * @return string The site's root page's title
263
     */
264
    public function getTitle()
265
    {
266
        return $this->rootPage['title'];
267
    }
268
269
    /**
270
     * Gets the site's config.sys_language_mode setting
271
     *
272
     * @param int $languageUid
273
     *
274
     * @return string The site's config.sys_language_mode
275
     */
276 20
    public function getSysLanguageMode($languageUid = 0)
277
    {
278 20
        if (is_null($this->sysLanguageMode)) {
0 ignored issues
show
introduced by
The condition is_null($this->sysLanguageMode) can never be true.
Loading history...
279 20
            Util::initializeTsfe($this->getRootPageId(), $languageUid);
280 20
            $this->sysLanguageMode = $GLOBALS['TSFE']->sys_language_mode;
281
        }
282
283 20
        return $this->sysLanguageMode;
284
    }
285
}
286