Completed
Push — develop ( f10e50...4f888d )
by Alec
03:20
created

Counter::bumpWith()   A

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 2
dl 0
loc 8
ccs 5
cts 5
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 $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
35
    {
36
        return
37 2
            $this->bumpUp();
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
91
    {
92 1
        $this->value -= $this->step;
93
        return
94 1
            $this->value;
95
    }
96
}
97