| Total Complexity | 44 |
| Total Lines | 282 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 4 | 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 |
||
| 16 | class Student extends Model implements HasMedia |
||
| 17 | { |
||
| 18 | use CrudTrait; |
||
|
|
|||
| 19 | use InteractsWithMedia; |
||
| 20 | use LogsActivity; |
||
| 21 | |||
| 22 | public $timestamps = true; |
||
| 23 | protected $guarded = []; |
||
| 24 | public $incrementing = false; |
||
| 25 | protected $with = ['user', 'phone', 'institution', 'profession']; |
||
| 26 | protected $appends = ['email', 'name', 'firstname', 'lastname', 'student_age', 'student_birthdate', 'lead_status', 'is_enrolled']; |
||
| 27 | protected static $logUnguarded = true; |
||
| 28 | |||
| 29 | protected static function boot() |
||
| 37 | }); |
||
| 38 | } |
||
| 39 | |||
| 40 | public function scopeComputedLeadType($query, $leadTypeId) |
||
| 41 | { |
||
| 42 | switch ($leadTypeId) { |
||
| 43 | case 1: // active / enrolled students |
||
| 44 | return $query->whereHas('enrollments', function ($query) use ($leadTypeId) { |
||
| 45 | return $query->whereHas('course', function ($q) use ($leadTypeId) { |
||
| 46 | $q->where('period_id', Period::get_default_period()->id); |
||
| 47 | }); |
||
| 48 | }); |
||
| 49 | break; |
||
| 50 | |||
| 51 | case 2: // custom lead status |
||
| 52 | case 3: |
||
| 53 | return $query->where('lead_type_id', $leadTypeId); |
||
| 54 | break; |
||
| 55 | |||
| 56 | case 4: // old students who have at least one enrollment |
||
| 57 | return $query |
||
| 58 | ->where('lead_type_id', $leadTypeId) |
||
| 59 | ->orWhere(function ($query) use ($leadTypeId) { |
||
| 60 | $query |
||
| 61 | ->whereNull('lead_type_id') |
||
| 62 | ->whereHas('enrollments', function ($query) use ($leadTypeId) { |
||
| 63 | return $query |
||
| 64 | ->whereHas('course', function ($q) use ($leadTypeId) { |
||
| 65 | $q->where('period_id', '!=', Period::get_default_period()->id); |
||
| 66 | }); |
||
| 67 | }) |
||
| 68 | ->whereDoesntHave('enrollments', function ($query) use ($leadTypeId) { |
||
| 69 | return $query |
||
| 70 | ->whereHas('course', function ($q) use ($leadTypeId) { |
||
| 71 | $q->where('period_id', Period::get_default_period()->id); |
||
| 72 | }); |
||
| 73 | }); |
||
| 74 | }); |
||
| 75 | break; |
||
| 76 | default: |
||
| 77 | return $query; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | public function registerMediaConversions(Media $media = null): void |
||
| 82 | { |
||
| 83 | $this->addMediaConversion('thumb') |
||
| 84 | ->fit(Manipulations::FIT_MAX, 1200, 1200) |
||
| 85 | ->optimize()->nonQueued(); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** relations */ |
||
| 89 | public function user() |
||
| 90 | { |
||
| 91 | return $this->belongsTo(User::class, 'id', 'id'); |
||
| 92 | } |
||
| 93 | |||
| 94 | public function attendance() |
||
| 95 | { |
||
| 96 | return $this->hasMany(Attendance::class); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function periodAbsences(Period $period = null) |
||
| 100 | { |
||
| 101 | if ($period == null) { |
||
| 102 | $period = $this->currentPeriod; |
||
| 103 | } |
||
| 104 | |||
| 105 | return $this->hasMany(Attendance::class) |
||
| 106 | ->where('attendance_type_id', 4) // absence |
||
| 107 | ->whereHas('event', function ($q) use ($period) { |
||
| 108 | return $q->whereHas('course', function ($c) use ($period) { |
||
| 109 | return $c->where('period_id', $period->id); |
||
| 110 | }); |
||
| 111 | }); |
||
| 112 | } |
||
| 113 | |||
| 114 | public function periodAttendance(Period $period = null) |
||
| 115 | { |
||
| 116 | if ($period == null) { |
||
| 117 | $period = Period::get_default_period(); |
||
| 118 | } |
||
| 119 | |||
| 120 | return $this->hasMany(Attendance::class) |
||
| 121 | ->whereHas('event', function ($q) use ($period) { |
||
| 122 | return $q->whereHas('course', function ($c) use ($period) { |
||
| 123 | return $c->where('period_id', $period->id); |
||
| 124 | }); |
||
| 125 | }); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function contacts() |
||
| 129 | { |
||
| 130 | return $this->hasMany(Contact::class, 'student_id'); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function comments() |
||
| 134 | { |
||
| 135 | return $this->morphMany(Comment::class, 'commentable'); |
||
| 136 | } |
||
| 137 | |||
| 138 | public function phone() |
||
| 139 | { |
||
| 140 | return $this->morphMany(PhoneNumber::class, 'phoneable'); |
||
| 141 | } |
||
| 142 | |||
| 143 | public function enrollments() |
||
| 144 | { |
||
| 145 | return $this->hasMany(Enrollment::class) |
||
| 146 | ->with('course'); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function leadType() |
||
| 150 | { |
||
| 151 | return $this->belongsTo(LeadType::class); |
||
| 152 | } |
||
| 153 | |||
| 154 | public function real_enrollments() |
||
| 155 | { |
||
| 156 | return $this->hasMany(Enrollment::class) |
||
| 157 | ->with('course') |
||
| 158 | ->whereIn('status_id', ['1', '2']) |
||
| 159 | ->whereDoesntHave('childrenEnrollments'); |
||
| 160 | } |
||
| 161 | |||
| 162 | public function institution() |
||
| 165 | } |
||
| 166 | |||
| 167 | public function profession() |
||
| 168 | { |
||
| 169 | return $this->belongsTo(Profession::class); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** attributes */ |
||
| 173 | public function getFirstnameAttribute() |
||
| 174 | { |
||
| 175 | if ($this->user) { |
||
| 176 | return Str::title($this->user->firstname); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | public function getLastnameAttribute() |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | public function getEmailAttribute() |
||
| 188 | { |
||
| 189 | if ($this->user) { |
||
| 190 | return $this->user->email; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | public function getNameAttribute() |
||
| 195 | { |
||
| 196 | if ($this->user) { |
||
| 197 | return $this->firstname.' '.$this->lastname; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | public function getStudentAgeAttribute() |
||
| 202 | { |
||
| 203 | return Carbon::parse($this->birthdate)->age ?? ''; |
||
| 204 | } |
||
| 205 | |||
| 206 | public function getStudentBirthdateAttribute() |
||
| 207 | { |
||
| 208 | return Carbon::parse($this->birthdate)->locale(App::getLocale())->isoFormat('LL'); |
||
| 209 | } |
||
| 210 | |||
| 211 | public function getIsEnrolledAttribute() |
||
| 212 | { |
||
| 213 | // if the student is currently enrolled |
||
| 214 | if ($this->enrollments()->whereHas('course', function ($q) { |
||
| 215 | return $q->where('period_id', Period::get_default_period()->id); |
||
| 216 | })->count() > 0) { |
||
| 217 | return 1; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | public function getLeadStatusNameAttribute() |
||
| 222 | { |
||
| 223 | return LeadType::find($this->lead_status)->name; |
||
| 224 | } |
||
| 225 | |||
| 226 | public function getLeadStatusAttribute() |
||
| 227 | { |
||
| 228 | // if the student is currently enrolled, they are CONVERTED |
||
| 229 | if ($this->is_enrolled) { |
||
| 230 | return 1; |
||
| 231 | } |
||
| 232 | |||
| 233 | // if the student has a special status, return it |
||
| 234 | if ($this->leadType != null) { |
||
| 235 | return $this->leadType->id; |
||
| 236 | } |
||
| 237 | // if the student was previously enrolled, they must be potential students |
||
| 238 | elseif ($this->has('enrollments')) { |
||
| 239 | return 4; |
||
| 240 | } else { |
||
| 241 | return; |
||
| 242 | } |
||
| 243 | // otherwise, their status cannot be determined and should be left blank |
||
| 244 | } |
||
| 245 | |||
| 246 | /** functions */ |
||
| 247 | |||
| 248 | /** |
||
| 249 | * enroll the student in a course. |
||
| 250 | * If the course has any children, we also enroll the student in the children courses. |
||
| 251 | */ |
||
| 252 | public function enroll(Course $course): int |
||
| 253 | { |
||
| 254 | // avoid duplicates by retrieving an potential existing enrollment for the same course |
||
| 255 | $enrollment = Enrollment::firstOrNew([ |
||
| 256 | 'student_id' => $this->id, |
||
| 257 | 'course_id' => $course->id, |
||
| 258 | ]); |
||
| 259 | |||
| 260 | $enrollment->responsible_id = backpack_user()->id ?? 1; |
||
| 261 | $enrollment->save(); |
||
| 262 | |||
| 263 | // if the course has children, enroll in children as well. |
||
| 264 | if ($course->children_count > 0) { |
||
| 265 | foreach ($course->children as $children_course) { |
||
| 266 | $child_enrollment = Enrollment::firstOrNew([ |
||
| 267 | 'student_id' => $this->id, |
||
| 268 | 'course_id' => $children_course->id, |
||
| 269 | 'parent_id' => $enrollment->id, |
||
| 270 | ]); |
||
| 271 | $child_enrollment->responsible_id = backpack_user()->id ?? null; |
||
| 272 | $child_enrollment->save(); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | $this->update(['lead_type_id' => null]); // fallback to default (converted) |
||
| 277 | |||
| 278 | return $enrollment->id; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** SETTERS */ |
||
| 282 | public function setFirstnameAttribute($value) |
||
| 283 | { |
||
| 284 | $this->user->firstname = $value; |
||
| 285 | $this->user->save(); |
||
| 286 | } |
||
| 287 | |||
| 288 | public function setLastnameAttribute($value) |
||
| 289 | { |
||
| 290 | $this->user->lastname = $value; |
||
| 291 | $this->user->save(); |
||
| 292 | } |
||
| 293 | |||
| 294 | public function setEmailAttribute($value) |
||
| 298 | } |
||
| 299 | } |
||
| 300 |