| Total Complexity | 8 |
| Total Lines | 76 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 7 | class SiftedNumberAlgorithm |
||
| 8 | { |
||
| 9 | private float $min = 0; |
||
| 10 | |||
| 11 | private float $max = 100; |
||
| 12 | |||
| 13 | private string $serverSeed; |
||
| 14 | |||
| 15 | private string $clientSeed; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @param float $number |
||
| 19 | * @return int |
||
| 20 | */ |
||
| 21 | public function __invoke(float $number): int |
||
| 22 | { |
||
| 23 | $clientHash = $this->getClientHash($number); |
||
| 24 | $shiftValue = $this->getShiftValue($clientHash); |
||
| 25 | |||
| 26 | return $this->shift($number, $shiftValue); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param float $min |
||
| 31 | * @return $this |
||
| 32 | */ |
||
| 33 | public function setMin(float $min): self |
||
| 34 | { |
||
| 35 | $this->min = $min; |
||
| 36 | |||
| 37 | return $this; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param float $max |
||
| 42 | * @return $this |
||
| 43 | */ |
||
| 44 | public function setMax(float $max): self |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param float $number |
||
| 53 | * @param int $shift |
||
| 54 | * @return int |
||
| 55 | */ |
||
| 56 | protected function shift(float $number, int $shift): int |
||
| 57 | { |
||
| 58 | if ($shift > $this->max) { |
||
| 59 | $shift %= ($this->max - $this->min + 1); |
||
| 60 | } |
||
| 61 | |||
| 62 | return $number + $shift <= $this->max ? |
||
|
|
|||
| 63 | $number + $shift : |
||
| 64 | $this->min + ($number + $shift - $this->max) - 1; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param float $secret |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | protected function getClientHash(float $secret): string |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $clientHash |
||
| 78 | * @return int |
||
| 79 | */ |
||
| 80 | protected function getShiftValue(string $clientHash): int |
||
| 83 | } |
||
| 84 | } |
||
| 85 |