CleanChildrenEnrollments   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 13
c 1
b 1
f 0
dl 0
loc 31
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 29 7
1
<?php
2
3
namespace App\Listeners;
4
5
use App\Events\EnrollmentUpdating;
6
use App\Models\Attendance;
7
use App\Models\Enrollment;
8
9
class CleanChildrenEnrollments
10
{
11
    public function handle(EnrollmentUpdating $event)
12
    {
13
        $enrollment = $event->enrollment;
14
15
        // If the course was changed, also update children
16
        if ($enrollment->isDirty('course_id')) {
17
            // if enrollment has children, delete them
18
            Enrollment::where('parent_id', $enrollment->id)->delete();
19
20
            // delete attendance
21
            foreach ($enrollment->course->events as $event) {
22
                Attendance::where('event_id', $event->id)->where('student_id', $enrollment->student_id)->delete();
23
            }
24
25
            foreach ($enrollment->course->children as $child) {
26
                foreach ($child->events as $event) {
27
                    Attendance::where('event_id', $event->id)->where('student_id', $enrollment->student_id)->delete();
28
                }
29
            }
30
31
            // TODO delete grades and/or skills
32
            // TODO dispatch event to LMS
33
        }
34
35
        // If the status has changed to paid, also update children
36
        if ($enrollment->isDirty('status_id')) {
37
            foreach ($enrollment->childrenEnrollments as $child) {
38
                $child->status_id = $enrollment->status_id;
39
                $child->save();
40
            }
41
        }
42
    }
43
}
44