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

ElasticsearchAwareCommand::getClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.0491

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 10
cts 13
cp 0.7692
rs 9.0856
cc 2
eloc 13
nc 2
nop 1
crap 2.0491
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