| Total Complexity | 50 | 
| Total Lines | 358 | 
| 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  | 
            ||
| 63 | class JobApplication extends BaseModel  | 
            ||
| 64 | { | 
            ||
| 65 | |||
| 66 | use Notifiable;  | 
            ||
| 67 | |||
| 68 | protected $dispatchesEvents = [  | 
            ||
| 69 | 'retrieved' => ApplicationRetrieved::class,  | 
            ||
| 70 | 'saved' => ApplicationSaved::class,  | 
            ||
| 71 | ];  | 
            ||
| 72 | |||
| 73 | protected $casts = [  | 
            ||
| 74 | 'job_poster_id' => 'int',  | 
            ||
| 75 | 'application_status_id' => 'int',  | 
            ||
| 76 | 'citizenship_declaration_id' => 'int',  | 
            ||
| 77 | 'veteran_status_id' => 'int',  | 
            ||
| 78 | 'preferred_language_id' => 'int',  | 
            ||
| 79 | 'applicant_id' => 'int',  | 
            ||
| 80 | 'applicant_snapshot_id' => 'int',  | 
            ||
| 81 | 'submission_signature' => 'string',  | 
            ||
| 82 | 'submission_date' => 'string',  | 
            ||
| 83 | 'experience_saved' => 'boolean',  | 
            ||
| 84 | 'language_requirement_confirmed' => 'boolean',  | 
            ||
| 85 | 'version_id' => 'int',  | 
            ||
| 86 | ];  | 
            ||
| 87 | protected $fillable = [  | 
            ||
| 88 | 'citizenship_declaration_id',  | 
            ||
| 89 | 'language_requirement_confirmed',  | 
            ||
| 90 | 'veteran_status_id',  | 
            ||
| 91 | 'preferred_language_id',  | 
            ||
| 92 | 'submission_signature',  | 
            ||
| 93 | 'submission_date',  | 
            ||
| 94 | 'experience_saved',  | 
            ||
| 95 | ];  | 
            ||
| 96 | |||
| 97 | /**  | 
            ||
| 98 | * The accessors to append to the model's array/json form.  | 
            ||
| 99 | *  | 
            ||
| 100 | * @var array  | 
            ||
| 101 | */  | 
            ||
| 102 | protected $appends = ['meets_essential_criteria'];  | 
            ||
| 103 | |||
| 104 | protected function createApplicantSnapshot($applicant_id)  | 
            ||
| 105 |     { | 
            ||
| 106 |         $applicant = Applicant::where('id', $applicant_id)->firstOrFail(); | 
            ||
| 107 | |||
| 108 | $snapshot = $applicant->replicate();  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 109 | }  | 
            ||
| 110 | |||
| 111 | public function applicant()  | 
            ||
| 112 |     { | 
            ||
| 113 | return $this->belongsTo(\App\Models\Applicant::class);  | 
            ||
| 114 | }  | 
            ||
| 115 | |||
| 116 | public function applicant_snapshot() //phpcs:ignore  | 
            ||
| 117 |     { | 
            ||
| 118 | return $this->belongsTo(\App\Models\Applicant::class, 'applicant_snapshot_id');  | 
            ||
| 119 | }  | 
            ||
| 120 | |||
| 121 | public function application_status() //phpcs:ignore  | 
            ||
| 122 |     { | 
            ||
| 123 | return $this->belongsTo(\App\Models\Lookup\ApplicationStatus::class);  | 
            ||
| 124 | }  | 
            ||
| 125 | |||
| 126 | public function citizenship_declaration() //phpcs:ignore  | 
            ||
| 127 |     { | 
            ||
| 128 | return $this->belongsTo(\App\Models\Lookup\CitizenshipDeclaration::class);  | 
            ||
| 129 | }  | 
            ||
| 130 | |||
| 131 | public function veteran_status() //phpcs:ignore  | 
            ||
| 132 |     { | 
            ||
| 133 | return $this->belongsTo(\App\Models\Lookup\VeteranStatus::class);  | 
            ||
| 134 | }  | 
            ||
| 135 | |||
| 136 | public function preferred_language() //phpcs:ignore  | 
            ||
| 137 |     { | 
            ||
| 138 | return $this->belongsTo(\App\Models\Lookup\PreferredLanguage::class);  | 
            ||
| 139 | }  | 
            ||
| 140 | |||
| 141 | public function job_poster() //phpcs:ignore  | 
            ||
| 142 |     { | 
            ||
| 143 | return $this->belongsTo(\App\Models\JobPoster::class);  | 
            ||
| 144 | }  | 
            ||
| 145 | |||
| 146 | public function job_application_answers() //phpcs:ignore  | 
            ||
| 147 |     { | 
            ||
| 148 | return $this->hasMany(\App\Models\JobApplicationAnswer::class);  | 
            ||
| 149 | }  | 
            ||
| 150 | |||
| 151 | public function skill_declarations() //phpcs:ignore  | 
            ||
| 152 |     { | 
            ||
| 153 | return $this->morphMany(\App\Models\SkillDeclaration::class, 'skillable');  | 
            ||
| 154 | }  | 
            ||
| 155 | |||
| 156 | public function application_review() //phpcs:ignore  | 
            ||
| 157 |     { | 
            ||
| 158 | return $this->hasOne(ApplicationReview::class);  | 
            ||
| 159 | }  | 
            ||
| 160 | |||
| 161 | public function degrees()  | 
            ||
| 162 |     { | 
            ||
| 163 |         return $this->morphMany(\App\Models\Degree::class, 'degreeable')->orderBy('end_date', 'desc'); | 
            ||
| 164 | }  | 
            ||
| 165 | |||
| 166 | public function courses()  | 
            ||
| 167 |     { | 
            ||
| 168 |         return $this->morphMany(\App\Models\Course::class, 'courseable')->orderBy('end_date', 'desc'); | 
            ||
| 169 | }  | 
            ||
| 170 | |||
| 171 | public function work_experiences() //phpcs:ignore  | 
            ||
| 174 | }  | 
            ||
| 175 | |||
| 176 | public function references()  | 
            ||
| 177 |     { | 
            ||
| 178 | return $this->morphMany(\App\Models\Reference::class, 'referenceable');  | 
            ||
| 179 | }  | 
            ||
| 180 | |||
| 181 | public function projects()  | 
            ||
| 184 | }  | 
            ||
| 185 | |||
| 186 | public function work_samples() //phpcs:ignore  | 
            ||
| 187 |     { | 
            ||
| 188 | return $this->morphMany(\App\Models\WorkSample::class, 'work_sampleable');  | 
            ||
| 189 | }  | 
            ||
| 190 | |||
| 191 | public function job_application_version() //phpcs:ignore  | 
            ||
| 192 |     { | 
            ||
| 193 | return $this->hasOne(\App\Models\JobApplicationVersion::class);  | 
            ||
| 194 | }  | 
            ||
| 195 | |||
| 196 | // Version 2 application models.  | 
            ||
| 197 | public function experiences_work() //phpcs:ignore  | 
            ||
| 198 |     { | 
            ||
| 199 | return $this->morphMany(\App\Models\ExperienceWork::class, 'experienceable')  | 
            ||
| 200 |             ->orderBy('end_date', 'desc'); | 
            ||
| 201 | }  | 
            ||
| 202 | |||
| 203 | public function experiences_personal() //phpcs:ignore  | 
            ||
| 204 |     { | 
            ||
| 205 | return $this->morphMany(\App\Models\ExperiencePersonal::class, 'experienceable')  | 
            ||
| 206 |             ->orderBy('end_date', 'desc'); | 
            ||
| 207 | }  | 
            ||
| 208 | |||
| 209 | public function experiences_education() //phpcs:ignore  | 
            ||
| 213 | }  | 
            ||
| 214 | |||
| 215 | public function experiences_award() //phpcs:ignore  | 
            ||
| 216 |     { | 
            ||
| 217 | return $this->morphMany(\App\Models\ExperienceAward::class, 'experienceable');  | 
            ||
| 218 | }  | 
            ||
| 219 | |||
| 220 | public function experiences_community() //phpcs:ignore  | 
            ||
| 221 |     { | 
            ||
| 222 | return $this->morphMany(\App\Models\ExperienceCommunity::class, 'experienceable')  | 
            ||
| 223 |             ->orderBy('end_date', 'desc'); | 
            ||
| 224 | }  | 
            ||
| 225 | |||
| 226 | /**  | 
            ||
| 227 | * Return either 'complete', 'incomplete' or 'error', depending on the  | 
            ||
| 228 | * status of the requested section.  | 
            ||
| 229 | *  | 
            ||
| 230 | * @param string $section Should be one of:  | 
            ||
| 231 | * 'basics'  | 
            ||
| 232 | * 'experience'  | 
            ||
| 233 | * 'essential_skills'  | 
            ||
| 234 | * 'asset_skills'  | 
            ||
| 235 | * 'preview'  | 
            ||
| 236 | *  | 
            ||
| 237 | * @return string $status 'complete', 'incomplete' or 'error'  | 
            ||
| 238 | */  | 
            ||
| 239 | public function getSectionStatus(string $section)  | 
            ||
| 240 |     { | 
            ||
| 241 | // TODO: determine whether sections are complete or invalid  | 
            ||
| 242 | $validator = new ApplicationValidator();  | 
            ||
| 243 | $status = 'incomplete';  | 
            ||
| 244 |         switch ($section) { | 
            ||
| 245 | case 'basics':  | 
            ||
| 246 |                 if ($validator->basicsComplete($this)) { | 
            ||
| 247 | $status = 'complete';  | 
            ||
| 248 | }  | 
            ||
| 249 | break;  | 
            ||
| 250 | case 'experience':  | 
            ||
| 251 |                 if ($validator->experienceComplete($this)) { | 
            ||
| 252 | $status = 'complete';  | 
            ||
| 253 | }  | 
            ||
| 254 | break;  | 
            ||
| 255 | case 'essential_skills':  | 
            ||
| 256 |                 if ($validator->essentialSkillsComplete($this)) { | 
            ||
| 257 | $status = 'complete';  | 
            ||
| 258 | }  | 
            ||
| 259 | break;  | 
            ||
| 260 | case 'asset_skills':  | 
            ||
| 261 |                 if ($validator->assetSkillsComplete($this)) { | 
            ||
| 262 | $status = 'complete';  | 
            ||
| 263 | }  | 
            ||
| 264 | break;  | 
            ||
| 265 | case 'preview':  | 
            ||
| 266 | if (  | 
            ||
| 267 | $validator->basicsComplete($this) &&  | 
            ||
| 268 | $validator->experienceComplete($this) &&  | 
            ||
| 269 | $validator->essentialSkillsComplete($this) &&  | 
            ||
| 270 | $validator->assetSkillsComplete($this)  | 
            ||
| 271 |                 ) { | 
            ||
| 272 | $status = 'complete';  | 
            ||
| 273 | }  | 
            ||
| 274 | break;  | 
            ||
| 275 | case 'confirm':  | 
            ||
| 276 |                 if ($validator->affirmationComplete($this)) { | 
            ||
| 277 | $status = 'complete';  | 
            ||
| 278 | }  | 
            ||
| 279 | break;  | 
            ||
| 280 | default:  | 
            ||
| 281 | $status = 'error';  | 
            ||
| 282 | break;  | 
            ||
| 283 | }  | 
            ||
| 284 | return $status;  | 
            ||
| 285 | }  | 
            ||
| 286 | |||
| 287 | /**  | 
            ||
| 288 | * Check if the status of the application is 'draft'  | 
            ||
| 289 | *  | 
            ||
| 290 | * @return boolean  | 
            ||
| 291 | */  | 
            ||
| 292 | public function isDraft(): bool  | 
            ||
| 293 |     { | 
            ||
| 294 | return $this->application_status->name === 'draft';  | 
            ||
| 295 | }  | 
            ||
| 296 | |||
| 297 | /**  | 
            ||
| 298 | * Returns true if this meets all the essential criteria.  | 
            ||
| 299 | * That means it has attached an SkillDeclaration for each essential criterion,  | 
            ||
| 300 | * with a level at least as high as the required level.  | 
            ||
| 301 | * NOTE: If this application is in draft status, it will use  | 
            ||
| 302 | * SkillDeclarations from the the applicants profile for this check.  | 
            ||
| 303 | *  | 
            ||
| 304 | * @return boolean  | 
            ||
| 305 | */  | 
            ||
| 306 | public function meetsEssentialCriteria(): bool  | 
            ||
| 307 |     { | 
            ||
| 308 | $essentialCriteria = $this->job_poster->criteria->filter(  | 
            ||
| 309 |             function ($value, $key) { | 
            ||
| 310 | return $value->criteria_type->name == 'essential';  | 
            ||
| 311 | }  | 
            ||
| 312 | );  | 
            ||
| 313 | $source = $this->isDraft() ? $this->applicant : $this;  | 
            ||
| 314 |         foreach ($essentialCriteria as $criterion) { | 
            ||
| 315 |             $skillDeclaration = $source->skill_declarations->where('skill_id', $criterion->skill_id)->first(); | 
            ||
| 316 | if (  | 
            ||
| 317 | $skillDeclaration === null ||  | 
            ||
| 318 | $skillDeclaration->skill_level_id < $criterion->skill_level_id  | 
            ||
| 319 |             ) { | 
            ||
| 320 | return false;  | 
            ||
| 321 | }  | 
            ||
| 322 | }  | 
            ||
| 323 | return true;  | 
            ||
| 324 | }  | 
            ||
| 325 | |||
| 326 | /**  | 
            ||
| 327 | * Accessor for meetsEssentialCriteria function, which  | 
            ||
| 328 | * allows this value to be automatically appended to array/json representation.  | 
            ||
| 329 | *  | 
            ||
| 330 | * @return boolean  | 
            ||
| 331 | */  | 
            ||
| 332 | public function getMeetsEssentialCriteriaAttribute(): bool  | 
            ||
| 333 |     { | 
            ||
| 334 | return $this->meetsEssentialCriteria();  | 
            ||
| 335 | }  | 
            ||
| 336 | |||
| 337 | /**  | 
            ||
| 338 | * Save copies of all relevant profile data to this application.  | 
            ||
| 339 | *  | 
            ||
| 340 | *  | 
            ||
| 341 | * @return void  | 
            ||
| 342 | */  | 
            ||
| 343 | public function saveProfileSnapshot(): void  | 
            ||
| 421 | }  | 
            ||
| 422 | }  | 
            ||
| 423 | }  | 
            ||
| 424 |