Passed
Push — task/applicant-profile-perform... ( 267d12...585f2b )
by Chris
13:18 queued 07:42
created

WorkSamplesController::editAuthenticated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\WorkSample;
12
use App\Services\Validation\Requests\UpdateWorkSampleValidator;
13
14
class WorkSamplesController extends Controller
15
{
16
17
    /**
18
     * Show the form for editing the logged-in applicant's Work Samples
19
     *
20
     * @param  Request $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
21
     * @return \Illuminate\Http\RedirectResponse
22
     */
23
    public function editAuthenticated(Request $request): \Illuminate\Http\RedirectResponse
24
    {
25
        $applicant = $request->user()->applicant;
26
        return redirect(route('profile.work_samples.edit', $applicant));
27
    }
28
29
30
    /**
31
     * Show the form for editing the applicant's work samples
32
     *
33
     * @param Request   $request   Incoming request object.
34
     * @param Applicant $applicant Incoming Applicant object.
35
     *
36
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
37
     */
38
    public function edit(Request $request, Applicant $applicant)
39
    {
40
        $applicant->load([
41
            'work_samples',
42
            'skill_declarations.skill',
43
        ]);
44
45
        return view('applicant/profile_05_portfolio', [
46
            'applicant' => $applicant,
47
            'profile' => Lang::get('applicant/profile_work_samples'),
48
        ]);
49
    }
50
51
    /**
52
     * Update the workSample in storage, or create new one.
53
     *
54
     * @param  \Illuminate\Http\Request    $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
55
     * @param  \App\Models\WorkSample|null $workSample
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
56
     * @return \Illuminate\Http\Response
57
     */
58
    public function update(Request $request, ?WorkSample $workSample = null)
0 ignored issues
show
introduced by
Method \App\Http\Controllers\WorkSamplesController::update() 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...
59
    {
60
        $validator = new UpdateWorkSampleValidator();
61
        $validator->validate($request->input());
62
63
        if ($workSample === null) {
64
            $workSample = new WorkSample();
65
            $workSample->applicant_id = $request->user()->applicant->id;
66
        }
67
        $workSample->fill([
68
            'name' => $request->input('name'),
69
            'file_type_id' => $request->input('file_type_id'),
70
            'url' => $request->input('url'),
71
            'description' => $request->input('description'),
72
        ]);
73
        $workSample->save();
74
75
        //Attach relatives
76
        $skillIds = $this->getRelativeIds($request->input(), 'skills');
77
        $workSample->skill_declarations()->sync($skillIds);
78
79
        // if an ajax request, return the new object
80
        if ($request->ajax()) {
81
            $workSample->load('file_type');
82
            return $workSample->toJson();
1 ignored issue
show
Bug Best Practice introduced by
The expression return $workSample->toJson() returns the type string which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
83
        } else {
84
            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...
85
        }
86
    }
87
88
    /**
89
     * Delete the particular work sample from storage.
90
     *
91
     * @param  \Illuminate\Http\Request $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
92
     * @param  \App\Models\WorkSample   $workSample
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
93
     * @return \Illuminate\Http\Response
94
     */
95
    public function destroy(Request $request, WorkSample $workSample)
0 ignored issues
show
introduced by
Method \App\Http\Controllers\WorkSamplesController::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...
96
    {
97
        $this->authorize('delete', $workSample);
98
99
        $workSample->delete();
100
101
        if ($request->ajax()) {
102
            return [
1 ignored issue
show
Bug Best Practice introduced by
The expression return array('message' => 'Work sample deleted') returns the type array<string,string> which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
103
                "message" => 'Work sample deleted'
104
            ];
105
        }
106
107
        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...
108
    }
109
}
110