Passed
Push — master ( 4b0d05...471523 )
by Arthur
103:49 queued 98:11
created

UserService::setRoles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 04.10.18
6
 * Time: 16:17.
7
 */
8
9
namespace Modules\User\Services;
10
11
use Modules\Authorization\Entities\Role;
12
use Modules\User\Contracts\UserServiceContract;
13
use Modules\User\Entities\User;
14
15
class UserService implements UserServiceContract
16
{
17 1
    public function all()
18
    {
19 1
        return User::all();
20
    }
21
22 3
    public function find($id): ?User
23
    {
24 3
        if ($id instanceof User) {
25 2
            return $id;
26
        }
27
28 1
        return User::find($id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Modules\User\Entities\User::find($id) could return the type Illuminate\Database\Eloquent\Collection which is incompatible with the type-hinted return Modules\User\Entities\User|null. Consider adding an additional type-check to rule them out.
Loading history...
29
    }
30
31 31
    public function findByIdentityId($id): ?User
32
    {
33 31
        return User::cache()->findBy('identity_id', $id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Modules\User\Enti...dBy('identity_id', $id) could return the type Eloquent which includes types incompatible with the type-hinted return Modules\User\Entities\User|null. Consider adding an additional type-check to rule them out.
Loading history...
34
    }
35
36
    public function update($id, $data): ?User
37
    {
38
        $user = $this->find($id);
39
        $user->update($data);
40
        return $user;
41
    }
42
43 8
    public function create($data): User
44
    {
45 8
        $user = User::create($data);
46 8
        $user->assignRole(Role::USER);
47
48 8
        return $user;
49
    }
50
51
    public function delete($id): bool
52
    {
53
        return User::destroy($id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Modules\User\Entities\User::destroy($id) returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
54
    }
55
56 31
    public function newUser($data): User
57
    {
58 31
        $user = new User($data);
59 31
        $user->assignRole(Role::USER);
60 31
        return $user;
61
    }
62
63 1
    public function setRoles($id, array $roles): void
64
    {
65 1
        if (!in_array(Role::USER, $roles)) {
66 1
            $roles[] = Role::USER;
67
        }
68 1
        $this->find($id)->syncRoles($roles);
69 1
    }
70
}
71