Completed
Push — master ( b8b393...c56780 )
by GBProd
02:34
created

ProvideCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 1 Features 2
Metric Value
wmc 7
c 8
b 1
f 2
lcom 1
cbo 6
dl 0
loc 87
ccs 53
cts 53
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 24 1
A execute() 0 22 3
A getClient() 0 23 2
A initializeProgress() 0 6 1
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\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\DependencyInjection\Container;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
/**
14
 * Command to run providing
15
 *
16
 * @author gbprod <[email protected]>
17
 */
18
class ProvideCommand extends ContainerAwareCommand
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 2
    protected function configure()
24
    {
25 2
        $this
26 2
            ->setName('elasticsearch:provide')
27 2
            ->setDescription('Provide data to Elasticsearch')
28 2
            ->addArgument(
29 2
                'index',
30 2
                InputArgument::OPTIONAL,
31
                'Index to provide'
32 2
            )
33 2
            ->addArgument(
34 2
                'type',
35 2
                InputArgument::OPTIONAL,
36
                'Type to provide'
37 2
            )
38 2
            ->addOption(
39 2
                'client',
40 2
                null,
41 2
                InputOption::VALUE_REQUIRED,
42 2
                'Client to use (if not default)',
43
                'default'
44 2
            )
45
        ;
46 2
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 2
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53 2
        $handler = $this->getContainer()
54 2
            ->get('gbprod.elasticsearch_dataprovider.handler')
55 2
        ;
56
57 2
        $client = $this->getClient($input->getOption('client'));
58
59 1
        $index = $input->getArgument('index');
60 1
        $type  = $input->getArgument('type');
61
62 1
        $output->writeln(sprintf(
63 1
            '<info>Providing <comment>%s/%s</comment> for client <comment>%s</comment></info>',
64 1
            $index ?: '*',
65 1
            $type ?: '*',
66 1
            $input->getOption('client')
67 1
        ));
68
69 1
        $this->initializeProgress($output);
70
71 1
        $handler->handle($client, $index, $type);
72 1
    }
73
74 2
    private function getClient($clientName)
75
    {
76 2
        $clientServiceName = sprintf(
77 2
            'm6web_elasticsearch.client.%s',
78
            $clientName
79 2
        );
80
81 2
        $client = $this->getContainer()
82 2
            ->get(
83 2
                $clientServiceName,
84
                ContainerInterface::NULL_ON_INVALID_REFERENCE
85 2
            )
86 2
        ;
87
88 2
        if (!$client) {
89 1
            throw new \InvalidArgumentException(sprintf(
90 1
                'No client "%s" found',
91
                $clientName
92 1
            ));
93
        }
94
95 1
        return $client;
96
    }
97
98 1
    private function initializeProgress(OutputInterface $output)
99
    {
100 1
        $dispatcher = $this->getContainer()->get('event_dispatcher');
101
102 1
        new ProvidingProgressBar($dispatcher, $output);
103 1
    }
104
}
105