Passed
Push — master ( f51c1b...16736d )
by Arthur
05:36
created

UserService::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15
16
class UserService implements UserServiceContract
17 30
{
18
    public function all()
19 30
    {
20
        return User::all();
21
    }
22
23
    public function find($id): ?User
24
    {
25
        if ($id instanceof User)
26
            return $id;
27
28
        $user = User::find($id);
29
30
        if ($user === null)
31
            throw new NotFoundHttpException();
32
33
        return $user;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $user 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...
34
    }
35
36
    public function findByIdentityId($id): ?User
37
    {
38
        return User::where('identity_id', $id)->first();
39
    }
40
41
    public function update($id, $data): ?User
42
    {
43 30
        $user = $this->find($id);
44
        $user->update($data);
45 30
46
        return $user;
47
    }
48
49
    public function create($data): User
50
    {
51
        $user = User::create($data);
52
        $user->assignRole(Role::USER);
53
54
        return $user;
55
    }
56
57
    public function delete($id): bool
58
    {
59
        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...
60
    }
61
62
    public function newUser($data): User
63
    {
64
        $user = new User($data);
65
        $user->assignRole(Role::USER);
66
67
        return $user;
68
    }
69
70
    public function setRoles($id, array $roles): void
71
    {
72
        if (!in_array(Role::USER, $roles))
73
            $roles[] = Role::USER;
74
        $this->find($id)->syncRoles($roles);
75
    }
76
}
77