ProvidingProgressBar   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 100%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A listen() 0 4 1
A onStartedProviding() 0 15 3
A onProvidedDocument() 0 6 2
A onFinishedProviding() 0 9 2
A __construct() 0 12 1
A onStartedHandling() 0 9 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 8
    public function __construct(
44
        EventDispatcherInterface $dispatcher,
45
        OutputInterface $output
46
    ) {
47 8
        $this->output     = $output;
48 8
        $this->dispatcher = $dispatcher;
49
50 8
        $this->listen('elasticsearch.has_started_handling', 'onStartedHandling');
51 8
        $this->listen('elasticsearch.has_started_providing', 'onStartedProviding');
52 8
        $this->listen('elasticsearch.has_provided_document', 'onProvidedDocument');
53 8
        $this->listen('elasticsearch.has_finished_providing', 'onFinishedProviding');
54 8
    }
55
56 8
    private function listen($eventName, $function)
57
    {
58 8
        $this->dispatcher->addListener($eventName, [$this, $function]);
59 8
    }
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 4
    public function onStartedProviding(HasStartedProviding $event)
72
    {
73 4
        $this->output->writeln(sprintf(
74 4
            '<info> - Running <comment>%s</comment> provider into <comment>%s/%s</comment></info>',
75 4
            get_class($event->getEntry()->getProvider()),
76 4
            $event->getEntry()->getIndex(),
77 4
            $event->getEntry()->getType()
78 4
        ));
79
80 4
        $count = $event->getEntry()->getProvider()->count();
81 4
        if (null !== $count && $count > 0) {
82 3
            $this->progressBar = new ProgressBar($this->output, $count);
83 3
            $this->progressBar->setFormat(self::PROGRESS_BAR_TEMPLATE);
84 3
        }
85 4
    }
86
87 1
    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 1
        if ($this->progressBar) {
90 1
            $this->progressBar->advance();
91 1
        }
92 1
    }
93
94 1
    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 1
        if ($this->progressBar) {
97 1
            $this->progressBar->finish();
98 1
        }
99
100 1
        $this->output->writeln('');
101 1
        $this->progressBar = null;
102 1
    }
103
}
104