Passed
Push — master ( 182bcf...5c27dd )
by Rafael
34:26
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 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
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 110
    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 110
        $this->nodes['read'] = $readNode;
155 110
        $this->nodes['write'] = $writeNode;
156 110
        $this->nodes['admin'] = $writeNode;
157 110
        $this->configuration = $configuration ?? Util::getSolrConfiguration();
158 110
        $this->synonymParser = $synonymParser;
159 110
        $this->stopWordParser = $stopWordParser;
160 110
        $this->schemaParser = $schemaParser;
161 110
        $this->logger = $logManager;
162 110
        $this->psr7Client = $psr7Client ?? GeneralUtility::getContainer()->get(ClientInterface::class);
163 110
        $this->requestFactory = $requestFactory ?? GeneralUtility::getContainer()->get(RequestFactoryInterface::class);
164 110
        $this->streamFactory = $streamFactory ?? GeneralUtility::getContainer()->get(StreamFactoryInterface::class);
165 110
        $this->eventDispatcher = $eventDispatcher ?? GeneralUtility::getContainer()->get(EventDispatcherInterface::class);
166 110
    }
167
168
    /**
169
     * @param string $key
170
     * @return Node
171
     */
172 105
    public function getNode(string $key): Node
173
    {
174 105
        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 53
    public function getReadService(): SolrReadService
205
    {
206 53
        if ($this->readService === null) {
207 53
            $this->readService = $this->buildReadService();
208
        }
209
210 53
        return $this->readService;
211
    }
212
213
    /**
214
     * @return SolrReadService
215
     * @noinspection PhpIncompatibleReturnTypeInspection
216
     */
217 53
    protected function buildReadService(): SolrReadService
218
    {
219 53
        $endpointKey = 'read';
220 53
        $client = $this->getClient($endpointKey);
221 53
        $this->initializeClient($client, $endpointKey);
222 53
        return GeneralUtility::makeInstance(SolrReadService::class, $client);
223
    }
224
225
    /**
226
     * @return SolrWriteService
227
     */
228 94
    public function getWriteService(): SolrWriteService
229
    {
230 94
        if ($this->writeService === null) {
231 94
            $this->writeService = $this->buildWriteService();
232
        }
233
234 94
        return $this->writeService;
235
    }
236
237
    /**
238
     * @return SolrWriteService
239
     * @noinspection PhpIncompatibleReturnTypeInspection
240
     */
241 94
    protected function buildWriteService(): SolrWriteService
242
    {
243 94
        $endpointKey = 'write';
244 94
        $client = $this->getClient($endpointKey);
245 94
        $this->initializeClient($client, $endpointKey);
246 94
        return GeneralUtility::makeInstance(SolrWriteService::class, $client);
247
    }
248
249
    /**
250
     * @param Client $client
251
     * @param string $endpointKey
252
     * @return Client
253
     */
254 101
    protected function initializeClient(Client $client, string $endpointKey): Client
255
    {
256 101
        if (trim($this->getNode($endpointKey)->getUsername()) === '') {
257 101
            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 101
    protected function getClient(string $endpointKey): Client
284
    {
285 101
        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 101
        $adapter = new Psr18Adapter($this->psr7Client, $this->requestFactory, $this->streamFactory);
290
291 101
        $client = new Client($adapter, $this->eventDispatcher);
292 101
        $client->getPlugin('postbigrequest');
293 101
        $client->clearEndpoints();
294
295 101
        $newEndpointOptions = $this->getNode($endpointKey)->getSolariumClientOptions();
296 101
        $newEndpointOptions['key'] = $endpointKey;
297 101
        $client->createEndpoint($newEndpointOptions, true);
298
299 101
        $this->clients[$endpointKey] = $client;
300 101
        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