Completed
Push — master ( c56780...0a78d5 )
by GBProd
02:21
created

ProvideCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
crap 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('index', InputArgument::OPTIONAL, 'Index to provide')
29 2
            ->addArgument('type', InputArgument::OPTIONAL, 'Type to provide')
30 2
            ->addOption('client', null, InputOption::VALUE_REQUIRED, 'Client to use (if not default)', 'default')
31
        ;
32 2
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39 2
        $handler = $this->getContainer()
40 2
            ->get('gbprod.elasticsearch_dataprovider.handler')
41 2
        ;
42
43 2
        $client = $this->getClient($input->getOption('client'));
44
45 1
        $index = $input->getArgument('index');
46 1
        $type  = $input->getArgument('type');
47
48 1
        $output->writeln(sprintf(
49 1
            '<info>Providing <comment>%s/%s</comment> for client <comment>%s</comment></info>',
50 1
            $index ?: '*',
51 1
            $type ?: '*',
52 1
            $input->getOption('client')
53 1
        ));
54
55 1
        $this->initializeProgress($output);
56
57 1
        $handler->handle($client, $index, $type);
58 1
    }
59
60 2
    private function getClient($clientName)
61
    {
62 2
        $clientServiceName = sprintf(
63 2
            'm6web_elasticsearch.client.%s',
64
            $clientName
65 2
        );
66
67 2
        $client = $this->getContainer()
68 2
            ->get(
69 2
                $clientServiceName,
70
                ContainerInterface::NULL_ON_INVALID_REFERENCE
71 2
            )
72 2
        ;
73
74 2
        if (!$client) {
75 1
            throw new \InvalidArgumentException(sprintf(
76 1
                'No client "%s" found',
77
                $clientName
78 1
            ));
79
        }
80
81 1
        return $client;
82
    }
83
84 1
    private function initializeProgress(OutputInterface $output)
85
    {
86 1
        $dispatcher = $this->getContainer()->get('event_dispatcher');
87
88 1
        new ProvidingProgressBar($dispatcher, $output);
89 1
    }
90
}
91