Passed
Pull Request — master (#2925)
by Rafael
36:15
created

Site   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 20
eloc 43
c 2
b 0
f 0
dl 0
loc 220
ccs 46
cts 48
cp 0.9583
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 3 1
A getRootPageId() 0 3 1
A getSolrConfiguration() 0 3 1
A getRootPage() 0 3 1
A getLabel() 0 11 2
A getDefaultLanguage() 0 3 1
A getPages() 0 14 2
A getRootPageIdsFromSites() 0 8 2
A getAllSolrConnectionConfigurations() 0 8 3
A getSiteHash() 0 3 1
A getDomain() 0 3 1
A isEnabled() 0 3 1
A isRootPage() 0 7 2
A getAvailableLanguageIds() 0 2 1
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Site;
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
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
19
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
20
use ApacheSolrForTypo3\Solr\System\Records\Pages\PagesRepository;
21
use TYPO3\CMS\Backend\Utility\BackendUtility;
22
23
/**
24
 * Base Class for Typo3ManagedSite and LegacySite
25
 *
26
 * (c) 2011-2015 Ingo Renner <[email protected]>
27
 */
28
abstract class Site implements SiteInterface
29
{
30
    /**
31
     * @var TypoScriptConfiguration
32
     */
33
    protected $configuration;
34
35
    /**
36
     * Root page record.
37
     *
38
     * @var array
39
     */
40
    protected $rootPage = [];
41
    /**
42
     * @var string
43
     */
44
    protected $domain;
45
46
    /**
47
     * @var string
48
     */
49
    protected $siteHash;
50
51
    /**
52
     * @var PagesRepository
53
     */
54
    protected $pagesRepository;
55
56
    /**
57
     * @var int
58
     */
59
    protected $defaultLanguageId = 0;
60
61
    /**
62
     * @var int[] Available language ids
63
     */
64
    protected $availableLanguageIds = [];
65
66
    /**
67
     * Takes an pagerecord and checks whether the page is marked as root page.
68
     *
69
     * @param array $page pagerecord
70
     * @return bool true if the page is marked as root page, false otherwise
71
     * @todo: move to SiteUtility?
72
     */
73 182
    public static function isRootPage($page)
74
    {
75 182
        if ($page['is_siteroot']) {
76 181
            return true;
77
        }
78
79 93
        return false;
80
    }
81
82
    /**
83
     * Gets the site's root page ID (uid).
84
     *
85
     * @return int The site's root page ID.
86
     */
87 83
    public function getRootPageId()
88
    {
89 83
        return (int)$this->rootPage['uid'];
90
    }
91
92
    /**
93
     * Gets available language id's for this site
94
     *
95
     * @return int[] array or language id's
96
     */
97 86
    public function getAvailableLanguageIds(): array {
98 86
        return $this->availableLanguageIds;
99
    }
100
101
    /**
102
     * Gets the site's label. The label is build from the the site title and root
103
     * page ID (uid).
104
     *
105
     * @return string The site's label.
106
     */
107 3
    public function getLabel()
108
    {
109 3
        $rootlineTitles = [];
110 3
        $rootLine = BackendUtility::BEgetRootLine($this->rootPage['uid']);
111
        // Remove last
112 3
        array_pop($rootLine);
113 3
        $rootLine = array_reverse($rootLine);
114 3
        foreach ($rootLine as $rootLineItem) {
115 3
            $rootlineTitles[] = $rootLineItem['title'];
116
        }
117 3
        return implode(' - ', $rootlineTitles) . ', Root Page ID: ' . $this->rootPage['uid'];
118
    }
119
120
    /**
121
     * Gets the site's Solr TypoScript configuration (plugin.tx_solr.*)
122
     *
123
     * Purpose: Interface and Unit test mocking helper method.
124
     *
125
     * @return  TypoScriptConfiguration The Solr TypoScript configuration
126
     */
127 64
    public function getSolrConfiguration(): TypoScriptConfiguration
128
    {
129 64
        return $this->configuration;
130
    }
131
132
    /**
133
     * Gets the site's default language as configured in
134
     * config.sys_language_uid. If sys_language_uid is not set, 0 is assumed to
135
     * be the default.
136
     *
137
     * @return int The site's default language.
138
     */
139 1
    public function getDefaultLanguage()
140
    {
141 1
        return $this->defaultLanguageId;
142
    }
143
144
    /**
145
     * @inheritDoc
146
     */
147 13
    public function getPages(
148
        ?int $pageId = null,
149
        ?string $indexQueueConfigurationName = null
150
    ): array
151
    {
152 13
        $pageId = $pageId ?? (int)$this->rootPage['uid'];
153
154 13
        $initialPagesAdditionalWhereClause = '';
155
        // Fetch configuration in order to be able to read initialPagesAdditionalWhereClause
156 13
        if ($indexQueueConfigurationName !== null) {
157 12
            $solrConfiguration = $this->getSolrConfiguration();
158 12
            $initialPagesAdditionalWhereClause = $solrConfiguration->getInitialPagesAdditionalWhereClause($indexQueueConfigurationName);
159
        }
160 13
        return $this->pagesRepository->findAllSubPageIdsByRootPage($pageId, $initialPagesAdditionalWhereClause);
161
    }
162
163
    /**
164
     * Generates the site's unique Site Hash.
165
     *
166
     * The Site Hash is build from the site's main domain, the system encryption
167
     * key, and the extension "tx_solr". These components are concatenated and
168
     * sha1-hashed.
169
     *
170
     * @return string Site Hash.
171
     */
172 95
    public function getSiteHash()
173
    {
174 95
        return $this->siteHash;
175
    }
176
177
    /**
178
     * Gets the site's main domain. More specifically the first domain record in
179
     * the site tree.
180
     *
181
     * @return string The site's main domain.
182
     */
183 94
    public function getDomain()
184
    {
185 94
        return $this->domain;
186
    }
187
188
    /**
189
     * Gets the site's root page.
190
     *
191
     * @return array The site's root page.
192
     */
193 21
    public function getRootPage()
194
    {
195 21
        return $this->rootPage;
196
    }
197
198
    /**
199
     * Gets the site's root page's title.
200
     *
201
     * @return string The site's root page's title
202
     */
203
    public function getTitle()
204
    {
205
        return $this->rootPage['title'];
206
    }
207
208
    /**
209
     * Retrieves the rootPageIds as an array from a set of sites.
210
     *
211
     * @param array $sites
212
     * @return array
213
     * @todo: move to SiteUtility?
214
     */
215 44
    public static function getRootPageIdsFromSites(array $sites): array
216
    {
217 44
        $rootPageIds = [];
218 44
        foreach ($sites as $site) {
219 7
            $rootPageIds[] = (int)$site->getRootPageId();
220
        }
221
222 44
        return $rootPageIds;
223
    }
224
225
    /**
226
     * @return array
227
     */
228 85
    public function getAllSolrConnectionConfigurations(): array {
229 85
        $configs = [];
230 85
        foreach ($this->getAvailableLanguageIds() as $languageId) {
231
            try {
232 85
                $configs[$languageId] = $this->getSolrConnectionConfiguration($languageId);
233 1
            } catch (NoSolrConnectionFoundException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
234
        }
235 85
        return $configs;
236
    }
237
238 77
    public function isEnabled(): bool
239
    {
240 77
        return !empty($this->getAllSolrConnectionConfigurations());
241
    }
242
243
    /**
244
     * @param int $language
245
     * @return array
246
     */
247
    abstract function getSolrConnectionConfiguration(int $language = 0): array;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
248
}
249