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

ProvideCommand::getClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

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