ProgressBarProcessNotifier   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 90
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0
wmc 17

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 4 1
A onCommand() 0 3 1
A __construct() 0 4 1
A onStartProcess() 0 3 1
A onUpdateIterableProcess() 0 11 3
A onExecutedProcess() 0 2 1
A onEndProcess() 0 11 3
A onStartIterableProcess() 0 21 4
A onFailedLoop() 0 3 1
A onSuccessLoop() 0 3 1
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\MonitorableStepInterface;
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 extends AbstractProcessNotifier implements EventSubscriberInterface
19
{
20
    /** @var ProgressBar */
21
    private $progressBar;
22
23 15
    public function __construct(ProgressBar $progressBar)
24
    {
25 15
        $this->progressBar = $progressBar;
26 15
        $this->progressBar->setOutput(new NullOutput());
27 15
    }
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 1
    public function onStartProcess(ProcessState $state, StepInterface $step)
42
    {
43 1
        return;
44
    }
45
46 7
    public function onStartIterableProcess(ProcessState $state, StepInterface $step)
47
    {
48 7
        if (!$step instanceof MonitorableStepInterface) {
49 2
            return null;
50
        }
51
52 5
        if (!$state->getOptions()['progress_bar']) {
53 1
            return null;
54
        }
55
56 4
        $count = $step->count($state);
57
58 4
        if (!$count) {
59 1
            return null;
60
        }
61
62 3
        $this->progressBar->create(
63 3
            $count,
64 3
            get_class($step),
65 3
            $state->getOption('progress_bar_min_item', 50),
66 3
            $state->getOption('progress_bar_max_memory', 30)
67
        );
68 3
    }
69
70 3
    public function onUpdateIterableProcess(ProcessState $state, StepInterface $step)
71
    {
72 3
        if (!$step instanceof MonitorableStepInterface) {
73 1
            return null;
74
        }
75
76 2
        if (!$state->getOptions()['progress_bar']) {
77 1
            return;
78
        }
79
80 1
        $this->progressBar->setProgress($step->getProgress($state));
81 1
    }
82
83 3
    public function onEndProcess(ProcessState $state, StepInterface $step)
84
    {
85 3
        if (!$step instanceof MonitorableStepInterface) {
86 1
            return null;
87
        }
88
89 2
        if (!$state->getOptions()['progress_bar']) {
90 1
            return;
91
        }
92
93 1
        $this->progressBar->finish();
94 1
    }
95
96 1
    public function onExecutedProcess(ProcessState $state, StepInterface $step)
97
    {
98 1
    }
99
100 1
    public function onFailedLoop(ProcessState $state, StepInterface $step)
101
    {
102 1
        return;
103
    }
104
105 1
    public function onSuccessLoop(ProcessState $state, StepInterface $step)
106
    {
107 1
        return;
108
    }
109
}
110