Test Failed
Push — master ( 2fcf63...a2b1bc )
by Alec
02:17
created

ExtendedCounter::bump()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

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