1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace App\Src\UseCases\Domain; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use App\Events\UserDeleted; |
8
|
|
|
use App\Events\UserLeaveOrganization; |
9
|
|
|
use App\Mail\UserJoinsOrganizationToUser; |
10
|
|
|
use App\Src\UseCases\Domain\Ports\UserRepository; |
11
|
|
|
use Illuminate\Support\Facades\Mail; |
12
|
|
|
|
13
|
|
|
class User |
14
|
|
|
{ |
15
|
|
|
private $id; |
16
|
|
|
private $email; |
17
|
|
|
private $lastname; |
18
|
|
|
private $firstname; |
19
|
|
|
private $organizationId; |
20
|
|
|
private $pathPicture; |
21
|
|
|
private $roles; |
22
|
|
|
|
23
|
|
|
public function __construct(string $id, string $email, string $firstname, string $lastname, string $organizationId = null, string $pathPicture = null, array $roles = []) |
24
|
|
|
{ |
25
|
|
|
$this->id = $id; |
26
|
|
|
$this->email = $email; |
27
|
|
|
$this->lastname = $lastname; |
28
|
|
|
$this->firstname = $firstname; |
29
|
|
|
$this->organizationId = $organizationId; |
30
|
|
|
$this->pathPicture = $pathPicture; |
31
|
|
|
$this->roles = $roles; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function email():string |
35
|
|
|
{ |
36
|
|
|
return $this->email; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function id():string |
40
|
|
|
{ |
41
|
|
|
return $this->id; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function fullname():string |
45
|
|
|
{ |
46
|
|
|
return ucfirst($this->firstname).' '.ucfirst($this->lastname); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function organizationId():?string |
50
|
|
|
{ |
51
|
|
|
return $this->organizationId; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function belongsTo(string $organisationId):bool |
55
|
|
|
{ |
56
|
|
|
return $this->organizationId === $organisationId; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function create(string $passwordHashed) |
60
|
|
|
{ |
61
|
|
|
app(UserRepository::class)->add($this, $passwordHashed); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function joinsOrganization(string $organizationId) |
65
|
|
|
{ |
66
|
|
|
$this->organizationId = $organizationId; |
67
|
|
|
app(UserRepository::class)->update($this); |
68
|
|
|
Mail::to($this->email())->send(new UserJoinsOrganizationToUser()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function grantAsAdmin() |
72
|
|
|
{ |
73
|
|
|
$this->roles = array_merge($this->roles, ['admin']); |
74
|
|
|
app(UserRepository::class)->update($this); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function revokeAsAdmin() |
78
|
|
|
{ |
79
|
|
|
$this->roles = []; |
80
|
|
|
app(UserRepository::class)->update($this); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function leaveOrganization() |
84
|
|
|
{ |
85
|
|
|
$this->organizationId = null; |
86
|
|
|
app(UserRepository::class)->update($this); |
87
|
|
|
event(new UserLeaveOrganization()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function isAdmin():bool |
91
|
|
|
{ |
92
|
|
|
return in_array('admin', $this->roles); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function update(string $email, string $firstname, string $lastname, string $pathPicture, string $ext = 'jpg') |
96
|
|
|
{ |
97
|
|
|
$this->email = $email; |
98
|
|
|
$this->firstname = $firstname; |
99
|
|
|
$this->lastname = $lastname; |
100
|
|
|
if($pathPicture !== "") { |
101
|
|
|
$picture = new Picture($pathPicture); |
102
|
|
|
$picture->resize('app/public/users/' . $this->id . '.' . $ext); |
103
|
|
|
$this->pathPicture = 'app/public/users/' . $this->id . '.' . $ext; |
104
|
|
|
} |
105
|
|
|
app(UserRepository::class)->update($this); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function delete() |
109
|
|
|
{ |
110
|
|
|
app(UserRepository::class)->delete($this->id); |
111
|
|
|
event(new UserDeleted($this->id, $this->organizationId)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function toArray() |
115
|
|
|
{ |
116
|
|
|
$urlPicture = $this->pathPicture != "" ? asset('storage/'.str_replace('app/public/', '', $this->pathPicture)) : null; |
117
|
|
|
return [ |
118
|
|
|
'uuid' => $this->id, |
119
|
|
|
'email' => $this->email, |
120
|
|
|
'firstname' => $this->firstname, |
121
|
|
|
'lastname' => $this->lastname, |
122
|
|
|
'organization_id' => $this->organizationId, |
123
|
|
|
'path_picture' => $this->pathPicture, |
124
|
|
|
'url_picture' => $urlPicture, |
125
|
|
|
'roles' => $this->roles |
126
|
|
|
]; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|