Test Setup Failed
Push — develop ( 7cd2b2...750858 )
by Alec
03:42
created

ExtendedCounter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools;
4
5
use AlecRabbit\Tools\Contracts\ExtendedCounterValuesInterface;
6
use AlecRabbit\Tools\Reports\SimpleCounterReport;
7
use AlecRabbit\Tools\Traits\ExtendedCounterFields;
8
9
class ExtendedCounter extends SimpleCounter implements ExtendedCounterValuesInterface
10
{
11
    use ExtendedCounterFields;
12
13
    /** {@inheritdoc} */
14
    public function __construct(?string $name = null, ?int $step = null, int $initialValue = 0)
15
    {
16
        parent::__construct($name, $step, $initialValue);
17
        $this->updateExtendedValues();
18
    }
19
20
    /** {@inheritdoc} */
21
    protected function buildReport(): void
22
    {
23
        // todo change to ExtendedCounterReport
24
        $this->report = (new SimpleCounterReport())->buildOn($this);
25
    }
26
27
    protected function updateExtendedValues(): void
28
    {
29
        $this->updateMaxAndMin();
30
        $this->updateDiff();
31
    }
32
33
    protected function updateMaxAndMin(): void
34
    {
35
        if ($this->value > $this->max) {
36
            $this->max = $this->value;
37
        }
38
        if ($this->value < $this->min) {
39
            $this->min = $this->value;
40
        }
41
    }
42
43
    protected function updateDiff(): void
44
    {
45
        $this->diff = $this->value - $this->initialValue;
46
    }
47
48
    /**
49
     * @param int $times
50
     * @return int
51
     */
52
    public function bumpBack(int $times = 1): int
53
    {
54
        return
55
            $this->bump($times, false);
56
    }
57
58
    /**
59
     * @param int $times
60
     * @param bool $forward
61
     * @return int
62
     */
63
    public function bump(int $times = 1, bool $forward = true): int
64
    {
65
        $times = $this->assertTimes($times);
66
        if ($this->isNotStarted()) {
67
            $this->start();
68
        }
69
        $this->path += $times * $this->step;
70
        $this->length += $times * $this->step;
71
        if ($forward) {
72
            $this->value += $times * $this->step;
73
            $this->bumped++;
74
        } else {
75
            $this->value -= $times * $this->step;
76
            $this->bumpedBack++;
77
        }
78
        $this->updateExtendedValues();
79
        return
80
            $this->value;
81
    }
82
83
    /**
84
     * @param int $initialValue
85
     * @return AbstractCounter
86
     */
87
    public function setInitialValue(int $initialValue): AbstractCounter
88
    {
89
        if (false === $this->isStarted()) {
90
            $this->value = $this->initialValue = $this->length = $this->max = $this->min = $initialValue;
91
        } else {
92
            throw new \RuntimeException('You can\'t set counter initial value, it has been bumped already.');
93
        }
94
        return $this;
95
    }
96
}
97