Completed
Push — master ( 628a17...692b18 )
by GBProd
02:20
created

Command/ProvidingProgressBar.php (2 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 ProgressBar
29
     *
30
     * public for test purpose
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_provided_document',
56 6
            [$this, 'onProvidedDocument']
57 6
        );
58
59 6
        $dispatcher->addListener(
60 6
            'elasticsearch.has_finished_providing',
61 6
            [$this, 'onFinishedProviding']
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 onProvidedDocument(HasProvidedDocument $event)
1 ignored issue
show
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...
92
    {
93 1
        if ($this->progressBar) {
94 1
            $this->progressBar->advance();
95 1
        }
96 1
    }
97
98 1
    public function onFinishedProviding(HasFinishedProviding $event)
1 ignored issue
show
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...
99
    {
100 1
        $this->progressBar->finish();
101 1
        $this->output->writeln('');
102 1
        $this->progressBar = null;
103 1
    }
104
}
105