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

ProvidingProgressBar::onFinishedProviding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 3
Bugs 1 Features 2
Metric Value
c 3
b 1
f 2
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace GBProd\ElasticsearchDataProviderBundle\Command;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
use Symfony\Component\Console\Helper\ProgressBar;
7
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8
use GBProd\ElasticsearchDataProviderBundle\Event\HasStartedHandling;
9
use GBProd\ElasticsearchDataProviderBundle\Event\HasStartedProviding;
10
use GBProd\ElasticsearchDataProviderBundle\Event\HasFinishedProviding;
11
use GBProd\ElasticsearchDataProviderBundle\Event\HasIndexedDocument;
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 ProgressBar
29
     *
30
     * public for test...
31
     */
32
    public $progressBar;
33
34
    /**
35
     * @param EventDispatcherInterface $dispatcher
36
     * @param OutputInterface          $output
37
     */
38 6
    public function __construct(
39
        EventDispatcherInterface $dispatcher,
40
        OutputInterface $output
41
    ) {
42 6
        $this->output = $output;
43
44 6
        $dispatcher->addListener(
45 6
            'elasticsearch.has_started_handling',
46 6
            [$this, 'onStartedHandling']
47 6
        );
48
49 6
        $dispatcher->addListener(
50 6
            'elasticsearch.has_started_providing',
51 6
            [$this, 'onStartedProviding']
52 6
        );
53
54 6
        $dispatcher->addListener(
55 6
            'elasticsearch.has_finished_providing',
56 6
            [$this, 'onFinishedProviding']
57 6
        );
58
59 6
        $dispatcher->addListener(
60 6
            'elasticsearch.has_indexed_document',
61 6
            [$this, 'onIndexedDocument']
62 6
        );
63 6
    }
64
65 1
    public function onStartedHandling(HasStartedHandling $event)
66
    {
67 1
        $this->output->writeln(
68 1
            sprintf(
69 1
                '<info>Start running <comment>%d</comment> providers</info>',
70 1
                count($event->getEntries())
71 1
            )
72 1
        );
73 1
    }
74
75 2
    public function onStartedProviding(HasStartedProviding $event)
76
    {
77 2
        $this->output->writeln(sprintf(
78 2
            '<info> - Running <comment>%s</comment> provider into <comment>%s/%s</comment></info>',
79 2
            get_class($event->getEntry()->getProvider()),
80 2
            $event->getEntry()->getIndex(),
81 2
            $event->getEntry()->getType()
82 2
        ));
83
84 2
        $count = $event->getEntry()->getProvider()->count();
85 2
        if (null !== $count) {
86 1
            $this->progressBar = new ProgressBar($this->output, $count);
87 1
            $this->progressBar->setFormat(self::PROGRESS_BAR_TEMPLATE);
88 1
        }
89 2
    }
90
91 1
    public function onIndexedDocument(HasIndexedDocument $event)
92
    {
93 1
        if ($this->progressBar) {
94 1
            $this->progressBar->advance();
95 1
        }
96 1
    }
97
98 1
    public function onFinishedProviding(HasFinishedProviding $event)
99
    {
100 1
        $this->progressBar->finish();
101 1
        $this->output->writeln('');
102 1
        $this->progressBar = null;
103 1
    }
104
}
105