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