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