Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| UserAccessValidatorService | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
6 | |
100.00% |
1 / 1 |
| validate | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\User\Services; |
| 4 | |
| 5 | use Domain\Shared\Enums\SectorEnum; |
| 6 | use Domain\User\Enums\AccessLevel; |
| 7 | use Domain\User\Exceptions\UserAccessLevelNotAllowedInSectorException; |
| 8 | |
| 9 | readonly class UserAccessValidatorService |
| 10 | { |
| 11 | /** |
| 12 | * @param array $accessLevels |
| 13 | */ |
| 14 | public function validate(array $accessLevels): void |
| 15 | { |
| 16 | $errors = []; |
| 17 | |
| 18 | foreach ($accessLevels as $sectorId => $levels) { |
| 19 | |
| 20 | $sectorEnum = SectorEnum::tryFrom((int) $sectorId); |
| 21 | |
| 22 | if (!$sectorEnum) { |
| 23 | throw new UserAccessLevelNotAllowedInSectorException( |
| 24 | __("Invalid sector.") |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | $allowedLevels = collect($sectorEnum->allowedAccessLevels()) |
| 29 | ->map(fn ($level) => $level->value) |
| 30 | ->toArray(); |
| 31 | |
| 32 | foreach ((array) $levels as $level) { |
| 33 | if (!in_array($level->value, [AccessLevel::MASTER, AccessLevel::ADMIN])) { |
| 34 | if (!in_array($level->value, $allowedLevels, true)) { |
| 35 | throw new UserAccessLevelNotAllowedInSectorException( |
| 36 | __("The access level :accessLevel is not allowed for the sector :sector.", |
| 37 | [ |
| 38 | "accessLevel" => $level->label(), |
| 39 | "sector" => $sectorEnum->label() |
| 40 | ]) |
| 41 | ); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |