Passed
Push — main ( 2cee8a...b41e82 )
by Anatolyi
09:15
created

ShiftedNumber   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setMax() 0 5 1
A __invoke() 0 5 1
A getShiftValue() 0 3 1
A shift() 0 9 3
A setMin() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gambling\Tech;
6
7
/**
8
 * Base value  shifting by hash
9
 */
10
class ShiftedNumber
11
{
12
    private float $min = 0;
13
14
    private float $max = 100;
15
16
    /**
17
     * @param float $number
18
     * @param string $hash
19
     * @return int
20
     */
21
    public function __invoke(float $number, string $hash): int
22
    {
23
        $shiftValue = $this->getShiftValue($hash);
24
25
        return $this->shift($number, $shiftValue);
26
    }
27
28
    /**
29
     * @param float $min
30
     * @return $this
31
     */
32
    public function setMin(float $min): self
33
    {
34
        $this->min = $min;
35
36
        return $this;
37
    }
38
39
    /**
40
     * @param float $max
41
     * @return $this
42
     */
43
    public function setMax(float $max): self
44
    {
45
        $this->max = $max;
46
47
        return $this;
48
    }
49
50
    /**
51
     * @param float $number
52
     * @param int $shift
53
     * @return int
54
     */
55
    protected function shift(float $number, int $shift): int
56
    {
57
        if ($shift > $this->max) {
58
            $shift %= ($this->max - $this->min + 1);
59
        }
60
61
        return $number + $shift <= $this->max ?
0 ignored issues
show
Bug Best Practice introduced by
The expression return $number + $shift ...$shift - $this->max - 1 returns the type double which is incompatible with the type-hinted return integer.
Loading history...
62
            $number + $shift :
63
            $this->min + ($number + $shift - $this->max) - 1;
64
    }
65
66
    /**
67
     * @param string $clientHash
68
     * @return int
69
     */
70
    protected function getShiftValue(string $clientHash): int
71
    {
72
        return (int)hexdec(substr($clientHash, -5));
73
    }
74
}
75