Passed
Push — feature/review_applications ( da0b67...de6738 )
by Tristan
20:30 queued 05:30
created

getMeetsEssentialCriteriaAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * Created by Reliese Model.
5
 * Date: Thu, 12 Jul 2018 22:39:27 +0000.
6
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
7
8
namespace App\Models;
9
use App\Models\Lookup\VeteranStatus;
10
use App\Models\Lookup\PreferredLanguage;
11
use App\Models\Lookup\CitizenshipDeclaration;
12
use App\Models\Applicant;
13
use App\Models\SkillDeclaration;
14
use App\Models\ApplicationReview;
15
use Illuminate\Notifications\Notifiable;
16
use App\Events\ApplicationSaved;
17
use App\Events\ApplicationRetrieved;
18
use App\Services\Validation\ApplicationValidator;
19
20
/**
21
 * Class JobApplication
22
 *
23
 * @property int $id
24
 * @property int $job_poster_id
25
 * @property int $application_status_id
26
 * @property int $citizenship_declaration_id
27
 * @property int $veteran_status_id
28
 * @property int $preferred_language_id
29
 * @property int $applicant_id
30
 * @property int $applicant_snapshot_id
31
 * @property string $submission_signature
32
 * @property string $submission_date
33
 * @property boolean $experience_saved
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\Applicant $applicant_snapshot
39
 * @property \App\Models\Lookup\ApplicationStatus $application_status
40
 * @property \App\Models\Lookup\CitizenshipDeclaration $citizenship_declaration
41
 * @property \App\Models\Lookup\VeteranStatus $veteran_status
42
 * @property \App\Models\Lookup\PreferredLanguage $preferred_language
43
 * @property \App\Models\JobPoster $job_poster
44
 * @property \Illuminate\Database\Eloquent\Collection $job_application_answers
45
 * @property \Illuminate\Database\Eloquent\Collection $skill_declarations
46
 * @property \App\Models\ApplicationReview $application_review
47
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
48
class JobApplication extends BaseModel {
0 ignored issues
show
Coding Style introduced by
Opening brace of a class must be on the line after the definition
Loading history...
49
50
    use Notifiable;
0 ignored issues
show
introduced by
The trait Illuminate\Notifications\Notifiable requires some properties which are not provided by App\Models\JobApplication: $email, $phone_number
Loading history...
51
52
    protected $dispatchesEvents = [
53
        'retrieved' => ApplicationRetrieved::class,
54
        'saved' => ApplicationSaved::class,
55
    ];
56
57
    protected $casts = [
58
        'job_poster_id' => 'int',
59
        'application_status_id' => 'int',
60
        'citizenship_declaration_id' => 'int',
61
        'veteran_status_id' => 'int',
62
        'preferred_language_id' => 'int',
63
        'applicant_id' => 'int',
64
        'applicant_snapshot_id' => 'int',
65
        'submission_signature' => 'string',
66
        'submission_date' => 'string',
67
        'experience_saved' => 'boolean',
68
    ];
69
    protected $fillable = [
70
        'citizenship_declaration_id',
71
        'veteran_status_id',
72
        'preferred_language_id',
73
        'submission_signature',
74
        'submission_date',
75
        'experience_saved',
76
    ];
77
78
    /**
79
     * The accessors to append to the model's array/json form.
80
     *
81
     * @var array
82
     */
83
    protected $appends = ['meets_essential_criteria'];
84
85
    protected function createApplicantSnapshot($applicant_id) {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createApplicantSnapshot()
Loading history...
86
        $applicant = Applicant::where('id', $applicant_id)->firstOrFail();
87
88
        $snapshot = $applicant->replicate();
0 ignored issues
show
Unused Code introduced by
The assignment to $snapshot is dead and can be removed.
Loading history...
89
90
    }
91
92
    public function applicant() {
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function applicant()
Loading history...
93 11
        return $this->belongsTo(\App\Models\Applicant::class);
94 11
    }
95
96
    public function applicant_snapshot() {
0 ignored issues
show
Coding Style introduced by
Public method name "JobApplication::applicant_snapshot" is not in camel caps format
Loading history...
Coding Style introduced by
Missing doc comment for function applicant_snapshot()
Loading history...
97
        return $this->belongsTo(\App\Models\Applicant::class, 'applicant_snapshot_id');
98
    }
99
100
    public function application_status() {
0 ignored issues
show
Coding Style introduced by
Public method name "JobApplication::application_status" is not in camel caps format
Loading history...
Coding Style introduced by
Missing doc comment for function application_status()
Loading history...
101
        return $this->belongsTo(\App\Models\Lookup\ApplicationStatus::class);
102
    }
103
104
    public function citizenship_declaration() {
0 ignored issues
show
Coding Style introduced by
Public method name "JobApplication::citizenship_declaration" is not in camel caps format
Loading history...
Coding Style introduced by
Missing doc comment for function citizenship_declaration()
Loading history...
105
        return $this->belongsTo(\App\Models\Lookup\CitizenshipDeclaration::class);
106
    }
107
108
    public function veteran_status() {
0 ignored issues
show
Coding Style introduced by
Public method name "JobApplication::veteran_status" is not in camel caps format
Loading history...
Coding Style introduced by
Missing doc comment for function veteran_status()
Loading history...
109
        return $this->belongsTo(\App\Models\Lookup\VeteranStatus::class);
110
    }
111
112
    public function preferred_language() {
0 ignored issues
show
Coding Style introduced by
Public method name "JobApplication::preferred_language" is not in camel caps format
Loading history...
Coding Style introduced by
Missing doc comment for function preferred_language()
Loading history...
113
        return $this->belongsTo(\App\Models\Lookup\PreferredLanguage::class);
114
    }
115
116
    public function job_poster() {
0 ignored issues
show
Coding Style introduced by
Public method name "JobApplication::job_poster" is not in camel caps format
Loading history...
Coding Style introduced by
Missing doc comment for function job_poster()
Loading history...
117
        return $this->belongsTo(\App\Models\JobPoster::class);
118
    }
119
120
    public function job_application_answers() {
0 ignored issues
show
Coding Style introduced by
Public method name "JobApplication::job_application_answers" is not in camel caps format
Loading history...
Coding Style introduced by
Missing doc comment for function job_application_answers()
Loading history...
121
        return $this->hasMany(\App\Models\JobApplicationAnswer::class);
122
    }
123
124
    public function skill_declarations() {
0 ignored issues
show
Coding Style introduced by
Public method name "JobApplication::skill_declarations" is not in camel caps format
Loading history...
Coding Style introduced by
Missing doc comment for function skill_declarations()
Loading history...
125
        return $this->applicant->skill_declarations()
126
            ->whereIn('skill_id', $this->job_poster->criteria->pluck('skill_id'));
127
    }
128
129
    public function application_review() {
0 ignored issues
show
Coding Style introduced by
Public method name "JobApplication::application_review" is not in camel caps format
Loading history...
Coding Style introduced by
Missing doc comment for function application_review()
Loading history...
130
        return $this->hasOne(ApplicationReview::class);
131
    }
132
133
    /**
134
     * Return either 'complete', 'incomplete' or 'error', depending on the
135
     * status of the requested section.
136
     *
137
     * @param  string $section Should be one of:
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 2
Loading history...
138
     *                              'basics'
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 25 spaces but found 30
Loading history...
139
     *                              'experience'
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 25 spaces but found 30
Loading history...
140
     *                              'essential_skills'
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 25 spaces but found 30
Loading history...
141
     *                              'asset_skills'
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 25 spaces but found 30
Loading history...
142
     *                              'preview'
0 ignored issues
show
Coding Style introduced by
Parameter comment not aligned correctly; expected 25 spaces but found 30
Loading history...
143
     *
144
     * @return string $status   'complete', 'incomplete' or 'error'
145
     */
146
    public function getSectionStatus(string $section) {
147
        //TODO: determine whether sections are complete or invalid
148
        $validator = new ApplicationValidator();
149
        $status = 'incomplete';
150
        switch($section) {
151
            case 'basics':
152
                if ($validator->basicsComplete($this)) {
153
                    $status = 'complete';
154
                }
155
                break;
156
            case 'experience':
157
                if ($validator->experienceComplete($this)) {
158
                    $status = 'complete';
159
                }
160
                break;
161
            case 'essential_skills':
162
                if ($validator->essentialSkillsComplete($this)) {
163
                    $status = 'complete';
164
                }
165
                break;
166
            case 'asset_skills':
167
                if ($validator->assetSkillsComplete($this)) {
168
                    $status = 'complete';
169
                }
170
                break;
171
            case 'preview':
172
                if ($validator->basicsComplete($this) &&
173
                    $validator->experienceComplete($this) &&
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
174
                    $validator->essentialSkillsComplete($this) &&
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
175
                    $validator->assetSkillsComplete($this)) {
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
176
                    $status = 'complete';
177
                }
178
                break;
179
            case 'confirm':
180
                if ($validator->affirmationComplete($this)) {
181
                    $status = 'complete';
182
                }
183
                break;
184
            default:
185
                $status = 'error';
186
                break;
187
        }
188
        return $status;
189
    }
190
191
    /**
192
     * Returns true if this application meets all the essential criteria.
193
     * That means it has attached an SkillDeclaration for each essential criterion,
194
     * with a level at least as high as the required level.
195
     *
196
     * @return boolean
197
     */
198
    public function meetsEssentialCriteria(): bool
199
    {
200
        $essentialCriteria = $this->job_poster->criteria->filter(
201
            function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

201
            function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
202
                return $value->criteria_type->name == 'essential';
203
            }
204
        );
205
        foreach ($essentialCriteria as $criterion) {
206
            $skillDeclaration = $this->skill_declarations->where("skill_id", $criterion->skill_id)->first();
207
            if ($skillDeclaration === null ||
208
                $skillDeclaration->skill_level_id < $criterion->skill_level_id) {
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
209
                return false;
210
            }
211
        }
212
        return true;
213
    }
214
215
    /**
216
     * Accessor for meetsEssentialCriteria function, which
217
     * allows this value to be automtacially appended to array/json representation.
218
     *
219
     * @return boolean
220
     */
221
    public function getMeetsEssentialCriteriaAttribute():bool
222
    {
223
        return $this->meetsEssentialCriteria();
224
    }
225
}
226