Total Complexity | 65 |
Total Lines | 532 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like JobApplication 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 JobApplication, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
80 | class JobApplication extends BaseModel |
||
81 | { |
||
82 | // Trait for Backpack. |
||
83 | use CrudTrait; |
||
84 | |||
85 | use Notifiable; |
||
86 | |||
87 | protected $dispatchesEvents = [ |
||
88 | 'retrieved' => ApplicationRetrieved::class, |
||
89 | 'saved' => ApplicationSaved::class, |
||
90 | ]; |
||
91 | |||
92 | protected $casts = [ |
||
93 | 'job_poster_id' => 'int', |
||
94 | 'application_status_id' => 'int', |
||
95 | 'citizenship_declaration_id' => 'int', |
||
96 | 'veteran_status_id' => 'int', |
||
97 | 'preferred_language_id' => 'int', |
||
98 | 'applicant_id' => 'int', |
||
99 | 'applicant_snapshot_id' => 'int', |
||
100 | 'submission_signature' => 'string', |
||
101 | 'submission_date' => 'string', |
||
102 | 'experience_saved' => 'boolean', |
||
103 | 'language_requirement_confirmed' => 'boolean', |
||
104 | 'language_test_confirmed' => 'boolean', |
||
105 | 'education_requirement_confirmed' => 'boolean', |
||
106 | 'version_id' => 'int', |
||
107 | 'director_name' => 'string', |
||
108 | 'director_title' => 'string', |
||
109 | 'director_email' => 'string', |
||
110 | 'reference_name' => 'string', |
||
111 | 'reference_title' => 'string', |
||
112 | 'reference_email' => 'string', |
||
113 | 'gov_email' => 'string', |
||
114 | 'physical_office_willing' => 'boolean', |
||
115 | 'security_clearance_id' => 'int', |
||
116 | 'share_with_managers' => 'boolean', |
||
117 | ]; |
||
118 | protected $fillable = [ |
||
119 | 'citizenship_declaration_id', |
||
120 | 'veteran_status_id', |
||
121 | 'preferred_language_id', |
||
122 | 'language_requirement_confirmed', |
||
123 | 'language_test_confirmed', |
||
124 | 'education_requirement_confirmed', |
||
125 | 'version_id', |
||
126 | 'veteran_status_id', |
||
127 | 'preferred_language_id', |
||
128 | 'submission_signature', |
||
129 | 'submission_date', |
||
130 | 'experience_saved', |
||
131 | 'director_name', |
||
132 | 'director_title', |
||
133 | 'director_email', |
||
134 | 'reference_name', |
||
135 | 'reference_title', |
||
136 | 'reference_email', |
||
137 | 'gov_email', |
||
138 | 'physical_office_willing', |
||
139 | 'security_clearance_id', |
||
140 | 'share_with_managers', |
||
141 | ]; |
||
142 | |||
143 | /** |
||
144 | * The accessors to append to the model's array/json form. |
||
145 | * |
||
146 | * @var array |
||
147 | */ |
||
148 | protected $appends = ['meets_essential_criteria']; |
||
149 | |||
150 | protected function createApplicantSnapshot($applicant_id) |
||
151 | { |
||
152 | $applicant = Applicant::where('id', $applicant_id)->firstOrFail(); |
||
153 | |||
154 | $snapshot = $applicant->replicate(); |
||
|
|||
155 | } |
||
156 | |||
157 | public function applicant() |
||
158 | { |
||
159 | return $this->belongsTo(\App\Models\Applicant::class); |
||
160 | } |
||
161 | |||
162 | public function applicant_snapshot() //phpcs:ignore |
||
163 | { |
||
164 | return $this->belongsTo(\App\Models\Applicant::class, 'applicant_snapshot_id'); |
||
165 | } |
||
166 | |||
167 | public function application_status() //phpcs:ignore |
||
168 | { |
||
169 | return $this->belongsTo(\App\Models\Lookup\ApplicationStatus::class); |
||
170 | } |
||
171 | |||
172 | public function citizenship_declaration() //phpcs:ignore |
||
173 | { |
||
174 | return $this->belongsTo(\App\Models\Lookup\CitizenshipDeclaration::class); |
||
175 | } |
||
176 | |||
177 | public function veteran_status() //phpcs:ignore |
||
178 | { |
||
179 | return $this->belongsTo(\App\Models\Lookup\VeteranStatus::class); |
||
180 | } |
||
181 | |||
182 | public function preferred_language() //phpcs:ignore |
||
183 | { |
||
184 | return $this->belongsTo(\App\Models\Lookup\PreferredLanguage::class); |
||
185 | } |
||
186 | |||
187 | public function job_poster() //phpcs:ignore |
||
188 | { |
||
189 | return $this->belongsTo(\App\Models\JobPoster::class); |
||
190 | } |
||
191 | |||
192 | public function job_application_answers() //phpcs:ignore |
||
193 | { |
||
194 | return $this->hasMany(\App\Models\JobApplicationAnswer::class); |
||
195 | } |
||
196 | |||
197 | public function skill_declarations() //phpcs:ignore |
||
198 | { |
||
199 | return $this->morphMany(\App\Models\SkillDeclaration::class, 'skillable'); |
||
200 | } |
||
201 | |||
202 | public function application_review() //phpcs:ignore |
||
203 | { |
||
204 | return $this->hasOne(ApplicationReview::class); |
||
205 | } |
||
206 | |||
207 | public function degrees() |
||
208 | { |
||
209 | return $this->morphMany(\App\Models\Degree::class, 'degreeable')->orderBy('end_date', 'desc'); |
||
210 | } |
||
211 | |||
212 | public function courses() |
||
213 | { |
||
214 | return $this->morphMany(\App\Models\Course::class, 'courseable')->orderBy('end_date', 'desc'); |
||
215 | } |
||
216 | |||
217 | public function work_experiences() //phpcs:ignore |
||
220 | } |
||
221 | |||
222 | public function references() |
||
223 | { |
||
224 | return $this->morphMany(\App\Models\Reference::class, 'referenceable'); |
||
225 | } |
||
226 | |||
227 | public function projects() |
||
230 | } |
||
231 | |||
232 | public function work_samples() //phpcs:ignore |
||
233 | { |
||
234 | return $this->morphMany(\App\Models\WorkSample::class, 'work_sampleable'); |
||
235 | } |
||
236 | |||
237 | public function job_application_version() //phpcs:ignore |
||
238 | { |
||
239 | return $this->hasOne(\App\Models\JobApplicationVersion::class, 'version_id'); |
||
240 | } |
||
241 | |||
242 | public function security_clearance() //phpcs:ignore |
||
243 | { |
||
244 | return $this->belongsTo(\App\Models\Lookup\SecurityClearance::class); |
||
245 | } |
||
246 | |||
247 | // Version 2 application models. |
||
248 | public function experiences_work() //phpcs:ignore |
||
249 | { |
||
250 | return $this->morphMany(\App\Models\ExperienceWork::class, 'experienceable') |
||
251 | ->orderBy('end_date', 'desc'); |
||
252 | } |
||
253 | |||
254 | public function experiences_personal() //phpcs:ignore |
||
255 | { |
||
256 | return $this->morphMany(\App\Models\ExperiencePersonal::class, 'experienceable') |
||
257 | ->orderBy('end_date', 'desc'); |
||
258 | } |
||
259 | |||
260 | public function experiences_education() //phpcs:ignore |
||
261 | { |
||
262 | return $this->morphMany(\App\Models\ExperienceEducation::class, 'experienceable') |
||
263 | ->orderBy('end_date', 'desc'); |
||
264 | } |
||
265 | |||
266 | public function experiences_award() //phpcs:ignore |
||
267 | { |
||
268 | return $this->morphMany(\App\Models\ExperienceAward::class, 'experienceable'); |
||
269 | } |
||
270 | |||
271 | public function experiences_community() //phpcs:ignore |
||
272 | { |
||
273 | return $this->morphMany(\App\Models\ExperienceCommunity::class, 'experienceable') |
||
274 | ->orderBy('end_date', 'desc'); |
||
275 | } |
||
276 | |||
277 | public function touched_application_steps() //phpcs:ignore |
||
278 | { |
||
279 | return $this->hasMany(\App\Models\TouchedApplicationStep::class); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Return either 'complete', 'incomplete' or 'error', depending on the |
||
284 | * status of the requested section. |
||
285 | * |
||
286 | * @param string $section Should be one of: |
||
287 | * 'basics' |
||
288 | * 'experience' |
||
289 | * 'essential_skills' |
||
290 | * 'asset_skills' |
||
291 | * 'preview' |
||
292 | * |
||
293 | * @return string $status 'complete', 'incomplete' or 'error' |
||
294 | */ |
||
295 | public function getSectionStatus(string $section) |
||
296 | { |
||
297 | // TODO: determine whether sections are complete or invalid |
||
298 | $jobPoster = $this->job_poster; |
||
299 | $validator = new ApplicationValidator(); |
||
300 | $status = 'incomplete'; |
||
301 | switch ($section) { |
||
302 | case 'basics': |
||
303 | if ($validator->basicsComplete($this)) { |
||
304 | $status = 'complete'; |
||
305 | } |
||
306 | break; |
||
307 | case 'experience': |
||
308 | if ($validator->experienceComplete($this)) { |
||
309 | $status = 'complete'; |
||
310 | } |
||
311 | break; |
||
312 | case 'essential_skills': |
||
313 | if ($validator->essentialSkillsComplete($this)) { |
||
314 | $status = 'complete'; |
||
315 | } |
||
316 | break; |
||
317 | case 'asset_skills': |
||
318 | if ($validator->assetSkillsComplete($this)) { |
||
319 | $status = 'complete'; |
||
320 | } |
||
321 | break; |
||
322 | case 'preview': |
||
323 | if ($validator->basicsComplete($this) && |
||
324 | $validator->experienceComplete($this) && |
||
325 | $validator->essentialSkillsComplete($this) && |
||
326 | $validator->assetSkillsComplete($this) |
||
327 | ) { |
||
328 | $status = 'complete'; |
||
329 | } |
||
330 | break; |
||
331 | case 'confirm': |
||
332 | if ($validator->affirmationComplete($this)) { |
||
333 | $status = 'complete'; |
||
334 | } |
||
335 | break; |
||
336 | default: |
||
337 | $status = 'error'; |
||
338 | break; |
||
339 | } |
||
340 | return $status; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Check if the status of the application is 'draft' |
||
345 | * |
||
346 | * @return boolean |
||
347 | */ |
||
348 | public function isDraft(): bool |
||
349 | { |
||
350 | return $this->application_status->name === 'draft'; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Returns true if this meets all the HARD SKILL essential criteria. |
||
355 | * That means it has attached an SkillDeclaration for each essential criterion, |
||
356 | * with a level at least as high as the required level. |
||
357 | * NOTE: If this application is in draft status, it will use |
||
358 | * SkillDeclarations from the the applicants profile for this check. |
||
359 | * |
||
360 | * @return boolean |
||
361 | */ |
||
362 | public function meetsEssentialCriteria(): bool |
||
363 | { |
||
364 | $essentialCriteria = $this->job_poster->criteria->filter( |
||
365 | function ($value, $key) { |
||
366 | return $value->criteria_type->name == 'essential' |
||
367 | && $value->skill->skill_type->name == 'hard'; |
||
368 | } |
||
369 | ); |
||
370 | $source = $this->isDraft() ? $this->applicant : $this; |
||
371 | foreach ($essentialCriteria as $criterion) { |
||
372 | $skillDeclaration = $source->skill_declarations->where('skill_id', $criterion->skill_id)->first(); |
||
373 | if ($skillDeclaration === null || |
||
374 | $skillDeclaration->skill_level_id < $criterion->skill_level_id |
||
375 | ) { |
||
376 | return false; |
||
377 | } |
||
378 | } |
||
379 | return true; |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * Accessor for meetsEssentialCriteria function, which |
||
384 | * allows this value to be automatically appended to array/json representation. |
||
385 | * |
||
386 | * @return boolean |
||
387 | */ |
||
388 | public function getMeetsEssentialCriteriaAttribute(): bool |
||
389 | { |
||
390 | return $this->meetsEssentialCriteria(); |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Save copies of all relevant profile data to this application. |
||
395 | * |
||
396 | * |
||
397 | * @return void |
||
398 | */ |
||
399 | public function saveProfileSnapshot(): void |
||
476 | } |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Save copies of Experiences and its linked skills (ExperienceSkills) to this application. |
||
481 | * |
||
482 | * @return void |
||
483 | */ |
||
484 | public function saveProfileSnapshotTimeline(): void |
||
485 | { |
||
486 | $this->refresh(); |
||
487 | $applicant = $this->applicant->fresh(); |
||
488 | $this->user_name = $applicant->user->full_name; |
||
489 | $this->user_email = $applicant->user->email; |
||
490 | $this->save(); |
||
491 | |||
492 | $deleteExperiences = function ($experiences) { |
||
493 | foreach ($experiences as $experience) { |
||
494 | $experience->delete(); |
||
495 | } |
||
496 | }; |
||
497 | |||
498 | // Delete experiences in previous snapshot. |
||
499 | $deleteExperiences($this->experiences_award); |
||
500 | $deleteExperiences($this->experiences_community); |
||
501 | $deleteExperiences($this->experiences_education); |
||
502 | $deleteExperiences($this->experiences_personal); |
||
503 | $deleteExperiences($this->experiences_work); |
||
504 | |||
505 | $replicateAndSaveExperience = function ($experiences, $experience_type) { |
||
506 | // Iterate through applicant experiences, replicate the experience, and save to the application. |
||
507 | foreach ($experiences as $experience) { |
||
508 | $experienceCopy = $experience->replicate(); |
||
509 | $this->{$experience_type}()->save($experienceCopy); |
||
510 | // Iterate through original experience experienceSkills list, replicate it, and save to the new copy. |
||
511 | foreach ($experience->experience_skills as $experienceSkill) { |
||
512 | $experienceSkillCopy = $experienceSkill->replicate(); |
||
513 | $experienceCopy->experience_skills()->save($experienceSkillCopy); |
||
514 | } |
||
515 | } |
||
516 | }; |
||
517 | |||
518 | $replicateAndSaveExperience($applicant->experiences_award, 'experiences_award'); |
||
519 | $replicateAndSaveExperience($applicant->experiences_community, 'experiences_community'); |
||
520 | $replicateAndSaveExperience($applicant->experiences_education, 'experiences_education'); |
||
521 | $replicateAndSaveExperience($applicant->experiences_personal, 'experiences_personal'); |
||
522 | $replicateAndSaveExperience($applicant->experiences_work, 'experiences_work'); |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * Attach steps to new application (version 2). |
||
527 | * |
||
528 | * @return void |
||
529 | */ |
||
530 | public function attachSteps(): void |
||
531 | { |
||
532 | if ($this->touched_application_steps->isEmpty()) { |
||
533 | $basicStep = new TouchedApplicationStep(); |
||
534 | $basicStep->step_id = JobApplicationStep::where('name', 'basic')->first()->id; |
||
535 | $this->touched_application_steps()->save($basicStep); |
||
536 | |||
537 | $experienceStep = new TouchedApplicationStep(); |
||
538 | $experienceStep->step_id = JobApplicationStep::where('name', 'experience')->first()->id; |
||
539 | $this->touched_application_steps()->save($experienceStep); |
||
540 | |||
541 | $skillsStep = new TouchedApplicationStep(); |
||
542 | $skillsStep->step_id = JobApplicationStep::where('name', 'skills')->first()->id; |
||
543 | $this->touched_application_steps()->save($skillsStep); |
||
544 | |||
545 | $fitStep = new TouchedApplicationStep(); |
||
546 | $fitStep->step_id = JobApplicationStep::where('name', 'fit')->first()->id; |
||
547 | $this->touched_application_steps()->save($fitStep); |
||
548 | |||
549 | $reviewStep = new TouchedApplicationStep(); |
||
550 | $reviewStep->step_id = JobApplicationStep::where('name', 'review')->first()->id; |
||
551 | $this->touched_application_steps()->save($reviewStep); |
||
552 | |||
553 | $submissionStep = new TouchedApplicationStep(); |
||
554 | $submissionStep->step_id = JobApplicationStep::where('name', 'submission')->first()->id; |
||
555 | $this->touched_application_steps()->save($submissionStep); |
||
556 | $this->save(); |
||
557 | $this->refresh(); |
||
558 | }; |
||
559 | } |
||
560 | |||
561 | /** |
||
562 | * Calculates and returns an associative array of application steps (version 2) with the value equal |
||
563 | * to it's status ('default', 'complete', 'error'). |
||
564 | * |
||
565 | * @return string $jobApplicationSteps |
||
566 | */ |
||
567 | public function jobApplicationSteps(): array |
||
612 | } |
||
613 | } |
||
614 |