1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Counters\Core; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Counters\Contracts\CounterInterface; |
6
|
|
|
use AlecRabbit\Reports\Core\Reportable; |
7
|
|
|
use AlecRabbit\Reports\SimpleCounterReport; |
8
|
|
|
use AlecRabbit\Counters\Core\Traits\SimpleCounterFields; |
9
|
|
|
use AlecRabbit\Reports\Contracts\ReportInterface; |
10
|
|
|
|
11
|
|
|
abstract class AbstractCounter extends Reportable implements CounterInterface |
12
|
|
|
{ |
13
|
|
|
use SimpleCounterFields; |
14
|
|
|
|
15
|
|
|
protected const DEFAULT_STEP = 1; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Counter constructor |
19
|
|
|
* @param null|string $name |
20
|
|
|
* @param null|int $step |
21
|
|
|
* @param int $initialValue |
22
|
|
|
* @throws \Exception |
23
|
|
|
*/ |
24
|
21 |
|
public function __construct(?string $name = null, ?int $step = null, int $initialValue = 0) |
25
|
|
|
{ |
26
|
21 |
|
$this->name = $this->defaultName($name); |
27
|
21 |
|
$this->setInitialValue($initialValue); |
28
|
21 |
|
$this->setStep($step); |
29
|
21 |
|
$this->report = $this->createEmptyReport(); |
30
|
21 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param int $initialValue |
34
|
|
|
* @return AbstractCounter |
35
|
|
|
*/ |
36
|
21 |
|
public function setInitialValue(int $initialValue): AbstractCounter |
37
|
|
|
{ |
38
|
21 |
|
if (false === $this->isStarted()) { |
39
|
21 |
|
$this->updateValues($initialValue); |
40
|
|
|
} else { |
41
|
1 |
|
throw new \RuntimeException('You can\'t set counter initial value, it has been bumped already.'); |
42
|
|
|
} |
43
|
21 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param int $initialValue |
48
|
|
|
*/ |
49
|
14 |
|
protected function updateValues(int $initialValue): void |
50
|
|
|
{ |
51
|
14 |
|
$this->value = $this->initialValue = $initialValue; |
52
|
14 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param null|int $step |
56
|
|
|
* @return AbstractCounter |
57
|
|
|
*/ |
58
|
21 |
|
public function setStep(?int $step = null): AbstractCounter |
59
|
|
|
{ |
60
|
21 |
|
$step = $this->assertStep($step); |
61
|
21 |
|
if (false === $this->isStarted()) { |
62
|
21 |
|
$this->step = $step; |
63
|
|
|
} else { |
64
|
1 |
|
throw new \RuntimeException('You can\'t set counter step value, it has been bumped already.'); |
65
|
|
|
} |
66
|
21 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param null|int $step |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
21 |
|
protected function assertStep(?int $step = null): int |
74
|
|
|
{ |
75
|
21 |
|
$step = $step ?? self::DEFAULT_STEP; |
76
|
21 |
|
if ($step === 0) { |
77
|
1 |
|
throw new \RuntimeException('Counter step should be non-zero integer.'); |
78
|
|
|
} |
79
|
21 |
|
return $step; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// /** |
83
|
|
|
// * @throws \Exception |
84
|
|
|
// */ |
85
|
14 |
|
// protected function buildReport(): void |
86
|
|
|
// { |
87
|
14 |
|
// $this->report = (new SimpleCounterReport())->buildOn($this); |
88
|
14 |
|
// } |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param int $times |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
|
|
public function bump(int $times = 1): int |
95
|
|
|
{ |
96
|
|
|
$times = $this->assertTimes($times); |
97
|
|
|
if ($this->isNotStarted()) { |
98
|
|
|
$this->start(); |
99
|
10 |
|
} |
100
|
|
|
$this->value += $times * $this->step; |
101
|
10 |
|
$this->bumped++; |
102
|
8 |
|
return |
103
|
8 |
|
$this->value; |
104
|
|
|
} |
105
|
8 |
|
|
106
|
8 |
|
protected function assertTimes(int $times): int |
107
|
|
|
{ |
108
|
8 |
|
if ($times < 1) { |
109
|
|
|
throw new \RuntimeException( |
110
|
|
|
'Parameter 0 for bump() or bumpBack() should be positive non-zero integer.' |
111
|
17 |
|
); |
112
|
|
|
} |
113
|
17 |
|
return $times; |
114
|
2 |
|
} |
115
|
|
|
} |
116
|
|
|
|