Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
39 / 39 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| UserDTO | |
100.00% |
39 / 39 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
3 | |||
| toArray | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| fromRequest | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| password | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\User\DTOs; |
| 4 | |
| 5 | use Domain\Shared\Interfaces\DataTransferObjectInterface; |
| 6 | use Illuminate\Foundation\Http\FormRequest; |
| 7 | use Illuminate\Support\Facades\Hash; |
| 8 | use Domain\User\Enums\AccessLevel; |
| 9 | |
| 10 | readonly final class UserDTO implements DataTransferObjectInterface |
| 11 | { |
| 12 | public ?int $id; |
| 13 | public ?string $name; |
| 14 | public ?string $email; |
| 15 | public ?array $accessLevel; |
| 16 | public ?string $password; |
| 17 | public ?int $badge; |
| 18 | public ?string $erp_user; |
| 19 | |
| 20 | public function __construct(array $args) |
| 21 | { |
| 22 | $this->id = $args['id'] ?? null; |
| 23 | $this->name = $args['name']; |
| 24 | $this->email = $args['email']; |
| 25 | $accessLevels = $args['access_level'] ?? []; |
| 26 | $this->accessLevel = collect($accessLevels) |
| 27 | ->mapWithKeys(fn($levels, $sector) => [ |
| 28 | $sector => array_map( |
| 29 | fn($level) => $level instanceof AccessLevel |
| 30 | ? $level |
| 31 | : AccessLevel::tryFrom((int)$level), |
| 32 | $levels |
| 33 | ) |
| 34 | ]) |
| 35 | ->toArray(); |
| 36 | $this->password = isset($args['password']) ? Hash::make($args['password']) : null; |
| 37 | $this->badge = $args['badge'] ?? null; |
| 38 | $this->erp_user = $args['erp_user'] ?? null; |
| 39 | } |
| 40 | |
| 41 | public function toArray(): array |
| 42 | { |
| 43 | return [ |
| 44 | 'id' => $this->id, |
| 45 | 'name' => $this->name, |
| 46 | 'email' => $this->email, |
| 47 | 'access_level' => array_map( |
| 48 | fn($levels) => array_map(fn($level) => $level->value, $levels), |
| 49 | $this->accessLevel |
| 50 | ), |
| 51 | 'badge' => $this->badge, |
| 52 | 'erp_user' => $this->erp_user, |
| 53 | ]; |
| 54 | } |
| 55 | |
| 56 | public static function fromRequest(FormRequest $request): UserDTO |
| 57 | { |
| 58 | return new self([ |
| 59 | 'name' => $request->validated('name'), |
| 60 | 'email' => $request->validated('email'), |
| 61 | 'access_level' => collect($request->validated('access_level')) |
| 62 | ->map(fn($levels) => array_map(fn($level) => AccessLevel::tryFrom((int)$level), $levels)) |
| 63 | ->toArray(), |
| 64 | 'password' => $request->validated('password'), |
| 65 | 'badge' => $request->validated('badge'), |
| 66 | 'erp_user' => $request->validated('erp_user'), |
| 67 | ]); |
| 68 | } |
| 69 | |
| 70 | public function password(): ?string |
| 71 | { |
| 72 | return $this->password; |
| 73 | } |
| 74 | } |