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
|
9 |
|
public function __construct(?string $name = null, ?int $step = null, int $initialValue = 0) |
15
|
|
|
{ |
16
|
9 |
|
parent::__construct($name, $step, $initialValue); |
17
|
9 |
|
$this->setBindings( |
18
|
9 |
|
ExtendedCounterReport::class, |
19
|
9 |
|
ExtendedCounterReportFormatter::class |
20
|
|
|
); |
21
|
9 |
|
$this->updateExtendedValues(); |
22
|
9 |
|
} |
23
|
|
|
|
24
|
9 |
|
protected function updateExtendedValues(): void |
25
|
|
|
{ |
26
|
9 |
|
$this->updateMaxAndMin(); |
27
|
9 |
|
$this->updateDiff(); |
28
|
9 |
|
} |
29
|
|
|
|
30
|
9 |
|
protected function updateMaxAndMin(): void |
31
|
|
|
{ |
32
|
9 |
|
if ($this->value > $this->max) { |
33
|
7 |
|
$this->max = $this->value; |
34
|
|
|
} |
35
|
9 |
|
if ($this->value < $this->min) { |
36
|
7 |
|
$this->min = $this->value; |
37
|
|
|
} |
38
|
9 |
|
} |
39
|
|
|
|
40
|
9 |
|
protected function updateDiff(): void |
41
|
|
|
{ |
42
|
9 |
|
$this->diff = $this->value - $this->initialValue; |
43
|
9 |
|
} |
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++; |
71
|
|
|
} else { |
72
|
7 |
|
$this->value -= $times * $this->step; |
73
|
7 |
|
$this->bumpedBack++; |
74
|
|
|
} |
75
|
7 |
|
$this->updateExtendedValues(); |
76
|
|
|
return |
77
|
7 |
|
$this->value; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
* |
83
|
|
|
*/ |
84
|
9 |
|
protected function updateValues(int $initialValue): void |
85
|
|
|
{ |
86
|
9 |
|
$this->value = $this->initialValue = $this->length = $this->max = $this->min = $initialValue; |
87
|
9 |
|
} |
88
|
|
|
} |
89
|
|
|
|