| Conditions | 12 |
| Paths | 3 |
| Total Lines | 53 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 12 | public function handle(CourseUpdated $event) |
||
| 13 | { |
||
| 14 | $course = $event->course; |
||
| 15 | |||
| 16 | // if course dates have changed, sync all events |
||
| 17 | if ($course->isDirty('start_date') || $course->isDirty('end_date') || $course->relationLoaded('times') && $course->times->isDirty()) { |
||
| 18 | Log::info('cleaning the events after course date change'); |
||
| 19 | |||
| 20 | if ($course->events->count() > 0) { |
||
| 21 | // create events before first existing event and after course start |
||
| 22 | $firstEvent = $course->events()->reorder('start')->first(); |
||
| 23 | $firstEventDate = Carbon::parse($firstEvent->start)->startOfDay(); |
||
| 24 | $lastEvent = $course->events()->reorder('start', 'desc')->first(); |
||
| 25 | $lastEventDate = Carbon::parse($lastEvent->start)->endOfDay(); |
||
| 26 | } |
||
| 27 | |||
| 28 | $courseStartDate = Carbon::parse($course->start_date)->startOfDay(); |
||
| 29 | $courseEndDate = Carbon::parse($course->end_date)->startOfDay(); |
||
| 30 | |||
| 31 | // delete events before course start date |
||
| 32 | Event::where('course_id', $course->id)->where('start', '<', $courseStartDate->startOfDay())->delete(); |
||
| 33 | // delete events after course end date |
||
| 34 | Event::where('course_id', $course->id)->where('end', '>', $courseEndDate->endOfDay())->delete(); |
||
| 35 | |||
| 36 | // for each day before the first event |
||
| 37 | while ($courseStartDate < $courseEndDate) { |
||
| 38 | // Event already exists |
||
| 39 | if (isset($firstEventDate) && isset($lastEventDate) && ($firstEventDate <= $courseStartDate) && ($courseStartDate <= $lastEventDate)) { |
||
| 40 | $courseStartDate->addDay(); |
||
| 41 | continue; |
||
| 42 | } |
||
| 43 | |||
| 44 | // if there is a coursetime for today, create the event |
||
| 45 | if ($course->times->contains('day', $courseStartDate->format('w'))) { |
||
| 46 | Event::create([ |
||
| 47 | 'course_id' => $course->id, |
||
| 48 | 'teacher_id' => $course->teacher_id, |
||
| 49 | 'room_id' => $course->room_id, |
||
| 50 | 'start' => $courseStartDate->setTimeFromTimeString($course->times->where('day', $courseStartDate->format('w'))->first()->start)->toDateTimeString(), |
||
| 51 | 'end' => $courseStartDate->setTimeFromTimeString($course->times->where('day', $courseStartDate->format('w'))->first()->end)->toDateTimeString(), |
||
| 52 | 'name' => $course->name, |
||
| 53 | 'course_time_id' => $course->times->where('day', $courseStartDate->format('w'))->first()->id, |
||
| 54 | 'exempt_attendance' => $course->exempt_attendance, |
||
| 55 | ]); |
||
| 56 | } |
||
| 57 | $courseStartDate->addDay(); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | // update course events with new room, teacher and name |
||
| 62 | Event::where('course_id', $course->id)->update(['room_id' => $course->room_id]); |
||
| 63 | Event::where('course_id', $course->id)->update(['teacher_id' => $course->teacher_id]); |
||
| 64 | Event::where('course_id', $course->id)->update(['name' => $course->name]); |
||
| 65 | } |
||
| 67 |