UserModel   B
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 312
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 42
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 312
rs 8.295

21 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 getRoles() 0 4 1
B hasRole() 0 19 5
B hasAnyRole() 0 18 5
A isAdmin() 0 11 3
A getPermissions() 0 4 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 getActivationCode() 0 4 1
A name() 0 4 1
A attachRole() 0 4 1
A routeNotificationForSlack() 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\Model\Users\UsersUsersEntryModel;
4
use Anomaly\Streams\Platform\Support\Collection;
5
use Anomaly\UsersModule\Role\Command\GetRole;
6
use Anomaly\UsersModule\Role\Contract\RoleInterface;
7
use Anomaly\UsersModule\Role\RoleCollection;
8
use Anomaly\UsersModule\User\Contract\UserInterface;
9
use Illuminate\Auth\Authenticatable;
10
use Illuminate\Notifications\Notifiable;
11
12
/**
13
 * Class UserModel
14
 *
15
 * @link   http://pyrocms.com/
16
 * @author PyroCMS, Inc. <[email protected]>
17
 * @author Ryan Thompson <[email protected]>
18
 */
19
class UserModel extends UsersUsersEntryModel implements UserInterface, \Illuminate\Contracts\Auth\Authenticatable
20
{
21
22
    use Notifiable;
23
    use Authenticatable;
24
25
    /**
26
     * The eager loaded relationships.
27
     *
28
     * @var array
29
     */
30
    protected $with = [
31
        'roles',
32
    ];
33
34
    /**
35
     * The guarded attributes.
36
     *
37
     * @var array
38
     */
39
    protected $guarded = [
40
        'password',
41
    ];
42
43
    /**
44
     * Get the email.
45
     *
46
     * @return string
47
     */
48
    public function getEmail()
49
    {
50
        return $this->email;
51
    }
52
53
    /**
54
     * Get the username.
55
     *
56
     * @return string
57
     */
58
    public function getUsername()
59
    {
60
        return $this->username;
61
    }
62
63
    /**
64
     * Get the display name.
65
     *
66
     * @return string
67
     */
68
    public function getDisplayName()
69
    {
70
        return $this->display_name;
71
    }
72
73
    /**
74
     * Get the first name.
75
     *
76
     * @return string
77
     */
78
    public function getFirstName()
79
    {
80
        return $this->first_name;
81
    }
82
83
    /**
84
     * Get the last name.
85
     *
86
     * @return string
87
     */
88
    public function getLastName()
89
    {
90
        return $this->last_name;
91
    }
92
93
    /**
94
     * Get related roles.
95
     *
96
     * @return RoleCollection
97
     */
98
    public function getRoles()
99
    {
100
        return $this->roles;
101
    }
102
103
    /**
104
     * Return whether a user is in a role.
105
     *
106
     * @param $role
107
     * @return bool
108
     */
109
    public function hasRole($role)
110
    {
111
        if (!is_object($role)) {
112
            $role = $this->dispatch(new GetRole($role));
113
        }
114
115
        if (!$role) {
116
            return false;
117
        }
118
119
        /* @var RoleInterface $role */
120
        foreach ($roles = $this->getRoles() as $attached) {
121
            if ($attached->getId() === $role->getId()) {
122
                return true;
123
            }
124
        }
125
126
        return false;
127
    }
128
129
    /**
130
     * Return whether a user is in
131
     * any of the provided roles.
132
     *
133
     * @param $roles
134
     * @return bool
135
     */
136
    public function hasAnyRole($roles)
137
    {
138
        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...
139
            $roles = $roles->all();
140
        }
141
142
        if (!$roles) {
143
            return false;
144
        }
145
146
        foreach ($roles as $role) {
147
            if ($this->hasRole($role)) {
148
                return true;
149
            }
150
        }
151
152
        return false;
153
    }
154
155
    /**
156
     * Return whether the user
157
     * is an admin or not.
158
     *
159
     * @return bool
160
     */
161
    public function isAdmin()
162
    {
163
        /* @var RoleInterface $role */
164
        foreach ($roles = $this->getRoles() as $role) {
165
            if ($role->getSlug() === 'admin') {
166
                return true;
167
            }
168
        }
169
170
        return false;
171
    }
172
173
    /**
174
     * Get the permissions.
175
     *
176
     * @return array
177
     */
178
    public function getPermissions()
179
    {
180
        return $this->permissions;
181
    }
182
183
    /**
184
     * Return whether a user or it's roles has a permission.
185
     *
186
     * @param        $permission
187
     * @param  bool  $checkRoles
188
     * @return mixed
189
     */
190
    public function hasPermission($permission, $checkRoles = true)
191
    {
192
        if (!$permission) {
193
            return true;
194
        }
195
196
        if (in_array($permission, $this->getPermissions())) {
197
            return true;
198
        }
199
200
        if ($checkRoles) {
201
202
            /* @var RoleInterface $role */
203
            foreach ($this->getRoles() as $role) {
204
                if ($role->hasPermission($permission) || $role->getSlug() === 'admin') {
205
                    return true;
206
                }
207
            }
208
        }
209
210
        return false;
211
    }
212
213
    /**
214
     * Return whether a user has any of provided permission.
215
     *
216
     * @param array $permissions
217
     * @param bool  $checkRoles
218
     * @return bool
219
     */
220
    public function hasAnyPermission(array $permissions, $checkRoles = true)
221
    {
222
        foreach ($permissions as $permission) {
223
            if ($this->hasPermission($permission, $checkRoles)) {
224
                return true;
225
            }
226
        }
227
228
        return false;
229
    }
230
231
    /**
232
     * Hash the password whenever setting it.
233
     *
234
     * @param $password
235
     */
236
    public function setPasswordAttribute($password)
237
    {
238
        $this->attributes['password'] = app('hash')->make($password);
239
    }
240
241
    /**
242
     * Return whether the model is deletable or not.
243
     *
244
     * @return bool
245
     */
246
    public function isDeletable()
247
    {
248
        // You can't delete yourself.
249
        if ($this->getId() == app('auth')->id()) {
250
            return false;
251
        }
252
253
        // Only admins can delete admins
254
        if (!app('auth')->user()->isAdmin() && $this->isAdmin()) {
255
            return false;
256
        }
257
258
        return true;
259
    }
260
261
    /**
262
     * Return the activated flag.
263
     *
264
     * @return bool
265
     */
266
    public function isActivated()
267
    {
268
        return $this->activated;
269
    }
270
271
    /**
272
     * Return the enabled flag.
273
     *
274
     * @return bool
275
     */
276
    public function isEnabled()
277
    {
278
        return $this->enabled;
279
    }
280
281
    /**
282
     * Get the reset code.
283
     *
284
     * @return string
285
     */
286
    public function getResetCode()
287
    {
288
        return $this->reset_code;
289
    }
290
291
    /**
292
     * Get the activation code.
293
     *
294
     * @return string
295
     */
296
    public function getActivationCode()
297
    {
298
        return $this->activation_code;
299
    }
300
301
    /**
302
     * Return the full name.
303
     *
304
     * @return string
305
     */
306
    public function name()
307
    {
308
        return $this->getFirstName() . ' ' . $this->getLastName();
309
    }
310
311
    /**
312
     * Attach a role to the user.
313
     *
314
     * @param RoleInterface $role
315
     */
316
    public function attachRole(RoleInterface $role)
317
    {
318
        $this->roles()->attach($role);
319
    }
320
321
    /**
322
     * Route notifications for the Slack channel.
323
     *
324
     * @return string
325
     */
326
    public function routeNotificationForSlack()
327
    {
328
        return env('SLACK_WEBHOOK');
329
    }
330
}
331