Test Setup Failed
Push — master ( 6863da...811519 )
by guillaume
27:05 queued 21:33
created

User::toDto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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