|
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 organizationId():?string |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->organizationId; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function belongsTo(string $organisationId):bool |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->organizationId === $organisationId; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function create(string $passwordHashed) |
|
54
|
|
|
{ |
|
55
|
|
|
app(UserRepository::class)->add($this, $passwordHashed); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function joinsOrganization(string $organizationId) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->organizationId = $organizationId; |
|
61
|
|
|
app(UserRepository::class)->update($this); |
|
62
|
|
|
Mail::to($this->email())->send(new UserJoinsOrganizationToUser()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function grantAsAdmin() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->roles = array_merge($this->roles, ['admin']); |
|
68
|
|
|
app(UserRepository::class)->update($this); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function revokeAsAdmin() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->roles = []; |
|
74
|
|
|
app(UserRepository::class)->update($this); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function update(string $email, string $firstname, string $lastname, string $pathPicture, string $ext = 'jpg') |
|
78
|
|
|
{ |
|
79
|
|
|
$this->email = $email; |
|
80
|
|
|
$this->firstname = $firstname; |
|
81
|
|
|
$this->lastname = $lastname; |
|
82
|
|
|
if($pathPicture !== "") { |
|
83
|
|
|
$picture = new Picture($pathPicture); |
|
84
|
|
|
$picture->resize('app/public/users/' . $this->id . '.' . $ext); |
|
85
|
|
|
$this->pathPicture = 'app/public/users/' . $this->id . '.' . $ext; |
|
86
|
|
|
} |
|
87
|
|
|
app(UserRepository::class)->update($this); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function delete() |
|
91
|
|
|
{ |
|
92
|
|
|
app(UserRepository::class)->delete($this->id); |
|
93
|
|
|
event(new UserDeleted($this->id, $this->organizationId)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function toArray() |
|
97
|
|
|
{ |
|
98
|
|
|
$urlPicture = $this->pathPicture != "" ? asset('storage/'.str_replace('app/public/', '', $this->pathPicture)) : null; |
|
99
|
|
|
return [ |
|
100
|
|
|
'uuid' => $this->id, |
|
101
|
|
|
'email' => $this->email, |
|
102
|
|
|
'firstname' => $this->firstname, |
|
103
|
|
|
'lastname' => $this->lastname, |
|
104
|
|
|
'organization_id' => $this->organizationId, |
|
105
|
|
|
'path_picture' => $this->pathPicture, |
|
106
|
|
|
'url_picture' => $urlPicture, |
|
107
|
|
|
'roles' => $this->roles |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|