Passed
Pull Request — master (#2985)
by
unknown
34:31
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 76
    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 76
        $this->nodes['read'] = $readNode;
155 76
        $this->nodes['write'] = $writeNode;
156 76
        $this->nodes['admin'] = $writeNode;
157 76
        $this->configuration = $configuration ?? Util::getSolrConfiguration();
158 76
        $this->synonymParser = $synonymParser;
159 76
        $this->stopWordParser = $stopWordParser;
160 76
        $this->schemaParser = $schemaParser;
161 76
        $this->logger = $logManager;
162 76
        $this->psr7Client = $psr7Client ?? GeneralUtility::getContainer()->get(ClientInterface::class);
163 76
        $this->requestFactory = $requestFactory ?? GeneralUtility::getContainer()->get(RequestFactoryInterface::class);
164 76
        $this->streamFactory = $streamFactory ?? GeneralUtility::getContainer()->get(StreamFactoryInterface::class);
165 76
        $this->eventDispatcher = $eventDispatcher ?? GeneralUtility::getContainer()->get(EventDispatcherInterface::class);
166 76
    }
167
168
    /**
169
     * @param string $key
170
     * @return Node
171
     */
172 73
    public function getNode(string $key): Node
173
    {
174 73
        return $this->nodes[$key];
175
    }
176
177
    /**
178
     * @return SolrAdminService
179
     */
180 5
    public function getAdminService(): SolrAdminService
181
    {
182 5
        if ($this->adminService === null) {
183 5
            $this->adminService = $this->buildAdminService();
184
        }
185
186 5
        return $this->adminService;
187
    }
188
189
    /**
190
     * @return SolrAdminService
191
     * @noinspection PhpIncompatibleReturnTypeInspection
192
     */
193 5
    protected function buildAdminService(): SolrAdminService
194
    {
195 5
        $endpointKey = 'admin';
196 5
        $client = $this->getClient($endpointKey);
197 5
        $this->initializeClient($client, $endpointKey);
198 5
        return GeneralUtility::makeInstance(SolrAdminService::class, $client, $this->configuration, $this->logger, $this->synonymParser, $this->stopWordParser, $this->schemaParser);
199
    }
200
201
    /**
202
     * @return SolrReadService
203
     */
204 20
    public function getReadService(): SolrReadService
205
    {
206 20
        if ($this->readService === null) {
207 20
            $this->readService = $this->buildReadService();
208
        }
209
210 20
        return $this->readService;
211
    }
212
213
    /**
214
     * @return SolrReadService
215
     * @noinspection PhpIncompatibleReturnTypeInspection
216
     */
217 20
    protected function buildReadService(): SolrReadService
218
    {
219 20
        $endpointKey = 'read';
220 20
        $client = $this->getClient($endpointKey);
221 20
        $this->initializeClient($client, $endpointKey);
222 20
        return GeneralUtility::makeInstance(SolrReadService::class, $client);
223
    }
224
225
    /**
226
     * @return SolrWriteService
227
     */
228 56
    public function getWriteService(): SolrWriteService
229
    {
230 56
        if ($this->writeService === null) {
231 56
            $this->writeService = $this->buildWriteService();
232
        }
233
234 56
        return $this->writeService;
235
    }
236
237
    /**
238
     * @return SolrWriteService
239
     * @noinspection PhpIncompatibleReturnTypeInspection
240
     */
241 56
    protected function buildWriteService(): SolrWriteService
242
    {
243 56
        $endpointKey = 'write';
244 56
        $client = $this->getClient($endpointKey);
245 56
        $this->initializeClient($client, $endpointKey);
246 56
        return GeneralUtility::makeInstance(SolrWriteService::class, $client);
247
    }
248
249
    /**
250
     * @param Client $client
251
     * @param string $endpointKey
252
     * @return Client
253
     */
254 68
    protected function initializeClient(Client $client, string $endpointKey): Client
255
    {
256 68
        if (trim($this->getNode($endpointKey)->getUsername()) === '') {
257 67
            return $client;
258
        }
259
260 1
        $username = $this->getNode($endpointKey)->getUsername();
261 1
        $password = $this->getNode($endpointKey)->getPassword();
262 1
        $this->setAuthenticationOnAllEndpoints($client, $username, $password);
263
264 1
        return $client;
265
    }
266
267
    /**
268
     * @param Client $client
269
     * @param string $username
270
     * @param string $password
271
     */
272 1
    protected function setAuthenticationOnAllEndpoints(Client $client, string $username, string $password)
273
    {
274 1
        foreach ($client->getEndpoints() as $endpoint) {
275 1
            $endpoint->setAuthentication($username, $password);
276
        }
277 1
    }
278
279
    /**
280
     * @param string $endpointKey
281
     * @return Client
282
     */
283 68
    protected function getClient(string $endpointKey): Client
284
    {
285 68
        if ($this->clients[$endpointKey]) {
286 2
            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 66
        $adapter = new Psr18Adapter($this->psr7Client, $this->requestFactory, $this->streamFactory);
290
291 66
        $client = new Client($adapter, $this->eventDispatcher);
292 66
        $client->getPlugin('postbigrequest');
293 66
        $client->clearEndpoints();
294
295 66
        $newEndpointOptions = $this->getNode($endpointKey)->getSolariumClientOptions();
296 66
        $newEndpointOptions['key'] = $endpointKey;
297 66
        $client->createEndpoint($newEndpointOptions, true);
298
299 66
        $this->clients[$endpointKey] = $client;
300 66
        return $client;
301
    }
302
303
    /**
304
     * @param Client $client
305
     * @param ?string $endpointKey
306
     */
307 2
    public function setClient(Client $client, ?string $endpointKey = 'read')
308
    {
309 2
        $this->clients[$endpointKey] = $client;
310 2
    }
311
}
312