Passed
Branch master (5f6eff)
by Rafael
04:03
created

SolrConnection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 9.8666
cc 1
nc 1
nop 11
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 Psr\Container\ContainerExceptionInterface;
37
use Psr\Container\NotFoundExceptionInterface;
38
use Psr\EventDispatcher\EventDispatcherInterface;
39
use Psr\Http\Client\ClientInterface;
40
use Psr\Http\Message\RequestFactoryInterface;
41
use Psr\Http\Message\StreamFactoryInterface;
42
use Solarium\Client;
43
use Solarium\Core\Client\Adapter\Psr18Adapter;
44
use TYPO3\CMS\Core\Utility\GeneralUtility;
45
46
/**
47
 * Solr Service Access
48
 *
49
 * @author Ingo Renner <[email protected]>
50
 */
51
class SolrConnection
52
{
53
    /**
54
     * @var SolrAdminService
55
     */
56
    protected $adminService;
57
58
    /**
59
     * @var SolrReadService
60
     */
61
    protected $readService;
62
63
    /**
64
     * @var SolrWriteService
65
     */
66
    protected $writeService;
67
68
    /**
69
     * @var TypoScriptConfiguration
70
     */
71
    protected $configuration;
72
73
    /**
74
     * @var SynonymParser
75
     */
76
    protected $synonymParser = null;
77
78
    /**
79
     * @var StopWordParser
80
     */
81
    protected $stopWordParser = null;
82
83
    /**
84
     * @var SchemaParser
85
     */
86
    protected $schemaParser = null;
87
88
    /**
89
     * @var Node[]
90
     */
91
    protected $nodes = [];
92
93
    /**
94
     * @var SolrLogManager
95
     */
96
    protected $logger = null;
97
98
    /**
99
     * @var ClientInterface[]
100
     */
101
    protected $clients = [];
102
103
    /**
104
     * @var ClientInterface
105
     */
106
    protected $psr7Client;
107
108
    /**
109
     * @var RequestFactoryInterface
110
     */
111
    protected $requestFactory;
112
113
    /**
114
     * @var StreamFactoryInterface
115
     */
116
    protected $streamFactory;
117
118
    /**
119
     * @var EventDispatcherInterface
120
     */
121
    protected $eventDispatcher;
122
123
    /**
124
     * Constructor
125
     *
126
     * @param Node $readNode
127
     * @param Node $writeNode
128
     * @param ?TypoScriptConfiguration $configuration
129
     * @param ?SynonymParser $synonymParser
130
     * @param ?StopWordParser $stopWordParser
131
     * @param ?SchemaParser $schemaParser
132
     * @param ?SolrLogManager $logManager
133
     * @param ?ClientInterface $psr7Client
134
     * @param ?RequestFactoryInterface $requestFactory
135
     * @param ?StreamFactoryInterface $streamFactory
136
     * @param ?EventDispatcherInterface $eventDispatcher
137
     *
138
     * @throws ContainerExceptionInterface
139
     * @throws NotFoundExceptionInterface
140
     */
141 111
    public function __construct(
142
        Node $readNode,
143
        Node $writeNode,
144
        TypoScriptConfiguration $configuration = null,
145
        SynonymParser $synonymParser = null,
146
        StopWordParser $stopWordParser = null,
147
        SchemaParser $schemaParser = null,
148
        SolrLogManager $logManager = null,
149
        ClientInterface $psr7Client = null,
150
        RequestFactoryInterface $requestFactory = null,
151
        StreamFactoryInterface $streamFactory = null,
152
        EventDispatcherInterface $eventDispatcher = null
153
    ) {
154 111
        $this->nodes['read'] = $readNode;
155 111
        $this->nodes['write'] = $writeNode;
156 111
        $this->nodes['admin'] = $writeNode;
157 111
        $this->configuration = $configuration ?? Util::getSolrConfiguration();
158 111
        $this->synonymParser = $synonymParser;
159 111
        $this->stopWordParser = $stopWordParser;
160 111
        $this->schemaParser = $schemaParser;
161 111
        $this->logger = $logManager;
162 111
        $this->psr7Client = $psr7Client ?? GeneralUtility::getContainer()->get(ClientInterface::class);
163 111
        $this->requestFactory = $requestFactory ?? GeneralUtility::getContainer()->get(RequestFactoryInterface::class);
164 111
        $this->streamFactory = $streamFactory ?? GeneralUtility::getContainer()->get(StreamFactoryInterface::class);
165 111
        $this->eventDispatcher = $eventDispatcher ?? GeneralUtility::getContainer()->get(EventDispatcherInterface::class);
166 111
    }
167
168
    /**
169
     * @param string $key
170
     * @return Node
171
     */
172 106
    public function getNode(string $key): Node
173
    {
174 106
        return $this->nodes[$key];
175
    }
176
177
    /**
178
     * @return SolrAdminService
179
     */
180 3
    public function getAdminService(): SolrAdminService
181
    {
182 3
        if ($this->adminService === null) {
183 3
            $this->adminService = $this->buildAdminService();
184
        }
185
186 3
        return $this->adminService;
187
    }
188
189
    /**
190
     * @return SolrAdminService
191
     * @noinspection PhpIncompatibleReturnTypeInspection
192
     */
193 3
    protected function buildAdminService(): SolrAdminService
194
    {
195 3
        $endpointKey = 'admin';
196 3
        $client = $this->getClient($endpointKey);
197 3
        $this->initializeClient($client, $endpointKey);
198 3
        return GeneralUtility::makeInstance(SolrAdminService::class, $client, $this->configuration, $this->logger, $this->synonymParser, $this->stopWordParser, $this->schemaParser);
199
    }
200
201
    /**
202
     * @return SolrReadService
203
     */
204 54
    public function getReadService(): SolrReadService
205
    {
206 54
        if ($this->readService === null) {
207 54
            $this->readService = $this->buildReadService();
208
        }
209
210 54
        return $this->readService;
211
    }
212
213
    /**
214
     * @return SolrReadService
215
     * @noinspection PhpIncompatibleReturnTypeInspection
216
     */
217 54
    protected function buildReadService(): SolrReadService
218
    {
219 54
        $endpointKey = 'read';
220 54
        $client = $this->getClient($endpointKey);
221 54
        $this->initializeClient($client, $endpointKey);
222 54
        return GeneralUtility::makeInstance(SolrReadService::class, $client);
223
    }
224
225
    /**
226
     * @return SolrWriteService
227
     */
228 95
    public function getWriteService(): SolrWriteService
229
    {
230 95
        if ($this->writeService === null) {
231 95
            $this->writeService = $this->buildWriteService();
232
        }
233
234 95
        return $this->writeService;
235
    }
236
237
    /**
238
     * @return SolrWriteService
239
     * @noinspection PhpIncompatibleReturnTypeInspection
240
     */
241 95
    protected function buildWriteService(): SolrWriteService
242
    {
243 95
        $endpointKey = 'write';
244 95
        $client = $this->getClient($endpointKey);
245 95
        $this->initializeClient($client, $endpointKey);
246 95
        return GeneralUtility::makeInstance(SolrWriteService::class, $client);
247
    }
248
249
    /**
250
     * @param Client $client
251
     * @param string $endpointKey
252
     * @return Client
253
     */
254 102
    protected function initializeClient(Client $client, string $endpointKey): Client
255
    {
256 102
        if (trim($this->getNode($endpointKey)->getUsername()) === '') {
257 102
            return $client;
258
        }
259
260
        $username = $this->getNode($endpointKey)->getUsername();
261
        $password = $this->getNode($endpointKey)->getPassword();
262
        $this->setAuthenticationOnAllEndpoints($client, $username, $password);
263
264
        return $client;
265
    }
266
267
    /**
268
     * @param Client $client
269
     * @param string $username
270
     * @param string $password
271
     */
272
    protected function setAuthenticationOnAllEndpoints(Client $client, string $username, string $password)
273
    {
274
        foreach ($client->getEndpoints() as $endpoint) {
275
            $endpoint->setAuthentication($username, $password);
276
        }
277
    }
278
279
    /**
280
     * @param string $endpointKey
281
     * @return Client
282
     */
283 102
    protected function getClient(string $endpointKey): Client
284
    {
285 102
        if ($this->clients[$endpointKey]) {
286
            return $this->clients[$endpointKey];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->clients[$endpointKey] returns the type Psr\Http\Client\ClientInterface which is incompatible with the type-hinted return Solarium\Client.
Loading history...
287
        }
288
289 102
        $adapter = new Psr18Adapter($this->psr7Client, $this->requestFactory, $this->streamFactory);
290
291 102
        $client = new Client($adapter, $this->eventDispatcher);
292 102
        $client->getPlugin('postbigrequest');
293 102
        $client->clearEndpoints();
294
295 102
        $newEndpointOptions = $this->getNode($endpointKey)->getSolariumClientOptions();
296 102
        $newEndpointOptions['key'] = $endpointKey;
297 102
        $client->createEndpoint($newEndpointOptions, true);
298
299 102
        $this->clients[$endpointKey] = $client;
300 102
        return $client;
301
    }
302
303
    /**
304
     * @param Client $client
305
     * @param ?string $endpointKey
306
     */
307
    public function setClient(Client $client, ?string $endpointKey = 'read')
308
    {
309
        $this->clients[$endpointKey] = $client;
310
    }
311
}
312