Passed
Push — master ( ae02be...f196a8 )
by Timo
21:07
created

SiteRepository::getAvailableSites()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 17
cts 17
cp 1
rs 8.439
cc 6
eloc 18
nc 6
nop 1
crap 6
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 to retrieve instances of Site objects
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
     * @var Registry
57
     */
58
    protected $registry;
59
60
    /**
61
     * SiteRepository constructor.
62
     *
63
     * @param RootPageResolver|null $rootPageResolver
64
     * @param TwoLevelCache|null $twoLevelCache
65
     * @param Registry|null $registry
66
     */
67 135
    public function __construct(RootPageResolver $rootPageResolver = null, TwoLevelCache $twoLevelCache = null, Registry $registry = null)
68
    {
69 135
        $this->rootPageResolver = isset($rootPageResolver) ? $rootPageResolver : GeneralUtility::makeInstance(RootPageResolver::class);
70 135
        $this->runtimeCache = isset($twoLevelCache) ? $twoLevelCache : GeneralUtility::makeInstance(TwoLevelCache::class, 'cache_runtime');
71 135
        $this->registry = isset($registry) ? $registry : GeneralUtility::makeInstance(Registry::class);
72 135
    }
73
74
    /**
75
     * Gets the Site for a specific page Id.
76
     *
77
     * @param int $pageId The page Id to get a Site object for.
78
     * @param string $mountPointIdentifier
79
     * @return Site Site for the given page Id.
80
     */
81 73
    public function getSiteByPageId($pageId, $mountPointIdentifier = '')
82
    {
83 73
        $rootPageId = $this->rootPageResolver->getRootPageId($pageId, false, $mountPointIdentifier);
84 73
        return $this->getSiteByRootPageId($rootPageId);
85
    }
86
87
    /**
88
     * Gets the Site for a specific root page Id.
89
     *
90
     * @param int $rootPageId Root page Id to get a Site object for.
91
     * @return Site Site for the given page Id.
92
     */
93 81
    public function getSiteByRootPageId($rootPageId)
94
    {
95 81
        $cacheId = 'SiteRepository' . '_' . 'getSiteByPageId' . '_' . $rootPageId;
96
97 81
        $methodResult = $this->runtimeCache->get($cacheId);
98 81
        if (!empty($methodResult)) {
99 65
            return $methodResult;
100
        }
101
102 81
        $methodResult = $this->buildSite($rootPageId);
103 79
        $this->runtimeCache->set($cacheId, $methodResult);
104
105 79
        return $methodResult;
106
    }
107
108
    /**
109
     * Returns the first available Site.
110
     *
111
     * @param bool $stopOnInvalidSite
112
     * @return Site
113
     */
114 20
    public function getFirstAvailableSite($stopOnInvalidSite = false)
115
    {
116 20
        $sites = $this->getAvailableSites($stopOnInvalidSite);
117 20
        return array_shift($sites);
118
    }
119
120
    /**
121
     * Gets all available TYPO3 sites with Solr configured.
122
     *
123
     * @param bool $stopOnInvalidSite
124
     * @return Site[] An array of available sites
125
     */
126 70
    public function getAvailableSites($stopOnInvalidSite = false)
127
    {
128 70
        $sites = [];
129 70
        $cacheId = 'SiteRepository' . '_' . 'getAvailableSites';
130
131 70
        $methodResult = $this->runtimeCache->get($cacheId);
132 70
        if (!empty($methodResult)) {
133 15
            return $methodResult;
134
        }
135
136 70
        $servers = $this->getSolrServersFromRegistry();
137 70
        foreach ($servers as $server) {
138 70
            if (isset($sites[$server['rootPageUid']])) {
139
                //get each site only once
140 4
                continue;
141
            }
142
143
            try {
144 70
                $sites[$server['rootPageUid']] = $this->buildSite($server['rootPageUid']);
145 4
            } catch (\InvalidArgumentException $e) {
146 4
                if ($stopOnInvalidSite) {
147 70
                    throw $e;
148
                }
149
            }
150
        }
151
152 70
        $methodResult = $sites;
153 70
        $this->runtimeCache->set($cacheId, $methodResult);
154
155 70
        return $methodResult;
156
    }
157
158
    /**
159
     * Gets the system languages (IDs) for which Solr connections have been
160
     * configured.
161
     *
162
     * @return array Array of system language IDs for which connections have been configured on this site.
163
     */
164 1
    public function getAllLanguages(Site $site)
165
    {
166 1
        $siteLanguages = [];
167 1
        $servers = $this->getSolrServersFromRegistry();
168
169 1
        foreach ($servers as $connectionKey => $solrConnection) {
170 1
            list($siteRootPageId, $systemLanguageId) = explode('|', $connectionKey);
171
172 1
            if ($siteRootPageId == $site->getRootPageId()) {
173 1
                $siteLanguages[] = $systemLanguageId;
174
            }
175
        }
176
177 1
        return $siteLanguages;
178
    }
179
180
    /**
181
     * Creates an instance of the Site object.
182
     *
183
     * @param integer $rootPageId
184
     * @return Site
185
     */
186 116
    protected function buildSite($rootPageId)
187
    {
188 116
        return GeneralUtility::makeInstance(Site::class, $rootPageId);
189
    }
190
191
    /**
192
     * Retrieves the configured solr servers from the registry.
193
     *
194
     * @return array
195
     */
196 70
    protected function getSolrServersFromRegistry()
197
    {
198 70
        $servers = (array)$this->registry->get('tx_solr', 'servers', []);
199 70
        return $servers;
200
    }
201
}
202