Completed
Push — develop ( ca99c7...b953bd )
by Alec
03:03
created

ExtendedCounter::updateValues()   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 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 10
    public function __construct(?string $name = null, ?int $step = null, int $initialValue = 0)
15
    {
16 10
        parent::__construct($name, $step, $initialValue);
17 10
        $this->updateExtendedValues();
18 10
    }
19
20 10
    protected function updateExtendedValues(): void
21
    {
22 10
        $this->updateMaxAndMin();
23 10
        $this->updateDiff();
24 10
    }
25
26 10
    protected function updateMaxAndMin(): void
27
    {
28 10
        if ($this->value > $this->max) {
29 9
            $this->max = $this->value;
30
        }
31 10
        if ($this->value < $this->min) {
32 7
            $this->min = $this->value;
33
        }
34 10
    }
35
36 10
    protected function updateDiff(): void
37
    {
38 10
        $this->diff = $this->value - $this->initialValue;
39 10
    }
40
41
    /**
42
     * @param int $times
43
     * @return int
44
     */
45 7
    public function bumpBack(int $times = 1): int
46
    {
47
        return
48 7
            $this->bump($times, false);
49
    }
50
51
    /**
52
     * @param int $times
53
     * @param bool $forward
54
     * @return int
55
     */
56 9
    public function bump(int $times = 1, bool $forward = true): int
57
    {
58 9
        $times = $this->assertTimes($times);
59 9
        if ($this->isNotStarted()) {
60 9
            $this->start();
61
        }
62 9
        $this->path += $times * $this->step;
63 9
        $this->length += $times * $this->step;
64 9
        if ($forward) {
65 9
            $this->value += $times * $this->step;
66 9
            $this->bumped++;
67
        } else {
68 7
            $this->value -= $times * $this->step;
69 7
            $this->bumpedBack++;
70
        }
71 9
        $this->updateExtendedValues();
72
        return
73 9
            $this->value;
74
    }
75
76
    /** {@inheritdoc} */
77 10
    protected function buildReport(): void
78
    {
79
        // todo change to ExtendedCounterReport
80 10
        $this->report = (new SimpleCounterReport())->buildOn($this);
81 10
    }
82
83
    /**
84
     * {@inheritdoc}
85
     * todo rename this method
86
     */
87 10
    protected function updateValues(int $initialValue): void
88
    {
89 10
        $this->value = $this->initialValue = $this->length = $this->max = $this->min = $initialValue;
90 10
    }
91
92
}
93