Completed
Pull Request — dev (#235)
by Alies
07:06
created

UserAttribute   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 90
dl 0
loc 257
rs 9.0399
c 0
b 0
f 0
wmc 42

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatusLabelAttribute() 0 7 2
A getConfirmedLabelAttribute() 0 14 4
A getSocialButtonsAttribute() 0 12 3
A getStatusButtonAttribute() 0 22 4
A getChangePasswordButtonAttribute() 0 3 1
A getDeleteButtonAttribute() 0 12 3
A getClearSessionButtonAttribute() 0 11 3
A getDeletePermanentlyButtonAttribute() 0 3 1
A getConfirmedButtonAttribute() 0 7 3
A getFullNameAttribute() 0 5 2
A getPictureAttribute() 0 3 1
A getShowButtonAttribute() 0 3 1
A getActionButtonsAttribute() 0 26 2
A getRestoreButtonAttribute() 0 3 1
A getLoginAsButtonAttribute() 0 16 4
A getEditButtonAttribute() 0 3 1
A getNameAttribute() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like UserAttribute 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.

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 UserAttribute, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace App\Models\Auth\Traits\Attribute;
4
5
use Illuminate\Support\Facades\Hash;
6
7
/**
8
 * Trait UserAttribute.
9
 */
10
trait UserAttribute
11
{
12
    /**
13
     * @param $password
14
     */
15
    public function setPasswordAttribute($password) : void
16
    {
17
        // If password was accidentally passed in already hashed, try not to double hash it
18
        if (
19
            (\strlen($password) === 60 && preg_match('/^\$2y\$/', $password)) ||
20
            (\strlen($password) === 95 && preg_match('/^\$argon2i\$/', $password))
21
        ) {
22
            $hash = $password;
23
        } else {
24
            $hash = Hash::make($password);
25
        }
26
27
        // Note: Password Histories are logged from the \App\Observer\User\UserObserver class
28
        $this->attributes['password'] = $hash;
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public function getStatusLabelAttribute()
35
    {
36
        if ($this->isActive()) {
0 ignored issues
show
Bug introduced by
It seems like isActive() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        if ($this->/** @scrutinizer ignore-call */ isActive()) {
Loading history...
37
            return "<span class='badge badge-success'>".__('labels.general.active').'</span>';
38
        }
39
40
        return "<span class='badge badge-danger'>".__('labels.general.inactive').'</span>';
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getConfirmedLabelAttribute()
47
    {
48
        if ($this->isConfirmed()) {
0 ignored issues
show
Bug introduced by
It seems like isConfirmed() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        if ($this->/** @scrutinizer ignore-call */ isConfirmed()) {
Loading history...
49
            if ($this->id != 1 && $this->id != auth()->id()) {
50
                return '<a href="'.route(
51
                    'admin.auth.user.unconfirm',
52
                        $this
53
                ).'" data-toggle="tooltip" data-placement="top" title="'.__('buttons.backend.access.users.unconfirm').'" name="confirm_item"><span class="badge badge-success" style="cursor:pointer">'.__('labels.general.yes').'</span></a>';
54
            } else {
55
                return '<span class="badge badge-success">'.__('labels.general.yes').'</span>';
56
            }
57
        }
58
59
        return '<a href="'.route('admin.auth.user.confirm', $this).'" data-toggle="tooltip" data-placement="top" title="'.__('buttons.backend.access.users.confirm').'" name="confirm_item"><span class="badge badge-danger" style="cursor:pointer">'.__('labels.general.no').'</span></a>';
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getFullNameAttribute()
66
    {
67
        return $this->last_name
68
            ? $this->first_name.' '.$this->last_name
69
            : $this->first_name;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getNameAttribute()
76
    {
77
        return $this->full_name;
78
    }
79
80
    /**
81
     * @return mixed
82
     */
83
    public function getPictureAttribute()
84
    {
85
        return $this->getPicture();
0 ignored issues
show
Bug introduced by
The method getPicture() does not exist on App\Models\Auth\Traits\Attribute\UserAttribute. Did you maybe mean getPictureAttribute()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        return $this->/** @scrutinizer ignore-call */ getPicture();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getSocialButtonsAttribute()
92
    {
93
        $accounts = [];
94
95
        foreach ($this->providers as $social) {
96
            $accounts[] = '<a href="'.route(
97
                'admin.auth.user.social.unlink',
98
                    [$this, $social]
99
            ).'" data-toggle="tooltip" data-placement="top" title="'.__('buttons.backend.access.users.unlink').'" data-method="delete"><i class="fab fa-'.$social->provider.'"></i></a>';
100
        }
101
102
        return \count($accounts) ? implode(' ', $accounts) : __('labels.general.none');
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    public function getLoginAsButtonAttribute()
109
    {
110
        /*
111
         * If the admin is currently NOT spoofing a user
112
         */
113
        if (! session()->has('admin_user_id') || ! session()->has('temp_user_id')) {
114
            //Won't break, but don't let them "Login As" themselves
115
            if ($this->id != auth()->id()) {
116
                return '<a href="'.route(
117
                    'admin.auth.user.login-as',
118
                        $this
119
                ).'" class="dropdown-item">'.__('buttons.backend.access.users.login_as', ['user' => e($this->full_name)]).'</a> ';
0 ignored issues
show
Bug introduced by
Are you sure the usage of e($this->full_name) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
120
            }
121
        }
122
123
        return '';
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getClearSessionButtonAttribute()
130
    {
131
        if ($this->id != auth()->id() && config('session.driver') == 'database') {
132
            return '<a href="'.route('admin.auth.user.clear-session', $this).'"
133
			 	 data-trans-button-cancel="'.__('buttons.general.cancel').'"
134
                 data-trans-button-confirm="'.__('buttons.general.continue').'"
135
                 data-trans-title="'.__('strings.backend.general.are_you_sure').'"
136
                 class="dropdown-item" name="confirm_item">'.__('buttons.backend.access.users.clear_session').'</a> ';
137
        }
138
139
        return '';
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getShowButtonAttribute()
146
    {
147
        return '<a href="'.route('admin.auth.user.show', $this).'" data-toggle="tooltip" data-placement="top" title="'.__('buttons.general.crud.view').'" class="btn btn-info"><i class="fas fa-eye"></i></a>';
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getEditButtonAttribute()
154
    {
155
        return '<a href="'.route('admin.auth.user.edit', $this).'" data-toggle="tooltip" data-placement="top" title="'.__('buttons.general.crud.edit').'" class="btn btn-primary"><i class="fas fa-edit"></i></a>';
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function getChangePasswordButtonAttribute()
162
    {
163
        return '<a href="'.route('admin.auth.user.change-password', $this).'" class="dropdown-item">'.__('buttons.backend.access.users.change_password').'</a> ';
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function getStatusButtonAttribute()
170
    {
171
        if ($this->id != auth()->id()) {
172
            switch ($this->active) {
173
                case 0:
174
                    return '<a href="'.route('admin.auth.user.mark', [
175
                            $this,
176
                            1,
177
                        ]).'" class="dropdown-item">'.__('buttons.backend.access.users.activate').'</a> ';
178
179
                case 1:
180
                    return '<a href="'.route('admin.auth.user.mark', [
181
                            $this,
182
                            0,
183
                        ]).'" class="dropdown-item">'.__('buttons.backend.access.users.deactivate').'</a> ';
184
185
                default:
186
                    return '';
187
            }
188
        }
189
190
        return '';
191
    }
192
193
    /**
194
     * @return string
195
     */
196
    public function getConfirmedButtonAttribute()
197
    {
198
        if (! $this->isConfirmed() && ! config('access.users.requires_approval')) {
199
            return '<a href="'.route('admin.auth.user.account.confirm.resend', $this).'" class="dropdown-item">'.__('buttons.backend.access.users.resend_email').'</a> ';
200
        }
201
202
        return '';
203
    }
204
205
    /**
206
     * @return string
207
     */
208
    public function getDeleteButtonAttribute()
209
    {
210
        if ($this->id != auth()->id() && $this->id != 1) {
211
            return '<a href="'.route('admin.auth.user.destroy', $this).'"
212
                 data-method="delete"
213
                 data-trans-button-cancel="'.__('buttons.general.cancel').'"
214
                 data-trans-button-confirm="'.__('buttons.general.crud.delete').'"
215
                 data-trans-title="'.__('strings.backend.general.are_you_sure').'"
216
                 class="dropdown-item">'.__('buttons.general.crud.delete').'</a> ';
217
        }
218
219
        return '';
220
    }
221
222
    /**
223
     * @return string
224
     */
225
    public function getDeletePermanentlyButtonAttribute()
226
    {
227
        return '<a href="'.route('admin.auth.user.delete-permanently', $this).'" name="confirm_item" class="btn btn-danger"><i class="fas fa-trash" data-toggle="tooltip" data-placement="top" title="'.__('buttons.backend.access.users.delete_permanently').'"></i></a> ';
228
    }
229
230
    /**
231
     * @return string
232
     */
233
    public function getRestoreButtonAttribute()
234
    {
235
        return '<a href="'.route('admin.auth.user.restore', $this).'" name="confirm_item" class="btn btn-info"><i class="fas fa-refresh" data-toggle="tooltip" data-placement="top" title="'.__('buttons.backend.access.users.restore_user').'"></i></a> ';
236
    }
237
238
    /**
239
     * @return string
240
     */
241
    public function getActionButtonsAttribute()
242
    {
243
        if ($this->trashed()) {
0 ignored issues
show
Bug introduced by
It seems like trashed() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

243
        if ($this->/** @scrutinizer ignore-call */ trashed()) {
Loading history...
244
            return '
245
				<div class="btn-group" role="group" aria-label="'.__('labels.backend.access.users.user_actions').'">
246
				  '.$this->restore_button.'
247
				  '.$this->delete_permanently_button.'
248
				</div>';
249
        }
250
251
        return '
252
    	<div class="btn-group" role="group" aria-label="'.__('labels.backend.access.users.user_actions').'">
253
		  '.$this->show_button.'
254
		  '.$this->edit_button.'
255
256
		  <div class="btn-group btn-group-sm" role="group">
257
			<button id="userActions" type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
258
			  '.__('labels.general.more').'
259
			</button>
260
			<div class="dropdown-menu" aria-labelledby="userActions">
261
			  '.$this->clear_session_button.'
262
			  '.$this->login_as_button.'
263
			  '.$this->change_password_button.'
264
			  '.$this->status_button.'
265
			  '.$this->confirmed_button.'
266
			  '.$this->delete_button.'
267
			</div>
268
		  </div>
269
		</div>';
270
    }
271
}
272