1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Micro framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Stanislau Komar <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Micro\Plugin\Elastic\Client\Factory; |
15
|
|
|
|
16
|
|
|
use Elastic\Elasticsearch\ClientBuilder; |
17
|
|
|
use Elastic\Elasticsearch\ClientInterface; |
18
|
|
|
use Micro\Plugin\Elastic\Configuration\ElasticPluginConfigurationInterface; |
19
|
|
|
use Micro\Plugin\Logger\Facade\LoggerFacadeInterface; |
20
|
|
|
|
21
|
|
|
class ElasticClientFactory implements ElasticClientFactoryInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param ElasticPluginConfigurationInterface $pluginConfiguration |
25
|
|
|
* @param LoggerFacadeInterface $loggerFacade |
26
|
|
|
*/ |
27
|
2 |
|
public function __construct( |
28
|
|
|
private readonly ElasticPluginConfigurationInterface $pluginConfiguration, |
29
|
|
|
private readonly LoggerFacadeInterface $loggerFacade |
30
|
|
|
) { |
31
|
2 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritDoc} |
35
|
|
|
*/ |
36
|
1 |
|
public function create(string $clientName): ClientInterface |
37
|
|
|
{ |
38
|
1 |
|
$clientConfiguration = $this->pluginConfiguration->getClientConfiguration($clientName); |
39
|
1 |
|
$builder = ClientBuilder::create()->setHosts($clientConfiguration->getHosts()); |
40
|
|
|
|
41
|
1 |
|
$basicLogin = $clientConfiguration->getBasicAuthLogin(); |
42
|
1 |
|
if (!empty($basicLogin)) { |
43
|
1 |
|
$builder->setBasicAuthentication($basicLogin, $clientConfiguration->getBasicAuthPassword()); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
$apiKey = $clientConfiguration->getApiKey(); |
47
|
1 |
|
if ($apiKey) { |
48
|
|
|
$builder->setApiKey($apiKey); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
$elasticCloudId = $clientConfiguration->getElasticCloudId(); |
52
|
1 |
|
if ($elasticCloudId) { |
53
|
|
|
$builder->setElasticCloudId($elasticCloudId); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$logger = $this->loggerFacade->getLogger($clientConfiguration->getLoggerName()); |
57
|
|
|
|
58
|
1 |
|
$builder->setLogger($logger); |
59
|
1 |
|
$builder->setRetries($clientConfiguration->getRetries()); |
60
|
1 |
|
$builder->setSSLVerification($clientConfiguration->getSslVerification()); |
61
|
|
|
|
62
|
1 |
|
$caBundle = $clientConfiguration->getCABundle(); |
63
|
1 |
|
if ($caBundle) { |
64
|
1 |
|
$builder->setCABundle($caBundle); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
$sslKey = $clientConfiguration->getSslKey(); |
68
|
1 |
|
if ($sslKey) { |
69
|
1 |
|
$builder->setSSLKey($sslKey, $clientConfiguration->getSslKeyPassword()); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return $builder->build(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|