UserRepository::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4286
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
3
/**
4
 * @package     Dashboard
5
 * @author      Ian Olson <[email protected]>
6
 * @license     MIT
7
 * @copyright   2015, Laraflock
8
 * @link        https://github.com/laraflock
9
 */
10
11
namespace Laraflock\Dashboard\Repositories\User;
12
13
use Cartalyst\Sentinel\Sentinel;
14
use Cartalyst\Sentinel\Users\EloquentUser;
15
use Laraflock\Dashboard\Exceptions\RolesException;
16
use Laraflock\Dashboard\Exceptions\UsersException;
17
use Laraflock\Dashboard\Repositories\Auth\AuthRepositoryInterface;
18
use Laraflock\Dashboard\Repositories\Base\BaseRepository;
19
use Laraflock\Dashboard\Repositories\Role\RoleRepositoryInterface;
20
21
class UserRepository extends BaseRepository implements UserRepositoryInterface
22
{
23
    /**
24
     * Auth interface.
25
     *
26
     * @var \Laraflock\Dashboard\Repositories\Auth\AuthRepositoryInterface
27
     */
28
    protected $auth;
29
30
    /**
31
     * Role interface.
32
     *
33
     * @var \Laraflock\Dashboard\Repositories\Role\RoleRepositoryInterface
34
     */
35
    protected $role;
36
37
    /**
38
     * Sentinel instance.
39
     *
40
     * @var \Cartalyst\Sentinel\Sentinel
41
     */
42
    protected $sentinel;
43
44
    /**
45
     * User instance.
46
     *
47
     * @var \Cartalyst\Sentinel\Users\EloquentUser
48
     */
49
    protected $user;
50
51
    /**
52
     * The constructor.
53
     *
54
     * @param \Laraflock\Dashboard\Repositories\Auth\AuthRepositoryInterface $auth
55
     * @param \Laraflock\Dashboard\Repositories\Role\RoleRepositoryInterface $role
56
     * @param \Cartalyst\Sentinel\Sentinel                                   $sentinel
57
     * @param \Cartalyst\Sentinel\Users\EloquentUser                         $user
58
     */
59 110
    public function __construct(AuthRepositoryInterface $auth, RoleRepositoryInterface $role, Sentinel $sentinel, EloquentUser $user)
60
    {
61 110
        $this->auth     = $auth;
62 110
        $this->role     = $role;
63 110
        $this->sentinel = $sentinel;
64 110
        $this->user     = $user;
65 110
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    public function getAll()
71
    {
72
        return $this->user->all();
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78 1
    public function getAllWith($type)
79
    {
80 1
        return $this->user->with($type)
81 1
                          ->get();
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87 8
    public function getById($id)
88
    {
89 8
        return $this->user->find($id);
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95 2
    public function getByIdWith($id, $type)
96
    {
97 2
        return $this->user->with($type)
98 2
                          ->where('id', '=', $id)
99 2
                          ->first();
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105 12
    public function create(array $data, $validate = true)
106
    {
107 12
        $this->rules = [
108 12
          'email'                 => 'required|unique:users',
109 12
          'password'              => 'required|confirmed',
110 12
          'password_confirmation' => 'required',
111
        ];
112
113 12
        if ($validate) {
114 2
            $this->validate($data);
115 1
        }
116
117 12
        $this->auth->registerAndActivate($data);
118
119 12
        return;
120
    }
121
122
    /**
123
     * {@inheritDoc}
124
     */
125 6
    public function update(array $data, $id, $validate = true)
126
    {
127 6
        if (!$user = $this->getById($id)) {
128 2
            throw new UsersException(trans('dashboard::dashboard.errors.user.found'));
129
        }
130
131 4
        if ($user->email != $data['email']) {
132 3
            $this->rules['email'] = 'required|email|unique:users';
133 3
        } else {
134 1
            $this->rules['email'] = 'required|email';
135
        }
136
137 4
        if ($validate) {
138 4
            $this->validate($data);
139 2
        }
140
141 2
        $this->sentinel->update($user, $data);
142
143 2
        if (isset($data['role'])) {
144
145
            if (!$role = $this->role->getBySlug($data['role'])) {
146
                throw new RolesException(trans('dashboard::dashboard.errors.role.found'));
147
            }
148
149
            if (!$user->inRole($role)) {
150
                $role->users()
151
                     ->attach($user);
152
            }
153
        }
154
155 2
        $user->save();
156
157 2
        return;
158
    }
159
160
    /**
161
     * {@inheritDoc}
162
     */
163 3
    public function updatePassword(array $data, $validate = true)
164
    {
165 3
        $user = $this->auth->authenticate($data);
166
167 2
        $this->rules = [
168 2
          'new_password'              => 'required|confirmed',
169 2
          'new_password_confirmation' => 'required',
170
        ];
171
172 2
        if ($validate) {
173 2
            $this->validate($data);
174 1
        }
175
176
        $updatedData = [
177 1
          'password' => $data['new_password'],
178 1
        ];
179
180 1
        $this->sentinel->update($user, $updatedData);
181
182 1
        return;
183
    }
184
185
    /**
186
     * {@inheritDoc}
187
     */
188 2
    public function delete($id)
189
    {
190 2
        if (!$user = $this->getById($id)) {
191 1
            throw new UsersException(trans('dashboard::dashboard.errors.user.found'));
192
        }
193
194 1
        $user->delete();
195
196 1
        return;
197
    }
198
}