Passed
Push — master ( 956624...05ed8c )
by Bertrand
08:49
created

User::updateRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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 App\Src\UseCases\Domain\Shared\Model\Picture;
12
use App\Src\UseCases\Domain\Users\Identity;
13
use App\Src\UseCases\Domain\Users\State;
14
use App\Src\UseCases\Domain\Users\UserDto;
15
use Illuminate\Support\Facades\Mail;
16
17
class User
18
{
19
    private $id;
20
    private $email;
21
    private $lastname;
22
    private $firstname;
23
    private $organizationId;
24
    private $pathPicture;
25
    private $roles;
26
    private $providers;
27
28
    public function __construct(
29
        string $id,
30
        string $email,
31
        string $firstname,
32
        string $lastname,
33
        string $organizationId = null,
34
        string $pathPicture = null,
35
        array $roles = [],
36
        array $providers = []
37
    )
38
    {
39
        $this->id = $id;
40
        $this->email = $email;
41
        $this->lastname = $lastname;
42
        $this->firstname = $firstname;
43
        $this->organizationId = $organizationId;
44
        $this->pathPicture = $pathPicture;
45
        $this->roles = $roles;
46
        $this->providers = $providers;
47
    }
48
49
    public function email():string
50
    {
51
        return $this->email;
52
    }
53
54
    public function id():string
55
    {
56
        return $this->id;
57
    }
58
59
    public function fullname():string
60
    {
61
        return $this->firstname.' '.$this->lastname;
62
    }
63
64
    public function organizationId():?string
65
    {
66
        return $this->organizationId;
67
    }
68
69
    public function provider(string $provider, string $providerId):bool
70
    {
71
        if(isset($this->providers[$provider]) && $this->providers[$provider] == $providerId){
72
            return true;
73
        }
74
        return false;
75
    }
76
77
    public function addProvider(string $provider, string $providerId)
78
    {
79
        $this->providers[$provider] = $providerId;
80
        app(UserRepository::class)->updateProviders($this);
81
    }
82
83
    public function belongsTo(string $organisationId):bool
84
    {
85
        return $this->organizationId === $organisationId;
86
    }
87
88
    public function create(string $passwordHashed = null, Picture $picture = null)
89
    {
90
        if(isset($picture)) {
91
            $picture->resize('app/public/users/' . $this->id);
92
            $this->pathPicture = $picture->relativePath();
93
        }
94
        app(UserRepository::class)->add($this, $passwordHashed);
95
    }
96
97
    public function joinsOrganization(string $organizationId)
98
    {
99
        $this->organizationId = $organizationId;
100
        app(UserRepository::class)->update($this);
101
        Mail::to($this->email())->send(new UserJoinsOrganizationToUser());
102
    }
103
104
    public function grantAsAdmin()
105
    {
106
        $this->roles = array_merge($this->roles, ['admin']);
107
        app(UserRepository::class)->update($this);
108
    }
109
110
    public function revokeAsAdmin()
111
    {
112
        $this->roles = [];
113
        app(UserRepository::class)->update($this);
114
    }
115
116
    public function leaveOrganization()
117
    {
118
        $this->organizationId = null;
119
        app(UserRepository::class)->update($this);
120
        event(new UserLeaveOrganization());
121
    }
122
123
    public function isAdmin():bool
124
    {
125
        return in_array('admin', $this->roles);
126
    }
127
128
    public function update(string $email, string $firstname, string $lastname, string $pathPicture = "", string $ext = 'jpg')
129
    {
130
        $this->email = $email;
131
        $this->firstname = $firstname;
132
        $this->lastname = $lastname;
133
        if($pathPicture !== "") {
134
            $picture = new Picture($pathPicture);
135
            $picture->resize('app/public/users/' . $this->id . '.' . $ext);
136
            $this->pathPicture = 'app/public/users/' . $this->id . '.' . $ext;
137
        }
138
        app(UserRepository::class)->update($this);
139
    }
140
141
    public function updateAvatar(string $pathPicture, string $ext = 'jpg')
142
    {
143
        if($pathPicture !== "") {
144
            $picture = new Picture($pathPicture);
145
            $picture->resize('app/public/users/' . $this->id . '.' . $ext);
146
            $this->pathPicture = 'app/public/users/' . $this->id . '.' . $ext;
147
        }
148
        app(UserRepository::class)->update($this);
149
        return $this->pathPicture;
150
    }
151
152
    public function delete()
153
    {
154
        app(UserRepository::class)->delete($this->id);
155
        event(new UserDeleted($this->id, $this->organizationId));
156
    }
157
158
    public function toDto():UserDto
159
    {
160
        $identity = new Identity($this->id, $this->email, $this->firstname, $this->lastname, $this->pathPicture);
161
        $state = new State($this->organizationId, null, true);
162
        return new UserDto($identity, $state);
163
    }
164
165
    public function addRole(string $role)
166
    {
167
        $this->roles[] = $role;
168
        app(UserRepository::class)->update($this);
169
    }
170
171
    public function updateRole(string $role)
172
    {
173
        $this->roles = [$role];
174
        app(UserRepository::class)->update($this);
175
    }
176
177
    public function toArray()
178
    {
179
        $urlPicture = $this->pathPicture != "" ? asset('storage/'.str_replace('app/public/', '', $this->pathPicture)) : null;
180
        return [
181
            'uuid' => $this->id,
182
            'email' => $this->email,
183
            'firstname' => $this->firstname,
184
            'lastname' => $this->lastname,
185
            'organization_id' => $this->organizationId,
186
            'path_picture' => $this->pathPicture,
187
            'url_picture' => $urlPicture,
188
            'roles' => $this->roles,
189
            'providers' => $this->providers
190
        ];
191
    }
192
}
193