Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
26 / 26 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
UserService | |
100.00% |
26 / 26 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
create | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
getById | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
list | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace App\Service; |
6 | |
7 | use App\Dto\CreateUser; |
8 | use App\Entity\User; |
9 | use App\Exception\Service\UserService\UserExistsException; |
10 | use App\Exception\Service\UserService\UserNotFoundException; |
11 | use App\Repository\UserRepositoryInterface; |
12 | |
13 | class UserService |
14 | { |
15 | public function __construct(protected UserRepositoryInterface $userRepository) |
16 | { |
17 | } |
18 | |
19 | public function create(CreateUser $createUser): User |
20 | { |
21 | $userFound = $this |
22 | ->userRepository |
23 | ->getByEmail($createUser->email) |
24 | ; |
25 | |
26 | if ($userFound instanceof User) { |
27 | throw UserExistsException::create($createUser->email); |
28 | } |
29 | |
30 | $user = new User(); |
31 | $user->setName($createUser->name); |
32 | $user->setEmail($createUser->email); |
33 | $this |
34 | ->userRepository |
35 | ->save($user, true) |
36 | ; |
37 | |
38 | return $user; |
39 | } |
40 | |
41 | public function getById(int $id): User |
42 | { |
43 | $user = $this |
44 | ->userRepository |
45 | ->getById($id) |
46 | ; |
47 | if (!$user instanceof User) { |
48 | throw UserNotFoundException::create($id); |
49 | } |
50 | |
51 | return $user; |
52 | } |
53 | |
54 | public function list(?string $nameCriteria = null, ?string $emailCriteria = null): array |
55 | { |
56 | return $this |
57 | ->userRepository |
58 | ->list($nameCriteria, $emailCriteria) |
59 | ; |
60 | } |
61 | } |