/
User.php
141 lines (121 loc) · 2.54 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
<?php
namespace Lw\Domain\Model\User;
use Assert\Assertion;
use Ddd\Domain\DomainEventPublisher;
use Lw\Domain\Model\Wish\WishEmail;
use Lw\Domain\Model\Wish\WishId;
/**
* Class User
* @package Lw\Domain\Model\User
*/
class User
{
const MAX_LENGTH_EMAIL = 255;
const MAX_LENGTH_PASSWORD = 255;
/**
* @var UserId
*/
protected $userId;
/**
* @var string
*/
protected $email;
/**
* @var string
*/
protected $password;
/**
* @var \DateTime
*/
protected $createdOn;
/**
* @var \DateTime
*/
protected $updatedOn;
/**
* @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();
DomainEventPublisher::instance()->publish(
new UserRegistered(
$this->userId
)
);
}
/**
* @return UserId
*/
public function id()
{
return $this->userId;
}
/**
* @return string
*/
public function email()
{
return $this->email;
}
/**
* @return string
*/
public function password()
{
return $this->password;
}
/**
* @param string $password
*/
public function changePassword($password)
{
$password = trim($password);
if (!$password) {
throw new \InvalidArgumentException('password');
}
$this->password = $password;
}
/**
* @param WishId $wishId
* @param string $email
* @param string $content
* @return \Lw\Domain\Model\Wish\WishEmail
*/
public function makeWish(WishId $wishId, $email, $content)
{
return new WishEmail(
$wishId,
$this->id(),
$email,
$content
);
}
public function grantWishes()
{
$wishesGranted = 0;
foreach ($this->wishes as $wish) {
$wish->grant();
$wishesGranted++;
}
return $wishesGranted;
}
/**
* @param $email
*/
protected function setEmail($email)
{
$email = trim($email);
if (!$email) {
throw new \InvalidArgumentException('email');
}
Assertion::email($email);
$this->email = strtolower($email);
}
}