Passed
Push — master ( ae3f68...4bca36 )
by Timo
24:00
created

SolrConnection::setAuthenticationOnAllEndpoints()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Solr;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-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\System\Configuration\TypoScriptConfiguration;
28
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
29
use ApacheSolrForTypo3\Solr\System\Solr\Parser\SchemaParser;
30
use ApacheSolrForTypo3\Solr\System\Solr\Parser\StopWordParser;
31
use ApacheSolrForTypo3\Solr\System\Solr\Parser\SynonymParser;
32
use ApacheSolrForTypo3\Solr\System\Solr\Service\SolrAdminService;
33
use ApacheSolrForTypo3\Solr\System\Solr\Service\SolrReadService;
34
use ApacheSolrForTypo3\Solr\System\Solr\Service\SolrWriteService;
35
use ApacheSolrForTypo3\Solr\Util;
36
use Solarium\Client;
37
use Solarium\Core\Client\Endpoint;
38
use TYPO3\CMS\Core\Utility\GeneralUtility;
39
40
/**
41
 * Solr Service Access
42
 *
43
 * @author Ingo Renner <[email protected]>
44
 */
45
class SolrConnection
46
{
47
    /**
48
     * @var SolrAdminService
49
     */
50
    protected $adminService;
51
52
    /**
53
     * @var SolrReadService
54
     */
55
    protected $readService;
56
57
    /**
58
     * @var SolrWriteService
59
     */
60
    protected $writeService;
61
62
    /**
63
     * @var TypoScriptConfiguration
64
     */
65
    protected $configuration;
66
67
    /**
68
     * @var SynonymParser
69
     */
70
    protected $synonymParser = null;
71
72
    /**
73
     * @var StopWordParser
74
     */
75
    protected $stopWordParser = null;
76
77
    /**
78
     * @var SchemaParser
79
     */
80
    protected $schemaParser = null;
81
82
    /**
83
     * @var Node[]
84
     */
85
    protected $nodes = [];
86
87
    /**
88
     * @var SolrLogManager
89
     */
90
    protected $logger = null;
91
92
    /**
93
     * @var array
94
     */
95
    protected $clients = [];
96
97
    /**
98
     * Constructor
99
     *
100
     * @param Node $readNode,
101
     * @param Node $writeNode
102
     * @param TypoScriptConfiguration $configuration
103
     * @param SynonymParser $synonymParser
104
     * @param StopWordParser $stopWordParser
105
     * @param SchemaParser $schemaParser
106
     * @param SolrLogManager $logManager
107
     */
108 118
    public function __construct(
109
        Node $readNode,
110
        Node $writeNode,
111
        TypoScriptConfiguration $configuration = null,
112
        SynonymParser $synonymParser = null,
113
        StopWordParser $stopWordParser = null,
114
        SchemaParser $schemaParser = null,
115
        SolrLogManager $logManager = null
116
    ) {
117 118
        $this->nodes['read'] = $readNode;
118 118
        $this->nodes['write'] = $writeNode;
119 118
        $this->nodes['admin'] = $writeNode;
120 118
        $this->configuration = $configuration ?? Util::getSolrConfiguration();
121 118
        $this->synonymParser = $synonymParser;
122 118
        $this->stopWordParser = $stopWordParser;
123 118
        $this->schemaParser = $schemaParser;
124 118
        $this->logger = $logManager;
125 118
    }
126
127
    /**
128
     * @param string $key
129
     * @return Node
130
     */
131 110
    public function getNode($key)
132
    {
133 110
        return $this->nodes[$key];
134
    }
135
136
    /**
137
     * @return SolrAdminService
138
     */
139 8
    public function getAdminService()
140
    {
141 8
        if ($this->adminService === null) {
142 8
            $this->adminService = $this->buildAdminService();
143
        }
144
145 8
        return $this->adminService;
146
    }
147
148
    /**
149
     * @return SolrAdminService
150
     */
151 8
    protected function buildAdminService()
152
    {
153 8
        $endpointKey = 'admin';
154 8
        $client = $this->getClient($endpointKey);
155 8
        $this->initializeClient($client, $endpointKey);
156 8
        return GeneralUtility::makeInstance(SolrAdminService::class, $client, $this->configuration, $this->logger, $this->synonymParser, $this->stopWordParser, $this->schemaParser);
157
    }
158
159
    /**
160
     * @return SolrReadService
161
     */
162 56
    public function getReadService()
163
    {
164 56
        if ($this->readService === null) {
165 56
            $this->readService = $this->buildReadService();
166
        }
167
168 56
        return $this->readService;
169
    }
170
171
    /**
172
     * @return SolrReadService
173
     */
174 56
    protected function buildReadService()
175
    {
176 56
        $endpointKey = 'read';
177 56
        $client = $this->getClient($endpointKey);
178 56
        $this->initializeClient($client, $endpointKey);
179 56
        return GeneralUtility::makeInstance(SolrReadService::class, $client);
180
    }
181
182
    /**
183
     * @return SolrWriteService
184
     */
185 92
    public function getWriteService()
186
    {
187 92
        if ($this->writeService === null) {
188 92
            $this->writeService = $this->buildWriteService();
189
        }
190
191 92
        return $this->writeService;
192
    }
193
194
    /**
195
     * @return SolrWriteService
196
     */
197 92
    protected function buildWriteService()
198
    {
199 92
        $endpointKey = 'write';
200 92
        $client = $this->getClient($endpointKey);
201 92
        $this->initializeClient($client, $endpointKey);
202 92
        return GeneralUtility::makeInstance(SolrWriteService::class, $client);
203
    }
204
205
    /**
206
     * @param Client $client
207
     * @param string $endpointKey
208
     * @return Client
209
     */
210 109
    protected function initializeClient(Client $client, $endpointKey) {
211 109
        if (trim($this->getNode($endpointKey)->getUsername()) === '') {
212 108
            return $client;
213
        }
214
215 1
        $username = $this->getNode($endpointKey)->getUsername();
216 1
        $password = $this->getNode($endpointKey)->getPassword();
217 1
        $this->setAuthenticationOnAllEndpoints($client, $username, $password);
218
219 1
        return $client;
220
    }
221
222
    /**
223
     * @param Client $client
224
     * @param string $username
225
     * @param string $password
226
     */
227 1
    protected function setAuthenticationOnAllEndpoints(Client $client, $username, $password)
228
    {
229 1
        foreach ($client->getEndpoints() as $endpoint) {
230 1
            $endpoint->setAuthentication($username, $password);
231
        }
232 1
    }
233
234
    /**
235
     * Creates a string representation of the Solr connection. Specifically
236
     * will return the Solr URL.
237
     *
238
     * @deprecated Please use the string representations of the nodes instead
239
     * @return string The Solr URL.
240
     */
241
    public function __toString()
242
    {
243
        trigger_error('ConnectionManager::__toString is deprecated please use getNode($key)->__toString() now.', E_USER_DEPRECATED);
244
        $node = $this->getNode('read');
245
        return $node->__toString();
246
    }
247
248
    /**
249
     * @param string $endpointKey
250
     * @return Client
251
     */
252 109
    protected function getClient($endpointKey): Client
253
    {
254 109
        if($this->clients[$endpointKey]) {
255 2
            return $this->clients[$endpointKey];
256
        }
257
258 107
        $client = new Client(['adapter' => 'Solarium\Core\Client\Adapter\Guzzle']);
259 107
        $client->clearEndpoints();
260
261 107
        $newEndpointOptions = $this->getNode($endpointKey)->getSolariumClientOptions();
262 107
        $newEndpointOptions['key'] = $endpointKey;
263 107
        $client->createEndpoint($newEndpointOptions, true);
264
265 107
        $this->clients[$endpointKey] = $client;
266 107
        return $client;
267
    }
268
269
    /**
270
     * @param Client $client
271
     * @param string $endpointKey
272
     */
273 2
    public function setClient(Client $client, $endpointKey = 'read')
274
    {
275 2
        $this->clients[$endpointKey] = $client;
276 2
    }
277
}
278