Completed
Push — 0.4 ( bc6dc3...283c8e )
by jean
9s
created

BreakerProcessNotifier::onFailedLoop()   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 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ProcessBundle\ProcessNotifier;
6
7
use Darkilliant\ProcessBundle\State\ProcessState;
8
use Darkilliant\ProcessBundle\Step\StepInterface;
9
10
class BreakerProcessNotifier extends AbstractProcessNotifier
11
{
12
    private $breaker = null;
13
14
    private $count = 0;
15
    private $startedAt;
16
    private $timeElasped = 0;
17
18 1
    public function onStartProcess(ProcessState $state, StepInterface $step)
19
    {
20
        // TODO: Implement onStartProcess() method.
21 1
    }
22
23 7
    public function onStartIterableProcess(ProcessState $state, StepInterface $step)
24
    {
25 7
        $this->breaker = null;
26
27 7
        if (!$state->getOptions()['breaker']) {
28 1
            return;
29
        }
30
31 6
        $maxIteration = $state->getOptions()['breaker_max_iteration'];
32 6
        $maxTime = $state->getOptions()['breaker_max_time'];
33 6
        $sleepBetween = $state->getOptions()['breaker_sleep_between'];
34
35 6
        $this->breaker = ['max_time' => $maxTime, 'max_iteration' => $maxIteration, 'sleep_between' => $sleepBetween];
36
37 6
        $state->info('breaker', $this->breaker);
38
39 6
        $this->count = 0;
40 6
        $this->startedAt = time();
41 6
        $this->timeElasped = 0;
42 6
    }
43
44 6
    public function onUpdateIterableProcess(ProcessState $state, StepInterface $step)
45
    {
46 6
        if (!$this->breaker) {
47 1
            return;
48
        }
49
50 5
        $this->timeElasped = time() - $this->startedAt;
51 5
        ++$this->count;
52 5
        sleep($this->breaker['sleep_between']);
53
54 5
        if (!$this->isValid()) {
55 3
            $state->info('breaker stop current loop');
56 3
            $state->markBreak();
57
58 3
            return;
59
        }
60 2
    }
61
62 1
    public function onEndProcess(ProcessState $state, StepInterface $step)
63
    {
64
        // TODO: Implement onEndProcess() method.
65 1
    }
66
67 1
    public function onExecutedProcess(ProcessState $state, StepInterface $step)
68
    {
69
        // TODO: Implement onExecutedProcess() method.
70 1
    }
71
72 1
    public function onFailedLoop(ProcessState $state, StepInterface $step)
73
    {
74 1
        return;
75
    }
76
77 1
    public function onSuccessLoop(ProcessState $state, StepInterface $step)
78
    {
79 1
        return;
80
    }
81
82 5
    private function isValid()
83
    {
84 5
        if (null !== $this->breaker['max_time'] && $this->timeElasped >= $this->breaker['max_time']) {
85 2
            return false;
86
        }
87
88 3
        if (null !== $this->breaker['max_iteration'] && $this->count >= $this->breaker['max_iteration']) {
89 1
            return false;
90
        }
91
92 2
        return true;
93
    }
94
}
95