Completed
Push — master ( 692b18...8a2af4 )
by GBProd
04:49 queued 01:44
created

ProvidingProgressBar   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 100%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A listen() 0 4 1
A onStartedHandling() 0 9 1
A onStartedProviding() 0 15 2
A onProvidedDocument() 0 6 2
A onFinishedProviding() 0 6 1
1
<?php
2
3
namespace GBProd\ElasticaProviderBundle\Command;
4
5
use GBProd\ElasticaProviderBundle\Event\HasFinishedProviding;
6
use GBProd\ElasticaProviderBundle\Event\HasProvidedDocument;
7
use GBProd\ElasticaProviderBundle\Event\HasStartedHandling;
8
use GBProd\ElasticaProviderBundle\Event\HasStartedProviding;
9
use Symfony\Component\Console\Helper\ProgressBar;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
13
/**
14
 * Progress bar for providing
15
 *
16
 * @author gbprod <[email protected]>
17
 */
18
class ProvidingProgressBar
19
{
20
    const PROGRESS_BAR_TEMPLATE = ' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%';
21
22
    /**
23
     * @var OutputInterface
24
     */
25
    private $output;
26
    
27
    /**
28
     * @var EventDispatcherInterface
29
     */
30
    private $dispatcher;
31
32
    /**
33
     * @var ProgressBar
34
     *
35
     * public for test purpose
36
     */
37
    public $progressBar;
38
39
    /**
40
     * @param EventDispatcherInterface $dispatcher
41
     * @param OutputInterface          $output
42
     */
43 6
    public function __construct(
44
        EventDispatcherInterface $dispatcher,
45
        OutputInterface $output
46
    ) {
47 6
        $this->output     = $output;
48 6
        $this->dispatcher = $dispatcher;
49
        
50 6
        $this->listen('elasticsearch.has_started_handling', 'onStartedHandling');
51 6
        $this->listen('elasticsearch.has_started_providing', 'onStartedProviding');
52 6
        $this->listen('elasticsearch.has_provided_document', 'onProvidedDocument');
53 6
        $this->listen('elasticsearch.has_finished_providing', 'onFinishedProviding');
54 6
    }
55
    
56 6
    private function listen($eventName, $function)
57
    {
58 6
        $this->dispatcher->addListener($eventName, [$this, $function]);
59 6
    }
60
61 1
    public function onStartedHandling(HasStartedHandling $event)
62
    {
63 1
        $this->output->writeln(
64 1
            sprintf(
65 1
                '<info>Start running <comment>%d</comment> providers</info>',
66 1
                count($event->getEntries())
67 1
            )
68 1
        );
69 1
    }
70
71 2
    public function onStartedProviding(HasStartedProviding $event)
72
    {
73 2
        $this->output->writeln(sprintf(
74 2
            '<info> - Running <comment>%s</comment> provider into <comment>%s/%s</comment></info>',
75 2
            get_class($event->getEntry()->getProvider()),
76 2
            $event->getEntry()->getIndex(),
77 2
            $event->getEntry()->getType()
78 2
        ));
79
80 2
        $count = $event->getEntry()->getProvider()->count();
81 2
        if (null !== $count) {
82 1
            $this->progressBar = new ProgressBar($this->output, $count);
83 1
            $this->progressBar->setFormat(self::PROGRESS_BAR_TEMPLATE);
84 1
        }
85 2
    }
86
87 1
    public function onProvidedDocument(HasProvidedDocument $event)
88
    {
89 1
        if ($this->progressBar) {
90 1
            $this->progressBar->advance();
91 1
        }
92 1
    }
93
94 1
    public function onFinishedProviding(HasFinishedProviding $event)
95
    {
96 1
        $this->progressBar->finish();
97 1
        $this->output->writeln('');
98 1
        $this->progressBar = null;
99 1
    }
100
}
101