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