UsherUserRepository::create()   B
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 29
rs 8.8571
cc 3
eloc 16
nc 2
nop 1
1
<?php namespace Modules\User\Repositories\Usher;
2
3
use Maatwebsite\Usher\Contracts\Roles\RoleRepository;
4
use Maatwebsite\Usher\Contracts\Users\UserRepository as UsherUserRepo;
5
use Maatwebsite\Usher\Domain\Users\Embeddables\Email;
6
use Maatwebsite\Usher\Domain\Users\Embeddables\Name;
7
use Maatwebsite\Usher\Domain\Users\Embeddables\Password;
8
use Modules\User\Exceptions\UserNotFoundException;
9
use Modules\User\Repositories\UserRepository;
10
11
class UsherUserRepository implements UserRepository
12
{
13
    /**
14
     * @var UsherUserRepo
15
     */
16
    protected $user;
17
18
    /**
19
     * @var RoleRepository
20
     */
21
    protected $role;
22
23
    /**
24
     * @param UsherUserRepo  $user
25
     * @param RoleRepository $role
26
     */
27
    public function __construct(UsherUserRepo $user, RoleRepository $role)
28
    {
29
        $this->user = $user;
30
        $this->role = $role;
31
    }
32
33
    /**
34
     * Returns all the users
35
     * @return object
36
     */
37
    public function all()
38
    {
39
        return $this->user->all();
40
    }
41
42
    /**
43
     * Create a user resource
44
     * @param $data
45
     * @return mixed
46
     */
47
    public function create(array $data)
48
    {
49
        $entity = $this->user->getClassName();
50
        $user = new $entity;
51
52
        $name = new Name(
53
            $data['first_name'],
54
            $data['last_name']
55
        );
56
57
        $email = new Email(
58
            $data['email']
59
        );
60
61
        $password = new Password(
62
            $data['password']
63
        );
64
65
        if (isset($data['permissions']) && !empty($data['permissions'])) {
66
            $user->setPermissions($data['permissions']);
67
        }
68
69
        $user = $user->register($name, $email, $password);
70
71
        $this->user->persist($user);
72
        $this->user->flush();
73
74
        return $user;
75
    }
76
77
    /**
78
     * Create a user and assign roles to it
79
     * @param  array $data
80
     * @param  array $roles
81
     * @param bool $activated
82
     * @return mixed
83
     */
84
    public function createWithRoles($data, $roles, $activated = false)
85
    {
86
        $user = $this->create((array) $data);
87
88
        if (!empty($roles) && is_array($roles)) {
89
            foreach ($roles as $id) {
90
                $role = $this->role->find($id);
91
                $user->assignRole($role);
92
            }
93
        }
94
95
        $this->user->persist($user);
96
        $this->user->flush();
97
98
        return $user;
99
    }
100
101
    /**
102
     * Find a user by its ID
103
     * @param $id
104
     * @return mixed
105
     */
106
    public function find($id)
107
    {
108
        return $this->user->find($id);
109
    }
110
111
    /**
112
     * Update a user
113
     * @param $user
114
     * @param $data
115
     * @return mixed
116
     */
117
    public function update($user, $data)
118
    {
119
        $name = new Name(
120
            $data['first_name'],
121
            $data['last_name']
122
        );
123
124
        $email = new Email(
125
            $data['email']
126
        );
127
128
        $password = null;
129
        if (isset($data['password'])) {
130
            $password = new Password(
131
                $data['password']
132
            );
133
134
            if ($password->equals($user->getPassword())) {
135
                $password = null;
136
            }
137
        }
138
139
        if (isset($data['permissions']) && !empty($data['permissions'])) {
140
            $user->setPermissions($data['permissions']);
141
        }
142
143
        $user = $user->update($name, $email, $password);
144
145
        $this->user->persist($user);
146
        $this->user->flush();
147
148
        return $user;
149
    }
150
151
    /**
152
     * @param $userId
153
     * @param $data
154
     * @param $roles
155
     * @internal param $user
156
     * @return mixed
157
     */
158
    public function updateAndSyncRoles($userId, $data, $roles)
159
    {
160
        $user = $this->user->find($userId);
161
        $user = $this->update($user, $data);
162
163
        if (!empty($roles) && is_array($roles)) {
164
            foreach ($roles as $id) {
165
                $role = $this->role->find($id);
166
                $user->assignRole($role);
167
            }
168
        }
169
170
        $this->user->persist($user);
171
        $this->user->flush();
172
173
        return $user;
174
    }
175
176
    /**
177
     * Deletes a user
178
     * @param $id
179
     * @throws UserNotFoundException
180
     * @return mixed
181
     */
182
    public function delete($id)
183
    {
184
        $user = $this->find($id);
185
186
        return $this->user->delete($user);
187
    }
188
189
    /**
190
     * Find a user by its credentials
191
     * @param  array $credentials
192
     * @return mixed
193
     */
194
    public function findByCredentials(array $credentials)
195
    {
196
        return $this->user->findByCredentials($credentials);
197
    }
198
}
199