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