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