Completed
Push — master ( d972fa...aafada )
by Dmytro
13:29
created

ExponentialStrategy::setInterval()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Webhook\Domain\Infrastructure\Strategy;
6
7
8
/**
9
 * Class ExponentialStrategy.
10
 */
11
final class ExponentialStrategy extends AbstractStrategy
12
{
13
    /** @var int */
14
    protected $interval;
15
16
    /** @var float */
17
    protected $base;
18
19
    /**
20
     * @param int $interval
21
     * @param float $base
22
     */
23 3
    public function __construct(int $interval = 5, float $base = 2.0)
24
    {
25 3
        $this->setInterval($interval);
26 2
        $this->setBase($base);
27 2
    }
28
29
    /**
30
     * @param int $interval
31
     */
32 3 View Code Duplication
    public function setInterval($interval)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34 3
        if (!is_int($interval) || (int) $interval < 0) {
35 1
            throw new \InvalidArgumentException('Interval should be positive integer');
36
        }
37
38 2
        $this->interval = (int) $interval;
39 2
    }
40
41
    /**
42
     * @param $base
43
     */
44 2
    public function setBase($base)
45
    {
46 2
        if (!is_numeric($base) || (float) $base < 0) {
47
            throw new \InvalidArgumentException('Base should be positive float');
48
        }
49
50 2
        $this->base = (float) $base;
51 2
    }
52
53
    /**
54
     * @param int $attempt
55
     *
56
     * @return int
57
     */
58 1
    public function process(int $attempt): int
59
    {
60 1
        return (int) ceil($this->interval + ($this->base ** $attempt));
61
    }
62
}