Passed
Branch master (a9cf56)
by Francisco
07:28
created

EnrollmentsImport   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 61
rs 10
c 1
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B onRow() 0 54 6
1
<?php
2
3
namespace App\Imports;
4
5
use Maatwebsite\Excel\Row;
6
use App\Judite\Models\User;
7
use Illuminate\Support\Str;
8
use App\Judite\Models\Course;
9
use App\Judite\Models\Student;
10
use App\Judite\Models\Enrollment;
11
use Maatwebsite\Excel\Concerns\OnEachRow;
12
use App\Exceptions\InvalidFieldValueException;
13
use Maatwebsite\Excel\Concerns\WithHeadingRow;
14
15
class EnrollmentsImport implements OnEachRow, WithHeadingRow
16
{
17
    /**
18
     * The enrollment row to import.
19
     *
20
     * @param \Maatwebsite\Excel\Row $row
21
     */
22
    public function onRow(Row $row)
23
    {
24
        $index = $row->getIndex();
25
        $row = $row->toArray();
26
27
        $student = Student::whereNumber($row['student_id'])
28
            ->firstOrNew(['student_number' => strtolower($row['student_id'])]);
29
30
        if (! $student->exists) {
31
            /** @var \App\Judite\Models\User $user */
32
            $user = User::make([
33
                'name' => $row['student_name'] ?? $row['student_id'],
34
                'email' => strtolower($row['student_email'] ?? $row['student_id'].'@alunos.uminho.pt'),
35
                'password' => bcrypt(Str::random(8)),
36
            ]);
37
            $user->verification_token = Str::random(32);
38
            $user->save();
39
            $student = $user->student()->save($student);
40
        }
41
42
        // Check if the given course id exists
43
        $course = Course::whereCode($row['course_id'])->first();
44
        if ($course === null) {
45
            $exception = new InvalidFieldValueException();
46
            $exception->setField('Course ID', $row['course_id'], "The course {$row['course_id']} does not exist. (at line {$index})");
47
            throw $exception;
48
        }
49
50
        // Check if the enrollment exists
51
        $enrollment = Enrollment::where([
52
            'course_id' => $course->id,
53
            'student_id' => $student->id,
54
        ])->first();
55
56
        if ($enrollment === null) {
57
            $enrollment = $student->enroll($course);
58
        }
59
60
        // Check if the given shift tag exists in the associated course
61
        if ($row['shift'] !== null) {
62
            $shift = $course->getShiftByTag($row['shift']);
63
64
            if ($shift === null) {
65
                $shift = Shift::make(['tag' => $row['shift']]);
0 ignored issues
show
Bug introduced by
The type App\Imports\Shift was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
66
                $course->addShift($shift);
67
            }
68
69
            $enrollment->shift()->associate($shift);
70
        } else {
71
            $enrollment->shift()->dissociate();
72
        }
73
74
        // Add the shift to the enrollment
75
        $enrollment->save();
76
    }
77
}
78