Completed
Push — master ( 3f0650...a210b6 )
by Ryan
02:07
created

UserModel   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 352
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 22
Bugs 0 Features 1
Metric Value
wmc 45
c 22
b 0
f 1
lcom 1
cbo 1
dl 0
loc 352
rs 8.3673

24 Methods

Rating   Name   Duplication   Size   Complexity  
A getEmail() 0 4 1
A getUsername() 0 4 1
A getDisplayName() 0 4 1
A getFirstName() 0 4 1
A getLastName() 0 4 1
A setPassword() 0 6 1
A getRoles() 0 4 1
B hasRole() 0 19 5
B hasAnyRole() 0 18 5
A isAdmin() 0 11 3
A getPermissions() 0 4 1
A setPermissions() 0 6 1
C hasPermission() 0 22 7
A hasAnyPermission() 0 10 3
A setPasswordAttribute() 0 4 1
A isDeletable() 0 14 4
A isActivated() 0 4 1
A isEnabled() 0 4 1
A getResetCode() 0 4 1
A setResetCode() 0 6 1
A getActivationCode() 0 4 1
A setActivationCode() 0 6 1
A name() 0 4 1
A attachRole() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like UserModel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use UserModel, and based on these observations, apply Extract Interface, too.

1
<?php namespace Anomaly\UsersModule\User;
2
3
use Anomaly\Streams\Platform\Entry\EntryCollection;
4
use Anomaly\Streams\Platform\Model\Users\UsersUsersEntryModel;
5
use Anomaly\Streams\Platform\Support\Collection;
6
use Anomaly\UsersModule\Role\Command\GetRole;
7
use Anomaly\UsersModule\Role\Contract\RoleInterface;
8
use Anomaly\UsersModule\User\Contract\UserInterface;
9
use Illuminate\Auth\Authenticatable;
10
11
/**
12
 * Class UserModel
13
 *
14
 * @link          http://anomaly.is/streams-platform
15
 * @author        AnomalyLabs, Inc. <[email protected]>
16
 * @author        Ryan Thompson <[email protected]>
17
 * @package       Anomaly\UsersModule\User
18
 */
19
class UserModel extends UsersUsersEntryModel implements UserInterface, \Illuminate\Contracts\Auth\Authenticatable
0 ignored issues
show
Bug introduced by
There is one abstract method roles in this class; you could implement it, or declare this class as abstract.
Loading history...
20
{
21
22
    use Authenticatable;
23
24
    /**
25
     * The eager loaded relationships.
26
     *
27
     * @var array
28
     */
29
    protected $with = [
30
        'roles'
31
    ];
32
33
    /**
34
     * The hidden attributes.
35
     *
36
     * @var array
37
     */
38
    protected $hidden = [
39
        'password'
40
    ];
41
42
    /**
43
     * Get the email.
44
     *
45
     * @return string
46
     */
47
    public function getEmail()
48
    {
49
        return $this->email;
50
    }
51
52
    /**
53
     * Get the username.
54
     *
55
     * @return string
56
     */
57
    public function getUsername()
58
    {
59
        return $this->username;
60
    }
61
62
    /**
63
     * Get the display name.
64
     *
65
     * @return string
66
     */
67
    public function getDisplayName()
68
    {
69
        return $this->display_name;
70
    }
71
72
    /**
73
     * Get the first name.
74
     *
75
     * @return string
76
     */
77
    public function getFirstName()
78
    {
79
        return $this->first_name;
80
    }
81
82
    /**
83
     * Get the last name.
84
     *
85
     * @return string
86
     */
87
    public function getLastName()
88
    {
89
        return $this->last_name;
90
    }
91
92
    /**
93
     * Set the password.
94
     *
95
     * @param $password
96
     * @return $this
97
     */
98
    public function setPassword($password)
99
    {
100
        $this->password = $password;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Get related roles.
107
     *
108
     * @return EntryCollection
109
     */
110
    public function getRoles()
111
    {
112
        return $this->roles;
113
    }
114
115
    /**
116
     * Return whether a user is in a role.
117
     *
118
     * @param $role
119
     * @return bool
120
     */
121
    public function hasRole($role)
122
    {
123
        if (!is_object($role)) {
124
            $role = $this->dispatch(new GetRole($role));
125
        }
126
127
        if (!$role) {
128
            return true;
129
        }
130
131
        /* @var RoleInterface $role */
132
        foreach ($roles = $this->getRoles() as $attached) {
133
            if ($attached->getId() === $role->getId()) {
134
                return true;
135
            }
136
        }
137
138
        return false;
139
    }
140
141
    /**
142
     * Return whether a user is in
143
     * any of the provided roles.
144
     *
145
     * @param $roles
146
     * @return bool
147
     */
148
    public function hasAnyRole($roles)
149
    {
150
        if ($roles instanceof Collection) {
0 ignored issues
show
Bug introduced by
The class Anomaly\Streams\Platform\Support\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
151
            $roles = $roles->all();
152
        }
153
154
        if ($roles) {
155
            return true;
156
        }
157
158
        foreach ($roles as $role) {
159
            if ($this->hasRole($role)) {
160
                return true;
161
            }
162
        }
163
164
        return false;
165
    }
166
167
    /**
168
     * Return whether the user
169
     * is an admin or not.
170
     *
171
     * @return bool
172
     */
173
    public function isAdmin()
174
    {
175
        /* @var RoleInterface $role */
176
        foreach ($roles = $this->getRoles() as $role) {
177
            if ($role->getSlug() === 'admin') {
178
                return true;
179
            }
180
        }
181
182
        return false;
183
    }
184
185
    /**
186
     * Get the permissions.
187
     *
188
     * @return array
189
     */
190
    public function getPermissions()
191
    {
192
        return $this->permissions;
193
    }
194
195
    /**
196
     * Set the permissions.
197
     *
198
     * @param array $permissions
199
     * @return $this
200
     */
201
    public function setPermissions(array $permissions)
202
    {
203
        $this->permissions = $permissions;
204
205
        return $this;
206
    }
207
208
    /**
209
     * Return whether a user or it's roles has a permission.
210
     *
211
     * @param      $permission
212
     * @param bool $checkRoles
213
     * @return mixed
214
     */
215
    public function hasPermission($permission, $checkRoles = true)
216
    {
217
        if (!$permission) {
218
            return true;
219
        }
220
221
        if (in_array($permission, $this->getPermissions())) {
222
            return true;
223
        }
224
225
        if ($checkRoles) {
226
227
            /* @var RoleInterface $role */
228
            foreach ($this->getRoles() as $role) {
229
                if ($role->hasPermission($permission) || $role->getSlug() === 'admin') {
230
                    return true;
231
                }
232
            }
233
        }
234
235
        return false;
236
    }
237
238
    /**
239
     * Return whether a user has any of provided permission.
240
     *
241
     * @param $permissions
242
     * @return bool
243
     */
244
    public function hasAnyPermission(array $permissions)
245
    {
246
        foreach ($permissions as $permission) {
247
            if ($this->hasPermission($permission)) {
248
                return true;
249
            }
250
        }
251
252
        return false;
253
    }
254
255
    /**
256
     * Hash the password whenever setting it.
257
     *
258
     * @param $password
259
     */
260
    public function setPasswordAttribute($password)
261
    {
262
        $this->attributes['password'] = app('hash')->make($password);
263
    }
264
265
    /**
266
     * Return whether the model is deletable or not.
267
     *
268
     * @return bool
269
     */
270
    public function isDeletable()
271
    {
272
        // You can't delete yourself.
273
        if ($this->getId() == app('auth')->id()) {
274
            return false;
275
        }
276
277
        // Only admins can delete admins
278
        if (!app('auth')->user()->isAdmin() && $this->isAdmin()) {
279
            return false;
280
        }
281
282
        return true;
283
    }
284
285
    /**
286
     * Return the activated flag.
287
     *
288
     * @return bool
289
     */
290
    public function isActivated()
291
    {
292
        return $this->activated;
293
    }
294
295
    /**
296
     * Return the enabled flag.
297
     *
298
     * @return bool
299
     */
300
    public function isEnabled()
301
    {
302
        return $this->enabled;
303
    }
304
305
    /**
306
     * Get the reset code.
307
     *
308
     * @return string
309
     */
310
    public function getResetCode()
311
    {
312
        return $this->reset_code;
313
    }
314
315
    /**
316
     * Set the reset code.
317
     *
318
     * @param $code
319
     * @return $this
320
     */
321
    public function setResetCode($code)
322
    {
323
        $this->reset_code = $code;
324
325
        return $this;
326
    }
327
328
    /**
329
     * Get the activation code.
330
     *
331
     * @return string
332
     */
333
    public function getActivationCode()
334
    {
335
        return $this->activation_code;
336
    }
337
338
    /**
339
     * Set the activation code.
340
     *
341
     * @param $code
342
     * @return $this
343
     */
344
    public function setActivationCode($code)
345
    {
346
        $this->activation_code = $code;
347
348
        return $this;
349
    }
350
351
    /**
352
     * Return the full name.
353
     *
354
     * @return string
355
     */
356
    public function name()
357
    {
358
        return $this->getFirstName() . ' ' . $this->getLastName();
359
    }
360
361
    /**
362
     * Attach a role to the user.
363
     *
364
     * @param RoleInterface $role
365
     */
366
    public function attachRole(RoleInterface $role)
367
    {
368
        $this->roles()->attach($role);
369
    }
370
}
371