Passed
Push — master ( 2b2a7c...880107 )
by Christian
03:07 queued 12s
created

User::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Omatech\Mage\Core\Domains\Users;
4
5
use Omatech\Mage\Core\Domains\Users\Jobs\AllUser;
6
use Omatech\Mage\Core\Domains\Shared\Traits\FromArray;
7
use Omatech\Mage\Core\Domains\Roles\Contracts\RoleInterface;
8
use Omatech\Mage\Core\Domains\Users\Contracts\UserInterface;
9
use Omatech\Mage\Core\Domains\Users\Features\FindOrFailUser;
10
use Omatech\Mage\Core\Domains\Shared\Contracts\GetAllInterface;
11
use Omatech\Mage\Core\Domains\Users\Features\UpdateOrCreateUser;
12
use Omatech\Mage\Core\Domains\Users\Features\ExistsAndDeleteUser;
13
use Omatech\Mage\Core\Domains\Roles\Exceptions\RoleIsNotSavedException;
14
use Omatech\Mage\Core\Domains\Permissions\Contracts\PermissionInterface;
15
use Omatech\Mage\Core\Domains\Permissions\Exceptions\PermissionIsNotSavedException;
16
use Omatech\Mage\Core\Domains\Permissions\PermissionModel;
17
18
class User implements UserInterface
19
{
20
    use FromArray;
21
22
    private $id;
23
    private $name;
24
    private $language;
25
    private $email;
26
    private $emailVerifiedAt;
27
    private $password;
28
    private $rememberToken;
29
    private $createdAt;
30
    private $updatedAt;
31
    private $permissions = [];
32
    private $roles = [];
33
34
    /**
35
     * @return int|null
36
     */
37 17
    public function getId(): ?int
38
    {
39 17
        return $this->id;
40
    }
41
42
    /**
43
     * @param int $id
44
     * @return User
45
     */
46 17
    public function setId(int $id): self
47
    {
48 17
        $this->id = $id;
49
50 17
        return $this;
51
    }
52
53
    /**
54
     * @return string
55
     */
56 17
    public function getName(): string
57
    {
58 17
        return $this->name;
59
    }
60
61
    /**
62
     * @param string $name
63
     * @return User
64
     */
65 19
    public function setName(string $name): self
66
    {
67 19
        $this->name = $name;
68
69 19
        return $this;
70
    }
71
72
    /**
73
     * @return string
74
     */
75 17
    public function getLanguage(): string
76
    {
77 17
        return $this->language;
78
    }
79
80
    /**
81
     * @param string $language
82
     * @return User
83
     */
84 19
    public function setLanguage(string $language): self
85
    {
86 19
        $this->language = $language;
87
88 19
        return $this;
89
    }
90
91
    /**
92
     * @return string
93
     */
94 17
    public function getEmail(): string
95
    {
96 17
        return $this->email;
97
    }
98
99
    /**
100
     * @param string $email
101
     * @return User
102
     */
103 19
    public function setEmail(string $email): self
104
    {
105 19
        $this->email = $email;
106
107 19
        return $this;
108
    }
109
110
    /**
111
     * @return string|null
112
     */
113 17
    public function getEmailVerifiedAt(): ?string
114
    {
115 17
        return $this->emailVerifiedAt;
116
    }
117
118
    /**
119
     * @param string|null $emailVerifiedAt
120
     * @return User
121
     */
122 2
    public function setEmailVerifiedAt(?string $emailVerifiedAt): self
123
    {
124 2
        $this->emailVerifiedAt = $emailVerifiedAt;
125
126 2
        return $this;
127
    }
128
129
    /**
130
     * @return string
131
     */
132 17
    public function getPassword(): string
133
    {
134 17
        return $this->password;
135
    }
136
137
    /**
138
     * @param string $password
139
     * @return User
140
     */
141 19
    public function setPassword(string $password): self
142
    {
143 19
        $this->password = $password;
144
145 19
        return $this;
146
    }
147
148
149
    /**
150
     * @return string|null
151
     */
152 17
    public function getRememberToken(): ?string
153
    {
154 17
        return $this->rememberToken;
155
    }
156
157
    /**
158
     * @param string|null $rememberToken
159
     * @return User
160
     */
161 2
    public function setRememberToken(?string $rememberToken): self
162
    {
163 2
        $this->rememberToken = $rememberToken;
164
165 2
        return $this;
166
    }
167
168
    /**
169
     * @return string
170
     */
171 6
    public function getCreatedAt(): string
172
    {
173 6
        return $this->createdAt;
174
    }
175
176
    /**
177
     * @param string $createdAt
178
     * @return User
179
     */
180 17
    public function setCreatedAt(string $createdAt): self
181
    {
182 17
        $this->createdAt = $createdAt;
183
184 17
        return $this;
185
    }
186
187
    /**
188
     * @return string
189
     */
190 6
    public function getUpdatedAt(): string
191
    {
192 6
        return $this->updatedAt;
193
    }
194
195
    /**
196
     * @param string $updatedAt
197
     * @return User
198
     */
199 17
    public function setUpdatedAt(string $updatedAt): self
200
    {
201 17
        $this->updatedAt = $updatedAt;
202
203 17
        return $this;
204
    }
205
206
    /**
207
     * @return array
208
     */
209 18
    public function getPermissions(): array
210
    {
211 18
        return $this->permissions;
212
    }
213
214
    /**
215
     * @return array
216
     */
217 17
    public function getRoles(): array
218
    {
219 17
        return $this->roles;
220
    }
221
222
    /**
223
     * @return array
224
     */
225 17
    public function getPermissionsIds(): array
226
    {
227
        return array_map(static function (PermissionInterface $permission) {
228 6
            return $permission->getId();
229 17
        }, $this->getPermissions());
230
    }
231
232
    /**
233
     * @return array
234
     */
235 17
    public function getRolesIds(): array
236
    {
237
        return array_map(static function (RoleInterface $role) {
238 5
            return $role->getId();
239 17
        }, $this->getRoles());
240
    }
241
242
    /**
243
     * @param GetAllInterface $all
244
     * @return mixed
245
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
246
     */
247 1
    public static function all(GetAllInterface $all)
248
    {
249 1
        return app()->make(AllUser::class)->make($all);
250
    }
251
252
    /**
253
     * @param int $id
254
     * @return User
255
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
256
     */
257 3
    public static function find(int $id): self
258
    {
259 3
        return app()->make(FindOrFailUser::class)->make($id);
260
    }
261
262
    /**
263
     * @return bool
264
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
265
     */
266 17
    public function save(): bool
267
    {
268 17
        return app()->make(UpdateOrCreateUser::class)->make($this);
269
    }
270
271
    /**
272
     * @return bool
273
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
274
     */
275 2
    public function delete(): bool
276
    {
277 2
        return app()->make(ExistsAndDeleteUser::class)->make($this);
278
    }
279
280
    /**
281
     * @param PermissionInterface $permission
282
     * @return User
283
     * @throws PermissionIsNotSavedException
284
     */
285 1
    public function assignPermission(PermissionInterface $permission): self
286
    {
287 1
        $this->permissions = app()->make(PermissionModel::class)
288 1
            ->assignPermission($this->getPermissions(), $permission);
289
290 1
        return $this;
291
    }
292
293
    /**
294
     * @param array $permissions
295
     * @return User
296
     * @throws PermissionIsNotSavedException
297
     */
298 7
    public function assignPermissions(array $permissions): self
299
    {
300 7
        $this->permissions = app()->make(PermissionModel::class)
301 7
            ->assignPermissions($this->getPermissions(), $permissions);
302
303 6
        return $this;
304
    }
305
306
    /**
307
     * @param PermissionInterface $permission
308
     * @return User
309
     * @throws PermissionIsNotSavedException
310
     */
311 1
    public function removePermission(PermissionInterface $permission): self
312
    {
313 1
        $this->permissions = app()->make(PermissionModel::class)
314 1
            ->removePermission($this->getPermissions(), $permission);
315
316 1
        return $this;
317
    }
318
319
    /**
320
     * @param array $permissions
321
     * @return User
322
     * @throws PermissionIsNotSavedException
323
     */
324 2
    public function removePermissions(array $permissions): self
325
    {
326 2
        $this->permissions = app()->make(PermissionModel::class)
327 2
            ->removePermissions($this->getPermissions(), $permissions);
328
329 1
        return $this;
330
    }
331
332
    /**
333
     * @param RoleInterface $role
334
     * @return User
335
     * @throws RoleIsNotSavedException
336
     */
337 6
    public function assignRole(RoleInterface $role): self
338
    {
339 6
        if ($role->getId() === null) {
340 1
            throw new RoleIsNotSavedException;
341
        }
342
343 5
        if (!in_array($role, $this->getRoles(), true)) {
344 5
            $this->roles[] = $role;
345
        }
346
347 5
        return $this;
348
    }
349
350
    /**
351
     * @param array $roles
352
     * @return User
353
     * @throws RoleIsNotSavedException
354
     */
355 7
    public function assignRoles(array $roles): self
356
    {
357 7
        foreach ($roles as $role) {
358 6
            if ($role instanceof RoleInterface) {
359 6
                $this->assignRole($role);
360
            }
361
        }
362
363 6
        return $this;
364
    }
365
366
    /**
367
     * @param RoleInterface $role
368
     * @return User
369
     * @throws RoleIsNotSavedException
370
     */
371 2
    public function removeRole(RoleInterface $role): self
372
    {
373 2
        if ($role->getId() === null) {
374 1
            throw new RoleIsNotSavedException;
375
        }
376
377 1
        $this->roles = array_values(array_filter(
378 1
            $this->getRoles(),
379
            static function ($currentRole) use ($role) {
380 1
                return $currentRole !== $role;
381 1
            }
382
        ));
383
384 1
        return $this;
385
    }
386
387
    /**
388
     * @param array $roles
389
     * @return User
390
     * @throws RoleIsNotSavedException
391
     */
392 2
    public function removeRoles(array $roles): self
393
    {
394 2
        foreach ($roles as $role) {
395 2
            if ($role instanceof RoleInterface) {
396 2
                $this->removeRole($role);
397
            }
398
        }
399
400 1
        return $this;
401
    }
402
}
403