Total Complexity | 7 |
Total Lines | 63 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
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 |
||
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 ? |
||
|
|||
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 |
||
73 | } |
||
74 | } |
||
75 |