Passed
Push — dev ( 03a84b...1c649f )
by Chris
06:48 queued 10s
created

User::isGovIdentityConfirmed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
cc 2
nc 2
nop 0
crap 6
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;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Notifications\Notifiable requires the property $phone_number which is not provided by App\Models\User.
Loading history...
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 22
    protected $dispatchesEvents = [
87
        'created' => UserCreated::class,
88 22
        'updated' => UserUpdated::class,
89
    ];
90
91 37
    public function applicant() //phpcs:ignore
92
    {
93 37
        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 72
    public function profile_pic() //phpcs:ignore
102
    {
103 72
        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
    // Role related functions
112
113
    /**
114
     * Returns true if this user has the Applicant role.
115
     *
116
     * @return boolean
117
     */
118
    public function isApplicant(): bool
119
    {
120
        // Currently, every user can create an Applicant profile and apply to jobs.
121
        return true;
122
    }
123
124
    /**
125
     * Returns true if this user has the upgradedManager role.
126
     *
127
     * @return boolean
128
     */
129
    public function isUpgradedManager(): bool
130
    {
131
        return $this->isAdmin() || $this->user_role->name === 'upgradedManager';
132
    }
133
134
    /**
135
     * Returns true this user has the demoManager role.
136 47
     *
137
     * @return boolean
138 47
     */
139
    public function isDemoManager(): bool
140
    {
141
        // Currently, every non-upgradedManager user can be considered a demoManager.
142
        return !$this->isUpgradedManager();
143
    }
144
145
    /**
146
     * Returns true if this user has the demoManager or upgradedManager role.
147
     *
148
     * @return boolean
149
     */
150
    public function isManager(): bool
151
    {
152
        // Currently, every user can use the Manager portal as a demoManager.
153
        return $this->isDemoManager() || $this->isUpgradedManager();
154
    }
155
156
    /**
157
     * Returns true if this user has the Admin role.
158
     *
159
     * @return boolean
160
     */
161
    public function isAdmin(): bool
162
    {
163
        return $this->user_role->name === 'admin';
164
    }
165
166
    /**
167
    * Check if the user has the specified role.
168
    * @param string $role This may be either 'applicant', 'manager' or 'admin'.
169
    * @return boolean
170
    */
171
    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...
172
    {
173
        switch ($role) {
174
            case 'applicant':
175
                return $this->isApplicant();
176
            case 'manager':
177
                return $this->isManager();
178
            case 'admin':
179
                return $this->isAdmin();
180
            default:
181
                return false;
182
        }
183
    }
184
185
    /**
186
     * Set this user to the specified role.
187
     *
188
     * @param string $role Must be either 'applicant', 'manager' or 'admin.
189
    * @return void
190
    */
191
    public function setRole(string $role): void
192
    {
193
        $this->user_role()->associate(UserRole::where('name', $role)->firstOrFail());
194
    }
195
196
    /**
197
     * OVERRIDE
198
     * Send the password reset notification.
199
     *
200
     * @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...
201
     *
202
     * @return void
203
     */
204
    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...
205
    {
206
        $this->notify(new ResetPasswordNotification($token));
207
    }
208
209
    /**
210
     * Gov identity has been confirmed either if:
211
     *  - they have confirmed to NOT be in government,
212
     *  - OR they've added a gov email.
213
     *
214
     * @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...
215
     * @return boolean
216
     */
217
    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...
218
    {
219
        return $this->not_in_gov || !empty($this->gov_email);
220
    }
221
}
222