UserService   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 18.52%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 57
ccs 5
cts 27
cp 0.1852
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 3 1
A update() 0 6 1
A create() 0 7 1
A newUser() 0 6 1
A find() 0 7 2
A setRoles() 0 6 2
A all() 0 3 1
A findByIdentityId() 0 3 1
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 Modules\User\Events\UserRegisteredEvent;
15
16
class UserService implements UserServiceContract
17
{
18
    public function all()
19
    {
20
        return User::all();
21
    }
22
23
    public function find($id): ?User
24
    {
25
        if ($id instanceof User) {
26
            return $id;
27
        }
28
29
        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...
30
    }
31
32
    public function findByIdentityId($id): ?User
33
    {
34
        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...
35
    }
36
37
    public function update($id, $data): ?User
38
    {
39
        $user = $this->find($id);
40
        $user->update($data);
41
42
        return $user;
43
    }
44
45 1
    public function create($data): User
46
    {
47 1
        $user = User::create($data);
48 1
        $user->assignRole(Role::MEMBER);
49 1
        event(new UserRegisteredEvent($user));
50
51 1
        return $user;
52
    }
53
54
    public function delete($id): bool
55
    {
56
        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...
57
    }
58
59
    public function newUser($data): User
60
    {
61
        $user = new User($data);
62
        $user->assignRole(Role::MEMBER);
63
64
        return $user;
65
    }
66
67
    public function setRoles($id, array $roles): void
68
    {
69
        if (! in_array(Role::MEMBER, $roles)) {
70
            $roles[] = Role::MEMBER;
71
        }
72
        $this->find($id)->syncRoles($roles);
73
    }
74
}
75