Passed
Push — master ( a2b1bc...228ac7 )
by Alec
03:07
created

AbstractCounter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 95
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

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