ExtendedCounter::updateValues()   A
last analyzed

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\Counters;
4
5
use AlecRabbit\Counters\Core\Traits\ExtendedCounterFields;
6
use AlecRabbit\Formatters\ExtendedCounterReportFormatter;
7
use AlecRabbit\Reports\ExtendedCounterReport;
8
9
class ExtendedCounter extends SimpleCounter
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->setBindings(
18 10
            ExtendedCounterReport::class,
19 10
            ExtendedCounterReportFormatter::class
20
        );
21 10
        $this->updateExtendedValues();
22 10
    }
23
24 10
    protected function updateExtendedValues(): void
25
    {
26 10
        $this->updateMaxAndMin();
27 10
        $this->updateDiff();
28 10
    }
29
30 10
    protected function updateMaxAndMin(): void
31
    {
32 10
        if ($this->value > $this->max) {
33 7
            $this->max = $this->value;
34
        }
35 10
        if ($this->value < $this->min) {
36 7
            $this->min = $this->value;
37
        }
38 10
    }
39
40 10
    protected function updateDiff(): void
41
    {
42 10
        $this->diff = $this->value - $this->initialValue;
43 10
    }
44
45
    /**
46
     * @param int $times
47
     * @return int
48
     */
49 7
    public function bumpBack(int $times = 1): int
50
    {
51
        return
52 7
            $this->bump($times, false);
53
    }
54
55
    /**
56
     * @param int $times
57
     * @param bool $forward
58
     * @return int
59
     */
60 7
    public function bump(int $times = 1, bool $forward = true): int
61
    {
62 7
        $times = $this->assertTimes($times);
63 7
        if ($this->isNotStarted()) {
64 7
            $this->start();
65
        }
66 7
        $this->path += $times * $this->step;
67 7
        $this->length += $times * $this->step;
68 7
        if ($forward) {
69 7
            $this->value += $times * $this->step;
70 7
            $this->bumped += $times;
71
        } else {
72 7
            $this->value -= $times * $this->step;
73 7
            $this->bumpedBack += $times;
74
        }
75 7
        $this->updateExtendedValues();
76
        return
77 7
            $this->value;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     *
83
     */
84 10
    protected function updateValues(int $initialValue): void
85
    {
86 10
        $this->value = $this->initialValue = $this->length = $this->max = $this->min = $initialValue;
87 10
    }
88
}
89