Passed
Push — task/manager-applicant-review-... ( a1a27a...d21381 )
by Xander
15:21 queued 09:20
created

ReferencesController::update()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 51
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 5.0187

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 51
ccs 30
cts 33
cp 0.9091
rs 9.0808
c 0
b 0
f 0
cc 5
nc 8
nop 2
crap 5.0187

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Lang;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Http\Request;
8
use App\Http\Controllers\Controller;
9
use App\Models\Skill;
10
use App\Models\Applicant;
11
use App\Models\Reference;
12
use App\Models\Project;
13
use App\Models\Lookup\Relationship;
14
use App\Services\Validation\Requests\UpdateReferenceValidator;
15
16
class ReferencesController extends Controller
17
{
18
19
    /**
20
     * Show the form for editing the logged-in applicant's references
21
     *
22
     * @param  Request $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
23
     * @return \Illuminate\Http\RedirectResponse
24
     */
25 1
    public function editAuthenticated(Request $request): \Illuminate\Http\RedirectResponse
26
    {
27 1
        $applicant = $request->user()->applicant;
28 1
        return redirect(route('profile.references.edit', $applicant));
29
    }
30
31
    /**
32
     * Show the form for editing the applicant's references
33
     *
34
     * @param  Request               $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
35
     * @param  \App\Models\Applicant $applicant
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
36
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
37
     */
38 1
    public function edit(Request $request, Applicant $applicant)
39
    {
40 1
        return view('applicant/profile_04_references', [
41 1
            'applicant' => $applicant,
42 1
            'profile' => Lang::get('applicant/profile_references'),
43 1
            'reference_template' => Lang::get('common/references'),
44 1
            'relationships' => Relationship::all(),
45
        ]);
46
    }
47
48
    /**
49
     * Update or create a reference with the supplied data.
50
     *
51
     * @param \Illuminate\Http\Request   $request   The incoming request object.
52
     * @param \App\Models\Reference|null $reference The reference to update. If null, a new one should be created.
53
     *
54
     * @return
0 ignored issues
show
Coding Style introduced by
Return type missing for @return tag in function comment
Loading history...
55
     */
56 2
    public function update(Request $request, ?Reference $reference = null)
0 ignored issues
show
introduced by
Method \App\Http\Controllers\ReferencesController::update() does not have return type hint nor @return annotation for its return value.
Loading history...
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
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
114
     * @param  \App\Models\Reference    $reference
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
115
     * @return \Illuminate\Http\Response
116
     */
117 1
    public function destroy(Request $request, Reference $reference)
0 ignored issues
show
introduced by
Method \App\Http\Controllers\ReferencesController::destroy() does not have return type hint for its return value but it should be possible to add it based on @return annotation "\Illuminate\Http\Response".
Loading history...
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
Bug Best Practice introduced by
The expression return array('message' => 'Reference deleted') returns the type array<string,string> which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
128
                "message" => 'Reference deleted'
129
            ];
130
        }
131
132 1
        return redirect()->back();
1 ignored issue
show
Bug Best Practice introduced by
The expression return redirect()->back() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
133
    }
134
}
135