1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scheduler\Domain\Model\User; |
4
|
|
|
|
5
|
|
|
use DateTimeImmutable as DateTime; |
6
|
|
|
use DateTimeInterface; |
7
|
|
|
|
8
|
|
|
class User |
9
|
|
|
{ |
10
|
|
|
private $id; |
11
|
|
|
private $name; |
12
|
|
|
private $role; |
13
|
|
|
private $email; |
14
|
|
|
private $phone; |
15
|
|
|
private $created; |
16
|
|
|
private $updated; |
17
|
|
|
|
18
|
|
|
private $authenticated; |
19
|
|
|
|
20
|
|
|
private static $roles = [ |
21
|
|
|
"employee" => true, |
22
|
|
|
"manager" => true |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
public static function employeeNamedWithEmail($name, $email) |
26
|
|
|
{ |
27
|
|
|
return new User(null, $name, "employee", $email); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function managerNamedWithEmail($name, $email) |
31
|
|
|
{ |
32
|
|
|
return new User(null, $name, "manager", $email); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function __construct($id, $name, $role, $email = null, $phone = null, |
36
|
|
|
DateTimeInterface $created = null, DateTimeInterface $updated = null) |
37
|
|
|
{ |
38
|
|
|
if (isset($id) && ! is_int($id)) { |
39
|
|
|
throw new \InvalidArgumentException("The id must be an integer"); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (empty($email) && empty($phone)) { |
43
|
|
|
throw new \InvalidArgumentException("At least one of phone or email must be defined"); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (! isset(self::$roles[$role])) { |
47
|
|
|
throw new \InvalidArgumentException("The role must be either employee or manager"); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->id = $id; |
51
|
|
|
$this->name = $name; |
52
|
|
|
$this->role = $role; |
53
|
|
|
$this->email = $email; |
54
|
|
|
$this->phone = $phone; |
55
|
|
|
|
56
|
|
|
$this->created = $created ?: new DateTime(); |
57
|
|
|
$this->updated = $updated ?: new DateTime(); |
58
|
|
|
|
59
|
|
|
$this->authenticated = false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getId() |
63
|
|
|
{ |
64
|
|
|
return $this->id; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getName() |
68
|
|
|
{ |
69
|
|
|
return $this->name; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getRole() |
73
|
|
|
{ |
74
|
|
|
return $this->role; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getEmail() |
78
|
|
|
{ |
79
|
|
|
return $this->email; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getPhone() |
83
|
|
|
{ |
84
|
|
|
return $this->phone; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getCreated() |
88
|
|
|
{ |
89
|
|
|
return $this->created; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getUpdated() |
93
|
|
|
{ |
94
|
|
|
return $this->updated; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function isAuthenticated() |
98
|
|
|
{ |
99
|
|
|
return $this->authenticated; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function authenticate() |
103
|
|
|
{ |
104
|
|
|
$this->authenticated = true; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|