Passed
Pull Request — release/1.0.15 (#1655)
by Tristan
10:06 queued 02:47
created

User::setIsPriorityAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Created by Reliese Model.
5
 * Date: Thu, 12 Jul 2018 22:39:28 +0000.
6
 */
7
8
namespace App\Models;
9
10
use Illuminate\Auth\Authenticatable;
11
use Illuminate\Auth\Passwords\CanResetPassword;
12
use Illuminate\Foundation\Auth\Access\Authorizable;
13
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
14
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
15
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
16
use Illuminate\Notifications\Notifiable;
17
use App\Events\UserCreated;
18
use App\Events\UserUpdated;
19
use App\Notifications\ResetPasswordNotification;
20
use App\CRUD\TalentCloudCrudTrait as CrudTrait;
21
22
/**
23
 * Class User
24
 *
25
 * @property int $id
26
 * @property string $email
27
 * @property string $name
28
 * @property string $password
29
 * @property boolean $is_confirmed
30
 * @property boolean $is_priority
31
 * @property int $user_role_id
32
 * @property string $gov_email
33
 * @property boolean $not_in_gov
34
 * @property \Jenssegers\Date\Date $created_at
35
 * @property \Jenssegers\Date\Date $updated_at
36
 *
37
 * @property \App\Models\Applicant $applicant
38
 * @property \App\Models\Manager $manager
39
 * @property \App\Models\ProfilePic $profile_pic
40
 * @property \App\Models\UserRole $user_role
41
 */
42
class User extends BaseModel implements
43
    // Laravel contracts for native login.
44
    AuthenticatableContract,
45
    CanResetPasswordContract,
46
    // Contract for use with Gates and Policies.
47
    AuthorizableContract
48
    // Custom contract for use with openid login.
49
    // \App\Services\Auth\Contracts\OidcAuthenticatable.
50
{
51
52
    // Traits for Laravel basic authentication.
53
    use Authenticatable;
54
    use CanResetPassword;
55
    // Trait for working with Gates and Policies.
56
    use Authorizable;
57
    // Trait for notifications.
58
    use Notifiable;
59
    // Trait for Backpack.
60
    use CrudTrait;
61
62
    protected $casts = [
1 ignored issue
show
introduced by
Property \App\Models\User::$casts does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
63
        'is_confirmed' => 'boolean',
64
        'is_priority' => 'boolean',
65
        'user_role_id' => 'int',
66
        'email' => 'string',
67
        'gov_email' => 'string',
68
        'not_in_gov' => 'boolean',
69
    ];
70
71
    protected $fillable = [
1 ignored issue
show
introduced by
Property \App\Models\User::$fillable does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
72
        'name', 'email', 'password', 'is_priority', 'gov_email', 'not_in_gov'
73
    ];
74
75
    protected $with = ['user_role'];
1 ignored issue
show
introduced by
Property \App\Models\User::$with does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
76
77
    protected $hidden = [
1 ignored issue
show
introduced by
Property \App\Models\User::$hidden does not have @var annotation.
Loading history...
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
78
        'password', 'remember_token',
79
    ];
80
81
    /**
82
     * The event map for the model.
83
     *
84
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \App\Models\User::$dispatchesEvents does not specify type hint for its items.
Loading history...
85
     */
86
    protected $dispatchesEvents = [
87
        'created' => UserCreated::class,
88
        'updated' => UserUpdated::class,
89
    ];
90
91
    public function applicant() //phpcs:ignore
92
    {
93
        return $this->hasOne(\App\Models\Applicant::class);
94
    }
95
96
    public function manager() //phpcs:ignore
97
    {
98
        return $this->hasOne(\App\Models\Manager::class);
99
    }
100
101
    public function profile_pic() //phpcs:ignore
102
    {
103
        return $this->hasOne(\App\Models\ProfilePic::class);
104
    }
105
106
    public function user_role() //phpcs:ignore
107
    {
108
        return $this->belongsTo(\App\Models\UserRole::class);
109
    }
110
111
    public function setIsPriorityAttribute($value)
1 ignored issue
show
introduced by
Method \App\Models\User::setIsPriorityAttribute() does not have parameter type hint nor @param annotation for its parameter $value.
Loading history...
introduced by
Method \App\Models\User::setIsPriorityAttribute() does not have void return type hint.
Loading history...
Coding Style Documentation introduced by
Missing doc comment for function setIsPriorityAttribute()
Loading history...
112
    {
113
        if ($value === null) {
114
            $value = false;
115
        }
116
        $this->attributes['is_priority'] = $value;
117
    }
118
119
    // Role related functions
120
121
    /**
122
     * Returns true if this user has the Applicant role.
123
     *
124
     * @return boolean
125
     */
126
    public function isApplicant(): bool
127
    {
128
        // Currently, every user can create an Applicant profile and apply to jobs.
129
        return true;
130
    }
131
132
    /**
133
     * Returns true if this user has the upgradedManager role.
134
     *
135
     * @return boolean
136
     */
137
    public function isUpgradedManager(): bool
138
    {
139
        return $this->isAdmin() || $this->user_role->name === 'upgradedManager';
140
    }
141
142
    /**
143
     * Returns true this user has the demoManager role.
144
     *
145
     * @return boolean
146
     */
147
    public function isDemoManager(): bool
148
    {
149
        // Currently, every non-upgradedManager user can be considered a demoManager.
150
        return !$this->isUpgradedManager();
151
    }
152
153
    /**
154
     * Returns true if this user has the demoManager or upgradedManager role.
155
     *
156
     * @return boolean
157
     */
158
    public function isManager(): bool
159
    {
160
        // Currently, every user can use the Manager portal as a demoManager.
161
        return $this->isDemoManager() || $this->isUpgradedManager();
162
    }
163
164
    /**
165
     * Returns true if this user has the Admin role.
166
     *
167
     * @return boolean
168
     */
169
    public function isAdmin(): bool
170
    {
171
        return $this->user_role->name === 'admin';
172
    }
173
174
    /**
175
    * Check if the user has the specified role.
176
    * @param string $role This may be either 'applicant', 'manager' or 'admin'.
177
    * @return boolean
178
    */
179
    public function hasRole($role)
1 ignored issue
show
introduced by
Method \App\Models\User::hasRole() does not have parameter type hint for its parameter $role but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \App\Models\User::hasRole() does not have return type hint for its return value but it should be possible to add it based on @return annotation "boolean".
Loading history...
Coding Style introduced by
Type hint "string" missing for $role
Loading history...
180
    {
181
        switch ($role) {
182
            case 'applicant':
183
                return $this->isApplicant();
184
            case 'manager':
185
                return $this->isManager();
186
            case 'admin':
187
                return $this->isAdmin();
188
            default:
189
                return false;
190
        }
191
    }
192
193
    /**
194
     * Set this user to the specified role.
195
     *
196
     * @param string $role Must be either 'applicant', 'manager' or 'admin.
197
    * @return void
198
    */
199
    public function setRole(string $role): void
200
    {
201
        $this->user_role()->associate(UserRole::where('name', $role)->firstOrFail());
202
    }
203
204
    /**
205
     * OVERRIDE
206
     * Send the password reset notification.
207
     *
208
     * @param  string  $token
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
209
     *
210
     * @return void
211
     */
212
    public function sendPasswordResetNotification($token)
1 ignored issue
show
introduced by
Method \App\Models\User::sendPasswordResetNotification() does not have parameter type hint for its parameter $token but it should be possible to add it based on @param annotation "string".
Loading history...
introduced by
Method \App\Models\User::sendPasswordResetNotification() does not have return type hint for its return value but it should be possible to add it based on @return annotation "void".
Loading history...
Coding Style introduced by
Type hint "string" missing for $token
Loading history...
213
    {
214
        $this->notify(new ResetPasswordNotification($token));
215
    }
216
217
    /**
218
     * Gov identity has been confirmed either if:
219
     *  - they have confirmed to NOT be in government,
220
     *  - OR they've added a gov email.
221
     *
222
     * @param [type] $user
2 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Superfluous parameter comment
Loading history...
223
     * @return boolean
224
     */
225
    public function isGovIdentityConfirmed()
0 ignored issues
show
introduced by
Method \App\Models\User::isGovIdentityConfirmed() does not have return type hint for its return value but it should be possible to add it based on @return annotation "boolean".
Loading history...
226
    {
227
        return $this->not_in_gov || !empty($this->gov_email);
228
    }
229
}
230