Passed
Pull Request — master (#1151)
by
unknown
19:19
created

SiteRepository::getAllLanguages()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
crap 12
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Site;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2017 - Thomas Hohn <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\RootPageResolver;
29
use ApacheSolrForTypo3\Solr\Site;
30
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache;
31
use TYPO3\CMS\Core\Registry;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
34
/**
35
 * SiteRepository
36
 *
37
 * Responsible for...
38
 *
39
 * @author Thomas Hohn <[email protected]>
40
 */
41
class SiteRepository
42
{
43
    /**
44
     * Rootpage resolver
45
     *
46
     * @var RootPageResolver
47
     */
48
    protected $rootPageResolver;
49
50
    /**
51
     * @var TwoLevelCache
52
     */
53
    protected $runtimeCache;
54
55
    /**
56
     * SiteRepository constructor.
57
     *
58
     * @param RootPageResolver|null $rootPageResolver
59
     * @param TwoLevelCache|null $twoLevelCache
60
     */
61 110
    public function __construct(RootPageResolver $rootPageResolver = null, TwoLevelCache $twoLevelCache = null)
62
    {
63 110
        $this->rootPageResolver = isset($rootPageResolver) ? $rootPageResolver : GeneralUtility::makeInstance(RootPageResolver::class);
64 110
        $this->runtimeCache = isset($twoLevelCache) ? $twoLevelCache : GeneralUtility::makeInstance(TwoLevelCache::class,
65 110
            'cache_runtime');
66 110
    }
67
68
    /**
69
     * Gets the Site for a specific page Id.
70
     *
71
     * @param int $pageId The page Id to get a Site object for.
72
     * @return Site Site for the given page Id.
73
     */
74 64
    public function getSiteByPageId($pageId)
75
    {
76 64
        $rootPageId = $this->rootPageResolver->getRootPageId($pageId);
77 64
        return $this->getSiteByRootPageId($rootPageId);
78
    }
79
80
    /**
81
     * Gets the Site for a specific root page Id.
82
     *
83
     * @param int $rootPageId Root page Id to get a Site object for.
84
     * @return Site Site for the given page Id.
85
     */
86 71
    public function getSiteByRootPageId($rootPageId)
87
    {
88 71
        $cacheId = 'SiteRepository' . '_' . 'getSiteByPageId' . '_' . $rootPageId;
89
90 71
        $methodResult = $this->runtimeCache->get($cacheId);
91 71
        if (!empty($methodResult)) {
92 59
            return $methodResult;
93
        }
94
95 71
        $methodResult = GeneralUtility::makeInstance(Site::class, $rootPageId);
96 69
        $this->runtimeCache->set($cacheId, $methodResult);
97
98 69
        return $methodResult;
99
    }
100
101
    /**
102
     * Returns the first available Site.
103
     *
104
     * @param bool $stopOnInvalidSite
105
     * @return Site
106
     */
107 20
    public function getFirstAvailableSite($stopOnInvalidSite = false)
108
    {
109 20
        $sites = $this->getAvailableSites($stopOnInvalidSite);
110 20
        return array_shift($sites);
111
    }
112
113
    /**
114
     * Gets all available TYPO3 sites with Solr configured.
115
     *
116
     * @param bool $stopOnInvalidSite
117
     * @return Site[] An array of available sites
118
     */
119 62
    public function getAvailableSites($stopOnInvalidSite = false)
120
    {
121 62
        $sites = [];
122 62
        $cacheId = 'SiteRepository' . '_' . 'getAvailableSites';
123
124 62
        $methodResult = $this->runtimeCache->get($cacheId);
125 62
        if (!empty($methodResult)) {
126 15
            return $methodResult;
127
        }
128
129 62
        $servers = $this->getSolrServersFromRegistry();
130
131 62
        foreach ($servers as $server) {
132 62
            if (isset($sites[$server['rootPageUid']])) {
133
                //get each site only once
134 1
                continue;
135
            }
136
137
            try {
138 62
                $sites[$server['rootPageUid']] = GeneralUtility::makeInstance(Site::class, $server['rootPageUid']);
139 4
            } catch (\InvalidArgumentException $e) {
140 4
                if ($stopOnInvalidSite) {
141 62
                    throw $e;
142
                }
143
            }
144
        }
145
146 62
        $methodResult = $sites;
147 62
        $this->runtimeCache->set($cacheId, $methodResult);
148
149 62
        return $methodResult;
150
    }
151
152
    /**
153
     * Gets the system languages (IDs) for which Solr connections have been
154
     * configured.
155
     *
156
     * @return array Array of system language IDs for which connections have been configured on this site.
157
     */
158
    public function getAllLanguages(Site $site)
159
    {
160
        $siteLanguages = [];
161
162
        $servers = $this->getSolrServersFromRegistry();
163
164
        foreach ($servers as $connectionKey => $solrConnection) {
165
            list($siteRootPageId, $systemLanguageId) = explode('|',
166
                $connectionKey);
167
168
            if ($siteRootPageId == $site->getRootPage()) {
169
                $siteLanguages[] = $systemLanguageId;
170
            }
171
        }
172
173
        return $siteLanguages;
174
    }
175
176
    /**
177
     * Retrieves the configured solr servers from the registry.
178
     *
179
     * @return array
180
     */
181 62
    protected function getSolrServersFromRegistry()
182
    {
183
        /** @var $registry Registry */
184 62
        $registry = GeneralUtility::makeInstance(Registry::class);
185 62
        $servers = (array)$registry->get('tx_solr', 'servers', []);
186 62
        return $servers;
187
    }
188
}
189