Completed
Push — master ( c6e312...e2aeec )
by GBProd
02:14
created

ProvideCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 93.48%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
c 2
b 0
f 1
lcom 0
cbo 5
dl 0
loc 74
ccs 43
cts 46
cp 0.9348
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 25 1
A execute() 0 20 3
A getClient() 0 18 2
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
                null,
34 1
                InputArgument::OPTIONAL,
35
                'Type to provide'
36 1
            )
37 1
            ->addOption(
38 1
                'client',
39 1
                null,
40 1
                InputOption::VALUE_REQUIRED,
41 1
                'Client to use (if not default)',
42
                'default'
43 1
            )
44
        ;
45 1
    }
46
    
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52 1
        $handler = $this->getContainer()
53 1
            ->get('gbprod.elasticsearch_dataprovider.handler')
54 1
        ;
55
        
56 1
        $client = $this->getClient($input->getOption('client'));
57
        
58 1
        $index = $input->getArgument('index');
59 1
        $type  = $input->getArgument('type');
60
61 1
        $output->writeln(sprintf(
62 1
            '<info>Providing <comment>%s/%s</comment> for client <comment>%s</comment>...</info>',
63 1
            $index ?: '*',
64 1
            $type ?: '*',
65 1
            $input->getOption('client')
66 1
        ));
67
        
68 1
        $handler->handle($client, $index, $type);
69 1
    }
70
    
71 1
    private function getClient($clientName)
72
    {
73 1
        $client = $this->getContainer()
74 1
            ->get(sprintf(
75 1
                'm6web_elasticsearch.client.%s',
76
                $clientName
77 1
            ))
78 1
        ;
79
        
80 1
        if (!$client) {
81
            throw new \InvalidArgumentException(sprintf(
82
                'No client "%s" found',
83
                $clientName
84
            ));
85
        }
86
        
87 1
        return $client;
88
    }
89
}
90