1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace User\Service; |
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use User\Factory\User as UserFactory; |
9
|
|
|
use User\Entity\User as UserModel; |
10
|
|
|
use Exception; |
11
|
|
|
use InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* User Service |
15
|
|
|
* |
16
|
|
|
* @author Thiago Paes <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
final class User implements UserInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \User\Repository\UserInterface |
22
|
|
|
*/ |
23
|
|
|
private $users; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var EntityManagerInterface |
27
|
|
|
*/ |
28
|
|
|
private $em; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* User Service Constructor. |
32
|
|
|
* |
33
|
|
|
* @param EntityManagerInterface $em |
34
|
|
|
*/ |
35
|
|
|
public function __construct(EntityManagerInterface $em) |
36
|
|
|
{ |
37
|
|
|
$this->em = $em; |
38
|
|
|
$this->users = $em->getRepository(UserModel::class); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public function save(UserModel $user): UserModel |
45
|
|
|
{ |
46
|
|
|
$this->em->beginTransaction(); |
47
|
|
|
|
48
|
|
|
try { |
49
|
|
|
$this->em->persist($user); |
50
|
|
|
$this->em->flush(); |
51
|
|
|
$this->em->commit(); |
52
|
|
|
|
53
|
|
|
return $user; |
54
|
|
|
} catch (UniqueConstraintViolationException $ex) { |
55
|
|
|
$this->em->rollBack(); |
56
|
|
|
|
57
|
|
|
throw new InvalidArgumentException('Email is already registered', 409, $ex); |
58
|
|
|
} catch (Exception $ex) { |
59
|
|
|
$this->em->rollBack(); |
60
|
|
|
|
61
|
|
|
throw new InvalidArgumentException($ex->getMessage(), 500, $ex); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public function delete(UserModel $user): UserModel |
69
|
|
|
{ |
70
|
|
|
$user->delete(); |
71
|
|
|
|
72
|
|
|
$this->em->persist($user); |
73
|
|
|
$this->em->flush(); |
74
|
|
|
|
75
|
|
|
return $user; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function findByUserId(int $id): UserModel |
82
|
|
|
{ |
83
|
|
|
return $this->users->findById($id); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
|
|
public function listAll(): array |
90
|
|
|
{ |
91
|
|
|
$users = $this->users->findAll(); |
92
|
|
|
|
93
|
|
|
return $users; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritdoc |
98
|
|
|
*/ |
99
|
|
|
public function create(string $name, string $email): UserModel |
100
|
|
|
{ |
101
|
|
|
$user = UserFactory::create($name, $email); |
102
|
|
|
|
103
|
|
|
return $this->save($user); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
109
|
|
|
public function update(UserModel $user, string $name, string $email): UserModel |
110
|
|
|
{ |
111
|
|
|
/* @var $update \User\Entity\User */ |
112
|
|
|
$update = UserFactory::update($user, $name, $email); |
113
|
|
|
|
114
|
|
|
return $this->save($update); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..