Passed
Push — task/talent-dot-test ( 0ef1e8...074f28 )
by Grant
08:57 queued 03:14
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
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
34
     * @param  \App\Models\Applicant $applicant
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
35
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
36
     */
37
    public function edit(Request $request, Applicant $applicant)
38
    {
39
        return view('applicant/profile_05_portfolio', [
40
            'applicant' => $applicant,
41
            'profile' => Lang::get('applicant/profile_work_samples'),
42
        ]);
43
    }
44
45
    /**
46
     * Update the workSample in storage, or create new one.
47
     *
48
     * @param  \Illuminate\Http\Request    $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
49
     * @param  \App\Models\WorkSample|null $workSample
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
50
     * @return \Illuminate\Http\Response
51
     */
52
    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...
53
    {
54
        $validator = new UpdateWorkSampleValidator();
55
        $validator->validate($request->input());
56
57
        if ($workSample === null) {
58
            $workSample = new WorkSample();
59
            $workSample->applicant_id = $request->user()->applicant->id;
60
        }
61
        $workSample->fill([
62
            'name' => $request->input('name'),
63
            'file_type_id' => $request->input('file_type_id'),
64
            'url' => $request->input('url'),
65
            'description' => $request->input('description'),
66
        ]);
67
        $workSample->save();
68
69
        //Attach relatives
70
        $skillIds = $this->getRelativeIds($request->input(), 'skills');
71
        $workSample->skill_declarations()->sync($skillIds);
72
73
        // if an ajax request, return the new object
74
        if ($request->ajax()) {
75
            $workSample->load('file_type');
76
            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...
77
        } else {
78
            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...
79
        }
80
    }
81
82
    /**
83
     * Delete the particular work sample from storage.
84
     *
85
     * @param  \Illuminate\Http\Request $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
86
     * @param  \App\Models\WorkSample   $workSample
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
87
     * @return \Illuminate\Http\Response
88
     */
89
    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...
90
    {
91
        $this->authorize('delete', $workSample);
92
93
        $workSample->delete();
94
95
        if ($request->ajax()) {
96
            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...
97
                "message" => 'Work sample deleted'
98
            ];
99
        }
100
101
        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...
102
    }
103
}
104