Passed
Push — task/question-descriptions-opt... ( ce2668 )
by Grant
17:51 queued 08:44
created

ReferencesController::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 16
ccs 6
cts 7
cp 0.8571
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0116
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   Incoming request object.
35
     * @param Applicant $applicant Incoming applicant object.
36
     *
37
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
38
     */
39 1
    public function edit(Request $request, Applicant $applicant)
40
    {
41 1
        $applicant->load([
42 1
            'references.projects',
43
            'skill_declarations.skill',
44
        ]);
45
46 1
        return view('applicant/profile_04_references', [
47 1
            'applicant' => $applicant,
48 1
            'profile' => Lang::get('applicant/profile_references'),
49
        ]);
50
    }
51
52
    /**
53
     * Update or create a reference with the supplied data.
54
     *
55
     * @param \Illuminate\Http\Request   $request   The incoming request object.
56
     * @param \App\Models\Reference|null $reference The reference to update. If null, a new one should be created.
57
     *
58
     * @return
0 ignored issues
show
Coding Style introduced by
Return type missing for @return tag in function comment
Loading history...
59
     */
60 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...
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
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
118
     * @param  \App\Models\Reference    $reference
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
119
     * @return \Illuminate\Http\Response
120
     */
121 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...
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
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...
132
                "message" => 'Reference deleted'
133
            ];
134
        }
135
136 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...
137
    }
138
}
139