Completed
Push — master ( fe3dbd...6d023a )
by jean
03:47
created

ProgressBarProcessNotifier::onCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Darkilliant\ProcessBundle\ProcessNotifier;
4
5
use Darkilliant\ProcessBundle\Console\ProgressBar;
6
use Darkilliant\ProcessBundle\State\ProcessState;
7
use Darkilliant\ProcessBundle\Step\IterableStepInterface;
8
use Darkilliant\ProcessBundle\Step\StepInterface;
9
use Symfony\Component\Console\ConsoleEvents;
10
use Symfony\Component\Console\Event\ConsoleEvent;
11
use Symfony\Component\Console\Output\NullOutput;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
class ProgressBarProcessNotifier implements EventSubscriberInterface
15
{
16
    /** @var ProgressBar */
17
    private $progressBar;
18
19
    private $currentStep;
20
21 9
    public function __construct(ProgressBar $progressBar)
22
    {
23 9
        $this->progressBar = $progressBar;
24 9
        $this->progressBar->setOutput(new NullOutput());
25 9
    }
26
27 1
    public static function getSubscribedEvents()
28
    {
29
        return [
30 1
            ConsoleEvents::COMMAND => ['onCommand', 255],
31
        ];
32
    }
33
34 1
    public function onCommand(ConsoleEvent $event)
35
    {
36 1
        $this->progressBar->setOutput($event->getOutput());
37 1
    }
38
39 5
    public function onStartProcess(ProcessState $state, StepInterface $step)
40
    {
41 5
        if (!$step instanceof IterableStepInterface) {
42 1
            return;
43
        }
44
45 4
        $count = $step->count($state);
46
47 4
        if (!$count || !$state->getOptions()['progress_bar']) {
48 1
            return;
49
        }
50
51 3
        $this->progressBar->create($count, get_class($step));
52 3
        $this->currentStep = $step;
53 3
    }
54
55 2
    public function onUpdateProcess(ProcessState $state, StepInterface $step)
0 ignored issues
show
Unused Code introduced by
The parameter $step is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
    public function onUpdateProcess(ProcessState $state, /** @scrutinizer ignore-unused */ StepInterface $step)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
    {
57 2
        if (!$this->currentStep) {
58 1
            return;
59
        }
60 1
        $this->progressBar->setProgress($this->currentStep->getProgress($state));
61 1
    }
62
63 2
    public function onEndProcess(ProcessState $state, StepInterface $step)
0 ignored issues
show
Unused Code introduced by
The parameter $step is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

63
    public function onEndProcess(ProcessState $state, /** @scrutinizer ignore-unused */ StepInterface $step)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $state is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

63
    public function onEndProcess(/** @scrutinizer ignore-unused */ ProcessState $state, StepInterface $step)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65 2
        if (!$this->currentStep) {
66 1
            return;
67
        }
68
69 1
        $this->currentStep = null;
70 1
        $this->progressBar->finish();
71 1
    }
72
}
73