| Total Complexity | 43 |
| Total Lines | 312 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 3 | Features | 0 |
Complex classes like Student 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 Student, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 99 | class Student extends Model implements HasMedia |
||
| 100 | { |
||
| 101 | use CrudTrait; |
||
| 102 | use InteractsWithMedia; |
||
| 103 | use LogsActivity; |
||
| 104 | |||
| 105 | protected $dispatchesEvents = [ |
||
| 106 | 'deleting' => StudentDeleting::class, |
||
| 107 | 'updated' => StudentUpdated::class, |
||
| 108 | ]; |
||
| 109 | |||
| 110 | public $timestamps = true; |
||
| 111 | |||
| 112 | protected $fillable = [ |
||
| 113 | 'id', |
||
| 114 | 'idnumber', |
||
| 115 | 'firstname', |
||
| 116 | 'lastname', |
||
| 117 | 'email', |
||
| 118 | 'address', |
||
| 119 | 'city', |
||
| 120 | 'state', |
||
| 121 | 'country', |
||
| 122 | 'title_id', |
||
| 123 | 'birthdate', |
||
| 124 | 'terms_accepted_at', |
||
| 125 | 'created_at', |
||
| 126 | 'updated_at', |
||
| 127 | 'lead_type_id', |
||
| 128 | 'force_update', |
||
| 129 | 'profession_id', |
||
| 130 | 'institution_id', |
||
| 131 | 'zip_code', |
||
| 132 | 'iban', |
||
| 133 | 'bic', |
||
| 134 | ]; |
||
| 135 | |||
| 136 | public $incrementing = false; |
||
| 137 | |||
| 138 | protected $with = ['user', 'phone', 'institution', 'profession', 'title']; |
||
| 139 | |||
| 140 | protected $appends = ['email', 'name', 'firstname', 'lastname', 'student_age', 'student_birthdate', 'lead_status', 'is_enrolled']; |
||
| 141 | |||
| 142 | protected static $logUnguarded = true; |
||
| 143 | |||
| 144 | public function scopeComputedLeadType($query, $leadTypeId) |
||
| 145 | { |
||
| 146 | return match ($leadTypeId) { |
||
| 147 | 1 => $query->whereHas('enrollments', function ($query) { |
||
| 148 | return $query->whereHas('course', function ($q) { |
||
| 149 | $q->where('period_id', Period::get_default_period()->id); |
||
| 150 | }); |
||
| 151 | }), |
||
| 152 | |||
| 153 | 2, 3 => $query->where('lead_type_id', $leadTypeId), |
||
| 154 | |||
| 155 | 4 => $query |
||
| 156 | ->where('lead_type_id', $leadTypeId) |
||
| 157 | ->orWhere(function ($query) { |
||
| 158 | $query |
||
| 159 | ->whereNull('lead_type_id') |
||
| 160 | ->whereHas('enrollments', function ($query) { |
||
| 161 | return $query |
||
| 162 | ->whereHas('course', function ($q) { |
||
| 163 | $q->where('period_id', '!=', Period::get_default_period()->id); |
||
| 164 | }); |
||
| 165 | }) |
||
| 166 | ->whereDoesntHave('enrollments', function ($query) { |
||
| 167 | return $query |
||
| 168 | ->whereHas('course', function ($q) { |
||
| 169 | $q->where('period_id', Period::get_default_period()->id); |
||
| 170 | }); |
||
| 171 | }); |
||
| 172 | }), |
||
| 173 | |||
| 174 | default => $query, |
||
| 175 | }; |
||
| 176 | } |
||
| 177 | |||
| 178 | public function registerMediaConversions(Media $media = null): void |
||
| 179 | { |
||
| 180 | $this->addMediaConversion('thumb') |
||
| 181 | ->fit(Manipulations::FIT_MAX, 1200, 1200) |
||
| 182 | ->optimize(); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** relations */ |
||
| 186 | public function user() |
||
| 187 | { |
||
| 188 | return $this->belongsTo(User::class, 'id', 'id'); |
||
| 189 | } |
||
| 190 | |||
| 191 | public function attendance() |
||
| 192 | { |
||
| 193 | return $this->hasMany(Attendance::class); |
||
| 194 | } |
||
| 195 | |||
| 196 | public function periodAbsences(Period $period = null) |
||
| 197 | { |
||
| 198 | if ($period == null) { |
||
| 199 | $period = Period::get_default_period(); |
||
| 200 | } |
||
| 201 | |||
| 202 | return $this->hasMany(Attendance::class) |
||
| 203 | ->where('attendance_type_id', 4) // absence |
||
| 204 | ->whereHas('event', function ($q) use ($period) { |
||
| 205 | return $q->whereHas('course', function ($c) use ($period) { |
||
| 206 | return $c->where('period_id', $period->id); |
||
| 207 | }); |
||
| 208 | }); |
||
| 209 | } |
||
| 210 | |||
| 211 | public function periodAttendance(Period $period = null) |
||
| 212 | { |
||
| 213 | if ($period == null) { |
||
| 214 | $period = Period::get_default_period(); |
||
| 215 | } |
||
| 216 | |||
| 217 | return $this->hasMany(Attendance::class) |
||
| 218 | ->whereHas('event', function ($q) use ($period) { |
||
| 219 | return $q->whereHas('course', function ($c) use ($period) { |
||
| 220 | return $c->where('period_id', $period->id); |
||
| 221 | }); |
||
| 222 | }); |
||
| 223 | } |
||
| 224 | |||
| 225 | public function contacts() |
||
| 226 | { |
||
| 227 | return $this->hasMany(Contact::class, 'student_id'); |
||
| 228 | } |
||
| 229 | |||
| 230 | public function comments() |
||
| 231 | { |
||
| 232 | return $this->morphMany(Comment::class, 'commentable'); |
||
| 233 | } |
||
| 234 | |||
| 235 | public function phone() |
||
| 236 | { |
||
| 237 | return $this->morphMany(PhoneNumber::class, 'phoneable'); |
||
| 238 | } |
||
| 239 | |||
| 240 | public function enrollments() |
||
| 241 | { |
||
| 242 | return $this->hasMany(Enrollment::class) |
||
| 243 | ->with('course'); |
||
| 244 | } |
||
| 245 | |||
| 246 | public function leadType() |
||
| 247 | { |
||
| 248 | return $this->belongsTo(LeadType::class); |
||
| 249 | } |
||
| 250 | |||
| 251 | public function real_enrollments() |
||
| 252 | { |
||
| 253 | return $this->hasMany(Enrollment::class) |
||
| 254 | ->with('course') |
||
| 255 | ->whereIn('status_id', ['1', '2']) |
||
| 256 | ->whereDoesntHave('childrenEnrollments'); |
||
| 257 | } |
||
| 258 | |||
| 259 | public function institution() |
||
| 260 | { |
||
| 261 | return $this->belongsTo(Institution::class); |
||
| 262 | } |
||
| 263 | |||
| 264 | public function profession() |
||
| 265 | { |
||
| 266 | return $this->belongsTo(Profession::class); |
||
| 267 | } |
||
| 268 | |||
| 269 | public function title() |
||
| 272 | } |
||
| 273 | |||
| 274 | /** attributes */ |
||
| 275 | public function getFirstnameAttribute(): string |
||
| 276 | { |
||
| 277 | if ($this->user) { |
||
| 278 | return Str::title($this->user->firstname); |
||
| 279 | } |
||
| 280 | return ''; |
||
| 281 | } |
||
| 282 | |||
| 283 | public function getLastnameAttribute(): string |
||
| 289 | } |
||
| 290 | |||
| 291 | public function getEmailAttribute(): string |
||
| 292 | { |
||
| 293 | if ($this->user) { |
||
| 294 | return $this->user->email; |
||
| 295 | } |
||
| 296 | return ''; |
||
| 297 | } |
||
| 298 | |||
| 299 | public function getNameAttribute(): string |
||
| 300 | { |
||
| 301 | if ($this->user) { |
||
| 302 | return ($this->title ? ($this->title->title . ' ') : '') . $this->firstname.' '.$this->lastname; |
||
| 303 | } |
||
| 304 | return ''; |
||
| 305 | } |
||
| 306 | |||
| 307 | public function getStudentAgeAttribute() |
||
| 308 | { |
||
| 309 | return Carbon::parse($this->birthdate)->age ?? ''; |
||
| 310 | } |
||
| 311 | |||
| 312 | public function getStudentBirthdateAttribute() |
||
| 313 | { |
||
| 314 | return Carbon::parse($this->birthdate)->locale(App::getLocale())->isoFormat('LL'); |
||
| 315 | } |
||
| 316 | |||
| 317 | public function getIsEnrolledAttribute() |
||
| 318 | { |
||
| 319 | // if the student is currently enrolled |
||
| 320 | if ($this->enrollments()->whereHas('course', function ($q) { |
||
| 321 | return $q->where('period_id', Period::get_default_period()->id); |
||
| 322 | })->count() > 0) { |
||
| 323 | return 1; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | public function getLeadStatusNameAttribute() |
||
| 328 | { |
||
| 329 | return LeadType::find($this->lead_status)->name; |
||
| 330 | } |
||
| 331 | |||
| 332 | public function getLeadStatusAttribute() |
||
| 333 | { |
||
| 334 | // if the student is currently enrolled, they are CONVERTED |
||
| 335 | if ($this->is_enrolled) { |
||
| 336 | return 1; |
||
| 337 | } |
||
| 338 | |||
| 339 | // if the student has a special status, return it |
||
| 340 | if ($this->lead_type_id == 3 || $this->lead_type_id == 2) { |
||
| 341 | return $this->leadType->value(); |
||
| 342 | } |
||
| 343 | // if the student was previously enrolled, they must be potential students |
||
| 344 | elseif ($this->has('enrollments')) { |
||
| 345 | return 4; |
||
| 346 | } else { |
||
| 347 | return; |
||
| 348 | } |
||
| 349 | // otherwise, their status cannot be determined and should be left blank |
||
| 350 | } |
||
| 351 | |||
| 352 | /** functions */ |
||
| 353 | |||
| 354 | /** |
||
| 355 | * enroll the student in a course. |
||
| 356 | * If the course has any children, we also enroll the student in the children courses. |
||
| 357 | */ |
||
| 358 | public function enroll(Course $course): int |
||
| 359 | { |
||
| 360 | // avoid duplicates by retrieving an potential existing enrollment for the same course |
||
| 361 | $enrollment = Enrollment::firstOrCreate([ |
||
| 362 | 'student_id' => $this->id, |
||
| 363 | 'course_id' => $course->id, |
||
| 364 | ], |
||
| 365 | [ |
||
| 366 | 'responsible_id' => backpack_user()->id ?? 1, |
||
| 367 | ]); |
||
| 368 | |||
| 369 | // if the course has children, enroll in children as well. |
||
| 370 | if ($course->children_count > 0) { |
||
| 371 | foreach ($course->children as $children_course) { |
||
| 372 | Enrollment::firstOrCreate([ |
||
| 373 | 'student_id' => $this->id, |
||
| 374 | 'course_id' => $children_course->id, |
||
| 375 | 'parent_id' => $enrollment->id, |
||
| 376 | ], |
||
| 377 | [ |
||
| 378 | 'responsible_id' => backpack_user()->id ?? 1, |
||
| 379 | ]); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | |||
| 383 | //$this->update(['lead_type_id' => null]); // fallback to default (converted) |
||
| 384 | |||
| 385 | // to subscribe the student to mailing lists and so on |
||
| 386 | $listId = config('mailing-system.mailerlite.activeStudentsListId'); |
||
| 387 | LeadStatusUpdatedEvent::dispatch($this, $listId); |
||
| 388 | |||
| 389 | foreach ($this->contacts as $contact) |
||
| 390 | { |
||
| 391 | LeadStatusUpdatedEvent::dispatch($contact, $listId); |
||
| 392 | } |
||
| 393 | |||
| 394 | return $enrollment->id; |
||
| 395 | } |
||
| 396 | |||
| 397 | /** SETTERS */ |
||
| 398 | public function setFirstnameAttribute($value) |
||
| 399 | { |
||
| 400 | $this->user->update(['firstname' => $value]); |
||
| 401 | } |
||
| 402 | |||
| 403 | public function setLastnameAttribute($value) |
||
| 404 | { |
||
| 405 | $this->user->update(['lastname' => $value]); |
||
| 406 | } |
||
| 407 | |||
| 408 | public function setEmailAttribute($value) |
||
| 411 | } |
||
| 412 | } |
||
| 413 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths