| Conditions | 18 |
| Paths | 12 |
| Total Lines | 119 |
| Code Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 342 |
| 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 | 2 | public function update(Request $request, ?Reference $reference = null) |
|
| 61 | { |
||
| 62 | 2 | $validator = new UpdateReferenceValidator(); |
|
| 63 | 2 | $validator->validate($request->input()); |
|
| 64 | |||
| 65 | 2 | if ($reference === null) { |
|
| 66 | 1 | $reference = new Reference(); |
|
| 67 | 1 | $reference->applicant_id = $request->user()->applicant->id; |
|
| 68 | } |
||
| 69 | 2 | $reference->fill([ |
|
| 70 | 2 | 'name' => $request->input('name'), |
|
| 71 | 2 | 'email' => $request->input('email'), |
|
| 72 | 2 | 'relationship_id' => $request->input('relationship_id'), |
|
| 73 | 2 | 'description' => $request->input('description'), |
|
| 74 | ]); |
||
| 75 | 2 | $reference->save(); |
|
| 76 | |||
| 77 | 2 | $reference->load('projects'); |
|
| 78 | |||
| 79 | //TODO: As soon as you can interact with projects outside of references, |
||
| 80 | // this will become a dangerous operation |
||
| 81 | 2 | $reference->projects()->delete(); |
|
| 82 | |||
| 83 | 2 | $newProjects = []; |
|
| 84 | 2 | if ($request->input('projects')) { |
|
| 85 | 2 | foreach ($request->input('projects') as $projectInput) { |
|
| 86 | 2 | $project = new Project(); |
|
| 87 | 2 | $project->applicant_id = $reference->applicant_id; |
|
| 88 | 2 | $project->fill([ |
|
| 89 | 2 | 'name' => $projectInput['name'], |
|
| 90 | 2 | 'start_date' => $projectInput['start_date'], |
|
| 91 | 2 | 'end_date' => $projectInput['end_date'], |
|
| 92 | ]); |
||
| 93 | 2 | $project->save(); |
|
| 94 | 2 | $newProjects[] = $project->id; |
|
| 95 | // $reference->projects()->attach($project); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | 2 | $reference->projects()->sync($newProjects); |
|
| 99 | |||
| 100 | //Attach relatives |
||
| 101 | 2 | $skillIds = $this->getRelativeIds($request->input(), 'skills'); |
|
| 102 | 2 | $reference->skill_declarations()->sync($skillIds); |
|
| 103 | |||
| 104 | // if an ajax request, return the new object |
||
| 105 | 2 | if ($request->ajax()) { |
|
| 106 | $reference->load('relationship'); |
||
| 107 | $reference->load('projects'); |
||
| 108 | return $reference->toJson(); |
||
| 109 | } else { |
||
| 110 | 2 | return redirect()->back(); |
|
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Delete the particular reference from storage. |
||
| 116 | * |
||
| 117 | * @param \Illuminate\Http\Request $request |
||
|
1 ignored issue
–
show
|
|||
| 118 | * @param \App\Models\Reference $reference |
||
|
1 ignored issue
–
show
|
|||
| 119 | * @return \Illuminate\Http\Response |
||
| 120 | */ |
||
| 121 | 1 | public function destroy(Request $request, Reference $reference) |
|
| 122 | { |
||
| 123 | 1 | $this->authorize('delete', $reference); |
|
| 124 | |||
| 125 | //TODO: when projects exist independently on profile, delete seperatley |
||
| 126 | 1 | $reference->projects()->delete(); |
|
| 127 | |||
| 128 | 1 | $reference->delete(); |
|
| 129 | |||
| 130 | 1 | if ($request->ajax()) { |
|
| 131 | return [ |
||
|
1 ignored issue
–
show
|
|||
| 132 | "message" => 'Reference deleted' |
||
| 133 | ]; |
||
| 134 | } |
||
| 135 | |||
| 136 | 1 | return redirect()->back(); |
|
|
1 ignored issue
–
show
|
|||
| 137 | } |
||
| 138 | } |
||
| 139 |