-
Notifications
You must be signed in to change notification settings - Fork 121
/
User.php
185 lines (158 loc) · 3.79 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace Lw\Domain\Model\User;
use Assert\Assertion;
use Ddd\Domain\DomainEventPublisher;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Lw\Domain\Model\Wish\Wish;
use Lw\Domain\Model\Wish\WishId;
class User
{
const MAX_LENGTH_EMAIL = 255;
const MAX_LENGTH_PASSWORD = 255;
const MAX_WISHES = 3;
/**
* @var UserId
*/
protected $userId;
/**
* @var string
*/
protected $email;
/**
* @var string
*/
protected $password;
/**
* @var \DateTime
*/
protected $createdOn;
/**
* @var \DateTime
*/
protected $updatedOn;
/**
* @var ArrayCollection
*/
protected $wishes;
/**
* @param UserId $userId
* @param string $email
* @param string $password
*/
public function __construct(UserId $userId, $email, $password)
{
$this->userId = $userId;
$this->setEmail($email);
$this->changePassword($password);
$this->createdOn = new \DateTime();
$this->updatedOn = new \DateTime();
$this->wishes = new ArrayCollection();
DomainEventPublisher::instance()->publish(
new UserRegistered(
$this->userId
)
);
}
/**
* @param $email
*/
protected function setEmail($email)
{
$email = trim($email);
if (!$email) {
throw new \InvalidArgumentException('email');
}
Assertion::email($email);
$this->email = strtolower($email);
}
/**
* @param string $password
*/
public function changePassword($password)
{
$password = trim($password);
if (!$password) {
throw new \InvalidArgumentException('password');
}
$this->password = $password;
}
/**
* @return UserId
*/
public function id()
{
return $this->userId;
}
/**
* @return string
*/
public function email()
{
return $this->email;
}
/**
* @return string
*/
public function password()
{
return $this->password;
}
public function makeWishNoAggregateVersion(WishId $wishId, $address, $content)
{
return new Wish(
$wishId,
$this->id(),
$address,
$content
);
}
public function makeWishAggregateVersion($address, $content)
{
if (count($this->wishes) >= self::MAX_WISHES) {
throw new NoMoreWishesAllowedException();
}
$this->wishes[] = new Wish(
new WishId(),
$this->id(),
$address,
$content
);
}
public function numberOfRemainingWishes()
{
return self::MAX_WISHES - count($this->wishes);
}
public function grantWishes()
{
$wishesGranted = 0;
foreach ($this->wishes as $wish) {
$wish->grant();
++$wishesGranted;
}
return $wishesGranted;
}
public function updateWish(WishId $wishId, $address, $content)
{
foreach ($this->wishes as $k => $wish) {
if ($wish->id()->equals($wishId)) {
$wish->changeContent($content);
$wish->changeAddress($address);
break;
}
}
}
public function deleteWish(WishId $wishId)
{
// $wishes = $this->wishes->matching(Criteria::create()->where(Criteria::expr()->eq('id', $wishId)));
// foreach ($wishes as $wish) {
// $this->wishes->removeElement($wish);
// }
foreach ($this->wishes as $k => $wish) {
if ($wish->id()->equals($wishId)) {
unset($this->wishes[$k]);
break;
}
}
}
}