Completed
Push — master ( 1b53d6...5badca )
by GBProd
02:44
created

ProvidingProgressBar::onIndexedDocument()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 6
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\HasIndexedDocument;
11
12
/**
13
 * Progress bar for providing
14
 * 
15
 * @author gbprod <[email protected]>
16
 */
17
class ProvidingProgressBar
18
{
19
    /**
20
     * @var OutputInterface
21
     */
22
    private $output;
23
    
24
    /**
25
     * @var ProgressBar
26
     */
27
    private $progressBar;
28
    
29
    /**
30
     * @param EventDispatcherInterface $dispatcher
31
     * @param OutputInterface          $output
32
     */
33 1
    public function __construct(
34
        EventDispatcherInterface $dispatcher, 
35
        OutputInterface $output
0 ignored issues
show
Unused Code introduced by
The parameter $output 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...
36
    ) {
37 1
        $dispatcher->addListener(
38 1
            'elasticsearch.has_started_handling',
39 1
            [$this, 'onStartedHandling']
40 1
        );
41
42 1
        $dispatcher->addListener(
43 1
            'elasticsearch.has_started_providing',
44 1
            [$this, 'onStartedProviding']
45 1
        );
46
        
47 1
        $dispatcher->addListener(
48 1
            'elasticsearch.has_indexed_document',
49 1
            [$this, 'onIndexedDocument']
50 1
        );
51 1
    }
52
    
53
    public function onStartedHandling(HasStartedHandling $event) 
54
    {
55
        $this->output->writeln(sprintf(
56
            '<info>Start running <comment>%d</comment> providers</info>', 
57
            count($event->getEntries())
58
        ));
59
    }
60
    
61
    public function onStartedProviding(HasStartedProviding $event)
62
    {
63
        $this->output->writeln(sprintf(
64
            '<info>Start running <comment>%s</comment> provider</info>',
65
            get_class($event->getEntry()->getProvider())
66
        ));
67
        
68
        if (null !== $this->progressBar) {
69
            $progressBar->finish();
0 ignored issues
show
Bug introduced by
The variable $progressBar does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
70
        }
71
        
72
        $this->progressBar = null;
73
        $count = $event->getEntry()->getProvider()->count();
74
        if (null !== $count) {
75
            $this->progressBar = new ProgressBar($output, $count);
0 ignored issues
show
Bug introduced by
The variable $output does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
76
        } 
77
    }
78
79
    public function onIndexedDocument(HasIndexedDocument $event) 
1 ignored issue
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...
80
    {
81
        if ($this->progressBar) {
82
            $this->progressBar->advance();
83
        }
84
    }
85
}
86