Completed
Push — master ( bdc6eb...b8b393 )
by GBProd
03:26
created

ProvidingProgressBar::onStartedHandling()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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