Conditions | 7 |
Paths | 4 |
Total Lines | 70 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
61 | public function storeImport(ImportRequest $request) |
||
62 | { |
||
63 | try { |
||
64 | $file = $request->enrollments; |
||
65 | $path = $file->path(); |
||
66 | |||
67 | Excel::load($path, function ($reader) { |
||
68 | DB::transaction(function () use ($reader) { |
||
69 | // Loop for all the rows of the table |
||
70 | $reader->each(function ($row, $index) { |
||
71 | $index += 2; // Skip header |
||
72 | |||
73 | // Get the models of the given ids |
||
74 | // Check if the given student number exists |
||
75 | $student = Student::whereNumber($row->student_id)->first(); |
||
76 | if ($student === null) { |
||
77 | $user = User::make([ |
||
78 | 'name' => $row->student_name ?? $row->student_id, |
||
79 | 'email' => strtolower($row->student_email ?? $row->student_id.'@alunos.uminho.pt'), |
||
80 | 'password' => bcrypt(str_random(8)), |
||
81 | ]); |
||
82 | $user->verification_token = str_random(32); |
||
83 | $user->save(); |
||
84 | $student = $user->student()->create(['student_number' => strtolower($row->student_id)]); |
||
85 | } |
||
86 | |||
87 | // Check if the given course id exists |
||
88 | $course = Course::whereCode($row->course_id)->first(); |
||
89 | if ($course === null) { |
||
90 | $exception = new InvalidFieldValueException(); |
||
91 | $exception->setField('Course ID', $row->course_id, "The course {$row->course_id} does not exist. (at line {$index})"); |
||
92 | throw $exception; |
||
93 | } |
||
94 | |||
95 | // Check if the enrollment exists |
||
96 | $enrollment = Enrollment::where([ |
||
97 | 'course_id' => $course->id, |
||
98 | 'student_id' => $student->id, |
||
99 | ])->first(); |
||
100 | |||
101 | if ($enrollment === null) { |
||
102 | $enrollment = $student->enroll($course); |
||
103 | } |
||
104 | |||
105 | // Check if the given shift tag exists in the associated course |
||
106 | if ($row->shift !== null) { |
||
107 | $shift = $course->getShiftByTag($row->shift); |
||
108 | |||
109 | if ($shift === null) { |
||
110 | $shift = Shift::make(['tag' => $row->shift]); |
||
111 | $course->addShift($shift); |
||
112 | } |
||
113 | |||
114 | $enrollment->shift()->associate($shift); |
||
115 | } else { |
||
116 | $enrollment->shift()->dissociate(); |
||
117 | } |
||
118 | |||
119 | // Add the shift to the enrollment |
||
120 | $enrollment->save(); |
||
121 | }); |
||
122 | }); |
||
123 | }); |
||
124 | |||
125 | flash('The enrollments file was successfully imported.')->success(); |
||
126 | } catch (InvalidImportFileException $e) { |
||
127 | flash($e->getMessage())->error(); |
||
128 | } |
||
129 | |||
130 | return redirect()->route('enrollments.import'); |
||
131 | } |
||
143 |