SentryUserRepository::createWithRoles()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.2
c 1
b 0
f 0
cc 4
eloc 8
nc 6
nop 3
1
<?php namespace Modules\User\Repositories\Sentry;
2
3
use Cartalyst\Sentry\Facades\Laravel\Sentry;
4
use Cartalyst\Sentry\Users\UserNotFoundException;
5
use Modules\User\Exceptions\UserNotFoundException as BaseUserNotFoundException;
6
use Modules\User\Repositories\UserRepository;
7
8
class SentryUserRepository implements UserRepository
9
{
10
    /**
11
     * Returns all the users
12
     * @return object
13
     */
14
    public function all()
15
    {
16
        return Sentry::findAllUsers();
17
    }
18
19
    /**
20
     * Create a user resource
21
     * @param  array $data
22
     * @return mixed
23
     */
24
    public function create(array $data)
25
    {
26
        return Sentry::createUser($data);
27
    }
28
29
    /**
30
     * Create a user and assign roles to it
31
     * @param array $data
32
     * @param array $roles
33
     * @param bool  $activated
34
     */
35
    public function createWithRoles($data, $roles, $activated = false)
36
    {
37
        $user = Sentry::createUser(array_merge($data, ['activated' => $activated]));
38
        $user->activated = $activated ? 1 : 0;
39
        $user->save();
40
        if (!empty($roles)) {
41
            foreach ($roles as $roleId) {
42
                $group = Sentry::findGroupById($roleId);
43
                $user->addGroup($group);
44
            }
45
        }
46
    }
47
48
    /**
49
     * Find a user by its ID
50
     * @param $id
51
     * @return mixed
52
     */
53
    public function find($id)
54
    {
55
        return Sentry::findUserById($id);
56
    }
57
58
    /**
59
     * Update a user
60
     * @param $user
61
     * @param $data
62
     * @return mixed
63
     */
64
    public function update($user, $data)
65
    {
66
        return $user->update($data);
67
    }
68
69
    /**
70
     * Update a user and sync its roles
71
     * @param  int   $userId
72
     * @param $data
73
     * @param $roles
74
     * @return mixed
75
     */
76
    public function updateAndSyncRoles($userId, $data, $roles)
77
    {
78
        $user = Sentry::findUserById($userId);
79
80
        $this->checkForNewPassword($data);
81
82
        $user->update($data);
83
84
        if (!empty($roles)) {
85
            // Get the user roles
86
            $userRoles = $user->groups()->get()->toArray();
87
            $cleanedRoles = [];
88
            foreach ($userRoles as $role) {
89
                $cleanedRoles[$role['id']] = $role;
90
            }
91
            // Set the new roles
92
            foreach ($roles as $roleId) {
93
                if (isset($cleanedRoles[$roleId])) {
94
                    unset($cleanedRoles[$roleId]);
95
                }
96
                $group = Sentry::findGroupById($roleId);
97
                $user->addGroup($group);
98
            }
99
            // Unset the unchecked roles
100
            foreach ($cleanedRoles as $roleId => $role) {
101
                $group = Sentry::findGroupById($roleId);
102
                $user->removeGroup($group);
103
            }
104
        }
105
    }
106
107
    /**
108
     * Deletes a user
109
     * @param $id
110
     * @return mixed
111
     * @throws UserNotFoundException
112
     */
113
    public function delete($id)
114
    {
115
        if ($user = Sentry::findUserById($id)) {
116
            return $user->delete();
117
        };
118
        throw new UserNotFoundException();
119
    }
120
121
    /**
122
     * Find a user by its credentials
123
     * @param  array $credentials
124
     * @return mixed
125
     * @throws BaseUserNotFoundException
126
     */
127
    public function findByCredentials(array $credentials)
128
    {
129
        try {
130
            $user = Sentry::findUserByCredentials($credentials);
131
        } catch (\Exception $e) {
132
            throw new BaseUserNotFoundException();
133
        }
134
135
        return $user;
136
    }
137
138
    /**
139
     * Check if there is a new password given
140
     * If not, unset the password field
141
     * @param array $data
142
     */
143
    private function checkForNewPassword(array &$data)
144
    {
145
        if (! $data['password']) {
146
            unset($data['password']);
147
        }
148
    }
149
}
150