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