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