Failed Conditions
Pull Request — 0.3 (#20)
by jean
06:00
created

ProgressBarProcessNotifier   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 86.11%

Importance

Changes 0
Metric Value
dl 0
loc 75
ccs 31
cts 36
cp 0.8611
rs 10
c 0
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A onStartProcess() 0 3 1
A getSubscribedEvents() 0 4 1
A onCommand() 0 3 1
A __construct() 0 4 1
A onUpdateIterableProcess() 0 11 3
A onExecutedProcess() 0 2 1
A onEndProcess() 0 11 3
A onStartIterableProcess() 0 17 4
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
/**
15
 * @internal
16
 * Class ProgressBarProcessNotifier
17
 */
18
class ProgressBarProcessNotifier implements EventSubscriberInterface, ProcessNotifierInterface
19
{
20
    /** @var ProgressBar */
21
    private $progressBar;
22
23 10
    public function __construct(ProgressBar $progressBar)
24
    {
25 10
        $this->progressBar = $progressBar;
26 10
        $this->progressBar->setOutput(new NullOutput());
27 10
    }
28
29 1
    public static function getSubscribedEvents()
30
    {
31
        return [
32 1
            ConsoleEvents::COMMAND => ['onCommand', 255],
33
        ];
34
    }
35
36 1
    public function onCommand(ConsoleEvent $event)
37
    {
38 1
        $this->progressBar->setOutput($event->getOutput());
39 1
    }
40
41 4
    public function onStartProcess(ProcessState $state, StepInterface $step)
42
    {
43 4
        return;
44
    }
45
46 2
    public function onStartIterableProcess(ProcessState $state, StepInterface $step)
47
    {
48 2
        if (!$step instanceof IterableStepInterface) {
49
            return null;
50
        }
51
52 2
        if (!$state->getOptions()['progress_bar']) {
53
            return null;
54
        }
55
56 2
        $count = $step->count($state);
57
58 2
        if (!$count) {
59 1
            return null;
60
        }
61
62 1
        $this->progressBar->create($count, get_class($step));
63 1
    }
64
65 2
    public function onUpdateIterableProcess(ProcessState $state, StepInterface $step)
66
    {
67 2
        if (!$step instanceof IterableStepInterface) {
68
            return null;
69
        }
70
71 2
        if (!$state->getOptions()['progress_bar']) {
72 1
            return;
73
        }
74
75 1
        $this->progressBar->setProgress($step->getProgress($state));
76 1
    }
77
78 2
    public function onEndProcess(ProcessState $state, StepInterface $step)
79
    {
80 2
        if (!$step instanceof IterableStepInterface) {
81
            return null;
82
        }
83
84 2
        if (!$state->getOptions()['progress_bar']) {
85 1
            return;
86
        }
87
88 1
        $this->progressBar->finish();
89 1
    }
90
91
    public function onExecutedProcess(ProcessState $state, StepInterface $step)
92
    {
93
    }
94
}
95