Completed
Push — master ( d72a06...ba0ad0 )
by GBProd
04:38
created

ElasticsearchAwareCommand   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 76.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 33
ccs 10
cts 13
cp 0.7692
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getClient() 0 23 2
1
<?php
2
3
namespace GBProd\ElasticsearchExtraBundle\Command;
4
5
use Elasticsearch\Client;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
9
/**
10
 * Command tha use elasticsearch client from container
11
 *
12
 * @author gbprod <[email protected]>
13
 */
14
abstract class ElasticsearchAwareCommand extends ContainerAwareCommand
15
{
16
    /**
17
     * Get elasticsearch client from his name
18
     *
19
     * @param string $clientName
20
     *
21
     * @return Client
22
     */
23
    public function getClient($clientName)
24
    {
25 1
        $clientServiceName = sprintf(
26 1
            'm6web_elasticsearch.client.%s',
27
            $clientName
28 1
        );
29
30 1
        $client = $this->getContainer()
31 1
            ->get(
32 1
                $clientServiceName,
33
                ContainerInterface::NULL_ON_INVALID_REFERENCE
34 1
            )
35 1
        ;
36
37 1
        if (!$client) {
38
            throw new \InvalidArgumentException(sprintf(
39
                'No client "%s" found',
40
                $clientName
41
            ));
42
        }
43
44 1
        return $client;
45
    }
46
}
47