Completed
Push — master ( 029fc1...afa99c )
by guillaume
12:29 queued 06:09
created

User::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 8
dl 0
loc 19
rs 10
c 1
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 addProvider(string $provider, string $providerId)
77
    {
78
        $this->providers[$provider] = $providerId;
79
        app(UserRepository::class)->updateProviders($this);
80
    }
81
82
    public function belongsTo(string $organisationId):bool
83
    {
84
        return $this->organizationId === $organisationId;
85
    }
86
87
    public function create(string $passwordHashed = null, Picture $picture = null)
88
    {
89
        if(isset($picture)) {
90
            $picture->resize('app/public/users/' . $this->id);
91
            $this->pathPicture = $picture->relativePath();
92
        }
93
        app(UserRepository::class)->add($this, $passwordHashed);
94
    }
95
96
    public function joinsOrganization(string $organizationId)
97
    {
98
        $this->organizationId = $organizationId;
99
        app(UserRepository::class)->update($this);
100
        Mail::to($this->email())->send(new UserJoinsOrganizationToUser());
101
    }
102
103
    public function grantAsAdmin()
104
    {
105
        $this->roles = array_merge($this->roles, ['admin']);
106
        app(UserRepository::class)->update($this);
107
    }
108
109
    public function revokeAsAdmin()
110
    {
111
        $this->roles = [];
112
        app(UserRepository::class)->update($this);
113
    }
114
115
    public function leaveOrganization()
116
    {
117
        $this->organizationId = null;
118
        app(UserRepository::class)->update($this);
119
        event(new UserLeaveOrganization());
120
    }
121
122
    public function isAdmin():bool
123
    {
124
        return in_array('admin', $this->roles);
125
    }
126
127
    public function update(string $email, string $firstname, string $lastname, string $pathPicture, string $ext = 'jpg')
128
    {
129
        $this->email = $email;
130
        $this->firstname = $firstname;
131
        $this->lastname = $lastname;
132
        if($pathPicture !== "") {
133
            $picture = new Picture($pathPicture);
134
            $picture->resize('app/public/users/' . $this->id . '.' . $ext);
135
            $this->pathPicture = 'app/public/users/' . $this->id . '.' . $ext;
136
        }
137
        app(UserRepository::class)->update($this);
138
    }
139
140
    public function delete()
141
    {
142
        app(UserRepository::class)->delete($this->id);
143
        event(new UserDeleted($this->id, $this->organizationId));
144
    }
145
146
    public function toDto():UserDto
147
    {
148
        $identity = new Identity($this->id, $this->email, $this->firstname, $this->lastname, $this->pathPicture);
149
        $state = new State($this->organizationId, null, true);
150
        return new UserDto($identity, $state);
151
    }
152
153
    public function toArray()
154
    {
155
        $urlPicture = $this->pathPicture != "" ? asset('storage/'.str_replace('app/public/', '', $this->pathPicture)) : null;
156
        return [
157
            'uuid' => $this->id,
158
            'email' => $this->email,
159
            'firstname' => $this->firstname,
160
            'lastname' => $this->lastname,
161
            'organization_id' => $this->organizationId,
162
            'path_picture' => $this->pathPicture,
163
            'url_picture' => $urlPicture,
164
            'roles' => $this->roles,
165
            'providers' => $this->providers
166
        ];
167
    }
168
}
169