Completed
Push — master ( c24a07...3405a4 )
by Kévin
18s queued 10s
created

ElasticsearchClientPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 15
dl 0
loc 29
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 24 5
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler;
15
16
use Elasticsearch\ClientBuilder;
17
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
/**
22
 * Creates the Elasticsearch client.
23
 *
24
 * @author Baptiste Meyer <[email protected]>
25
 */
26
final class ElasticsearchClientPass implements CompilerPassInterface
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function process(ContainerBuilder $container)
32
    {
33
        if (!$container->getParameter('api_platform.elasticsearch.enabled')) {
34
            return;
35
        }
36
37
        $clientConfiguration = [];
38
39
        if ($hosts = $container->getParameter('api_platform.elasticsearch.hosts')) {
40
            $clientConfiguration['hosts'] = $hosts;
41
        }
42
43
        if ($container->has('logger')) {
44
            $clientConfiguration['logger'] = new Reference('logger');
45
            $clientConfiguration['tracer'] = new Reference('logger');
46
        }
47
48
        $clientDefinition = $container->getDefinition('api_platform.elasticsearch.client');
49
50
        if (!$clientConfiguration) {
51
            $clientDefinition->setFactory([ClientBuilder::class, 'build']);
52
        } else {
53
            $clientDefinition->setFactory([ClientBuilder::class, 'fromConfig']);
54
            $clientDefinition->setArguments([$clientConfiguration]);
55
        }
56
    }
57
}
58