Completed
Push — master ( 3b88e7...c64881 )
by GBProd
02:12
created

ProvideCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 78.79%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 7
c 4
b 1
f 1
lcom 1
cbo 5
dl 0
loc 100
ccs 52
cts 66
cp 0.7879
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 24 1
A execute() 0 22 3
A getClient() 0 18 2
B initializeProgress() 0 24 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\InputOption;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\DependencyInjection\Container;
11
12
/**
13
 * Command to run providing
14
 * 
15
 * @author gbprod <[email protected]>
16
 */
17
class ProvideCommand extends ContainerAwareCommand
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 1
    protected function configure()
23
    {
24 1
        $this
25 1
            ->setName('elasticsearch:provide')
26 1
            ->setDescription('Provide data to Elasticsearch')
27 1
            ->addArgument(
28 1
                'index',
29 1
                InputArgument::OPTIONAL,
30
                'Index to provide'
31 1
            )
32 1
            ->addArgument(
33 1
                'type',
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
        $this->initializeProgress($output);
69
        
70 1
        $handler->handle($client, $index, $type);
71 1
    }
72
    
73 1
    private function getClient($clientName)
74
    {
75 1
        $client = $this->getContainer()
76 1
            ->get(sprintf(
77 1
                'm6web_elasticsearch.client.%s',
78
                $clientName
79 1
            ))
80 1
        ;
81
        
82 1
        if (!$client) {
83
            throw new \InvalidArgumentException(sprintf(
84
                'No client "%s" found',
85
                $clientName
86
            ));
87
        }
88
        
89 1
        return $client;
90
    }
91
    
92 1
    private function initializeProgress(OutputInterface $output)
93
    {
94 1
        $dispatcher = $this->getContainer()->get('event_dispatcher');
95
        
96 1
        $dispatcher->addListener(
97 1
            'elasticsearch.has_started_handling',
98
            function (HasStartedHandling $event) use ($output) {
99
                $output->writeln(sprintf(
100
                    '<info>Start running <comment>%d</comment> providers</info', 
101
                    count($event->getEntries())
102
                ));
103
            }
104 1
        );
105
106 1
        $dispatcher->addListener(
107 1
            'elasticsearch.has_started_providing',
108
            function (HasStartedProviding $event) use ($output) {
109
                $output->writeln(sprintf(
110
                    '<info>Start running <comment>%s</comment> provider</info',
111
                    get_class($event->getEntry()->getProvider())
112
                ));
113
            }
114 1
        );
115 1
    }
116
}
117