Total Complexity | 9 |
Total Lines | 80 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
15 | class Counter implements CounterInterface, ReportableInterface |
||
16 | { |
||
17 | use CounterFields, Reportable; |
||
18 | |||
19 | /** |
||
20 | * Counter constructor |
||
21 | * @param string|null $name |
||
22 | * @param int $value |
||
23 | */ |
||
24 | 17 | public function __construct(?string $name = null, int $value = 0) |
|
25 | { |
||
26 | 17 | $this->name = $this->defaultName($name); |
|
27 | 17 | $this->value = $value; |
|
28 | 17 | $this->step = 1; |
|
29 | 17 | } |
|
30 | |||
31 | /** |
||
32 | * @return int |
||
33 | */ |
||
34 | 2 | public function bump(): int |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * @return int |
||
42 | */ |
||
43 | 5 | public function bumpUp(): int |
|
44 | { |
||
45 | 5 | $this->value += $this->step; |
|
46 | return |
||
47 | 5 | $this->value; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param int $step |
||
52 | * @param bool $setStep |
||
53 | * @return int |
||
54 | */ |
||
55 | 3 | public function bumpWith(int $step, bool $setStep = false): int |
|
56 | { |
||
57 | 3 | $this->value += $this->checkStep($step); |
|
58 | 1 | if ($setStep) { |
|
59 | 1 | $this->setStep($step); |
|
60 | } |
||
61 | return |
||
62 | 1 | $this->value; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param int $step |
||
67 | * @return int |
||
68 | */ |
||
69 | 5 | private function checkStep(int $step): int |
|
70 | { |
||
71 | 5 | if ($step === 0) { |
|
72 | 3 | throw new \RuntimeException('Counter step should be non-zero integer.'); |
|
73 | } |
||
74 | 2 | return $step; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param int $step |
||
79 | * @return Counter |
||
80 | */ |
||
81 | 3 | public function setStep(int $step): Counter |
|
82 | { |
||
83 | 3 | $this->step = $this->checkStep($step); |
|
84 | 2 | return $this; |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * @return int |
||
89 | */ |
||
90 | 1 | public function bumpDown(): int |
|
95 | } |
||
96 | } |
||
97 |