AbstractCounter::setInitialValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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