Completed
Push — master ( 2e17a9...fe4f4a )
by GBProd
02:14
created

ProvideCommand::getClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0811

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 8
cts 11
cp 0.7272
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
crap 2.0811
1
<?php
2
3
namespace GBProd\ElasticsearchDataProviderBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Command to run providing
13
 * 
14
 * @author gbprod <[email protected]>
15
 */
16
class ProvideCommand extends ContainerAwareCommand
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 1
    protected function configure()
22
    {
23 1
        $this
24 1
            ->setName('elasticsearch:provide')
25 1
            ->setDescription('Provide data to Elasticsearch')
26 1
            ->addArgument(
27 1
                'index',
28 1
                InputArgument::OPTIONAL,
29
                'Index to provide'
30 1
            )
31 1
            ->addArgument(
32 1
                'type',
33 1
                InputArgument::OPTIONAL,
34
                'Type to provide'
35 1
            )
36 1
            ->addOption(
37 1
                'client',
38 1
                null,
39 1
                InputOption::VALUE_REQUIRED,
40 1
                'Client to use (if not default)',
41
                'default'
42 1
            )
43
        ;
44 1
    }
45
    
46
    /**
47
     * {@inheritdoc}
48
     */
49 1
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51 1
        $handler = $this->getContainer()
52 1
            ->get('gbprod.elasticsearch_dataprovider.handler')
53 1
        ;
54
        
55 1
        $client = $this->getClient($input->getOption('client'));
56
        
57 1
        $index = $input->getArgument('index');
58 1
        $type  = $input->getArgument('type');
59
60 1
        $output->writeln(sprintf(
61 1
            '<info>Providing <comment>%s/%s</comment> for client <comment>%s</comment>...</info>',
62 1
            $index ?: '*',
63 1
            $type ?: '*',
64 1
            $input->getOption('client')
65 1
        ));
66
        
67 1
        $handler->handle($client, $index, $type);
68 1
    }
69
    
70 1
    private function getClient($clientName)
71
    {
72 1
        $client = $this->getContainer()
73 1
            ->get(sprintf(
74 1
                'm6web_elasticsearch.client.%s',
75
                $clientName
76 1
            ))
77 1
        ;
78
        
79 1
        if (!$client) {
80
            throw new \InvalidArgumentException(sprintf(
81
                'No client "%s" found',
82
                $clientName
83
            ));
84
        }
85
        
86 1
        return $client;
87
    }
88
}
89