Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| ApiKeyGenerator | |
100.00% |
7 / 7 |
|
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |
100.00% |
1 / 1 |
| generate | |
100.00% |
7 / 7 |
|
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Service\Data; |
| 4 | |
| 5 | use Random\RandomException; |
| 6 | |
| 7 | class ApiKeyGenerator implements ApiKeyGeneratorInterface |
| 8 | { |
| 9 | /** |
| 10 | * @throws RandomException |
| 11 | */ |
| 12 | public function generate(int $length = 32): string |
| 13 | { |
| 14 | if (0 !== $length % 2) { |
| 15 | throw new \InvalidArgumentException('Length must be even'); |
| 16 | } |
| 17 | |
| 18 | if ($length < 16) { |
| 19 | throw new \InvalidArgumentException('Length must be greater than 16'); |
| 20 | } |
| 21 | |
| 22 | if ($length > 64) { |
| 23 | throw new \InvalidArgumentException('Length must be less than 64'); |
| 24 | } |
| 25 | |
| 26 | return bin2hex(random_bytes($length / 2)); |
| 27 | } |
| 28 | } |