Completed
Pull Request — master (#17)
by Ambrosini
02:01
created

ProvidingProgressBar   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 7
dl 0
loc 86
ccs 32
cts 44
cp 0.7272
rs 10
c 0
b 0
f 0

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 3
A onProvidedDocument() 0 6 2
A onFinishedProviding() 0 9 2
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 && $count > 0) {
82 1
            $this->progressBar = new ProgressBar($this->output, $count);
83 1
            $this->progressBar->setFormat(self::PROGRESS_BAR_TEMPLATE);
84 1
        }
85 2
    }
86
87
    public function onProvidedDocument(HasProvidedDocument $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
    {
89
        if ($this->progressBar) {
90
            $this->progressBar->advance();
91
        }
92
    }
93
94
    public function onFinishedProviding(HasFinishedProviding $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
    {
96
        if ($this->progressBar) {
97
            $this->progressBar->finish();
98
        }
99
100
        $this->output->writeln('');
101
        $this->progressBar = null;
102
    }
103
}
104