Test Failed
Push — develop ( f741b1...4c7784 )
by Alec
05:40
created

Counter::setStep()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: alec
4
 * Date: 14.10.18
5
 * Time: 2:18
6
 */
7
8
namespace AlecRabbit\Tools;
9
10
use AlecRabbit\Tools\Contracts\CounterInterface;
11
use AlecRabbit\Tools\Reports\Contracts\ReportableInterface;
12
use AlecRabbit\Tools\Reports\Traits\Reportable;
13
use AlecRabbit\Tools\Traits\CounterFields;
14
15
class Counter implements CounterInterface, ReportableInterface
16
{
17
    use CounterFields, Reportable;
18
19
    /**
20
     * Counter constructor
21
     * @param string|null $name
22
     * @param int $step
23
     * @param int $initialValue
24 21
     */
25
    public function __construct(?string $name = null, int $step = 1, int $initialValue = 0)
26 21
    {
27 21
        $this->name = $this->defaultName($name);
28 21
        $this->setInitialValue($initialValue);
29 21
        $this->setStep($step);
30
    }
31
32
    /**
33
     * @param int $initialValue
34 4
     * @return Counter
35
     */
36
    public function setInitialValue(int $initialValue): Counter
37 4
    {
38
        if (false === $this->isStarted()) {
39
            $this->value = $this->initialValue = $this->length = $initialValue;
40
        } else {
41
            throw new \RuntimeException('You can\'t set counter initial value, it has been bumped already.');
42
        }
43 7
        return $this;
44
    }
45 7
46
    /**
47 7
     * @param int $step
48
     * @return Counter
49
     */
50
    public function setStep(int $step): Counter
51
    {
52
        $step = $this->assertStep($step);
53
        if (false === $this->isStarted()) {
54
            $this->step = $step;
55 3
        } else {
56
            throw new \RuntimeException('You can\'t set counter step value, it has been bumped already.');
57 3
        }
58 1
        return $this;
59 1
    }
60
61
    /**
62 1
     * @param int $step
63
     * @return int
64
     */
65
    protected function assertStep(int $step): int
66
    {
67
        if ($step === 0) {
68
            throw new \RuntimeException('Counter step should be non-zero integer.');
69 5
        }
70
        return $step;
71 5
    }
72 3
73
    /**
74 2
     * @param int $times
75
     * @return int
76
     */
77
    public function bumpBack(int $times = 1): int
78
    {
79
        return
80
            $this->bump($times, false);
81 3
    }
82
83 3
    /**
84 2
     * @param int $times
85
     * @param bool $forward
86
     * @return int
87
     */
88
    public function bump(int $times = 1, bool $forward = true): int
89
    {
90 1
        $times = $this->assertTimes($times);
91
        $this->start();
92 1
        $this->path += $times * $this->step;
93
        $this->length += $times * $this->step;
94 1
        if ($forward) {
95
            $this->value += $times * $this->step;
96
            $this->bumpedForward++;
97
        } else {
98
            $this->value -= $times * $this->step;
99
            $this->bumpedBack++;
100
        }
101
        return
102
            $this->value;
103
    }
104
105
    protected function assertTimes(int $times): int
106
    {
107
        if ($times < 1) {
108
            throw new \RuntimeException(__METHOD__ . ' parameter 0 should be positive non-zero integer.');
109
        }
110
        return $times;
111
    }
112
113
    protected function start(): void
114
    {
115
        $this->started = true;
116
    }
117
}
118