Passed
Push — feature/job-builder/xm-job-ste... ( 16a8d2...bd29e1 )
by Xander
13:15
created

JobApiController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\JobPoster;
7
use App\Models\Criteria;
8
use App\Services\Validation\JobPosterValidator;
9
use App\Http\Requests\UpdateJobPoster;
10
use App\Http\Requests\StoreJobPoster;
11
12
class JobApiController extends Controller
13
{
14
    /**
15
     * Class constructor
16
     */
17 8
    public function __construct()
18
    {
19
        // This applies the appropriate policy to each resource route.
20 8
        $this->authorizeResource(JobPoster::class, 'job');
21 8
    }
22
23
    /**
24
     * Convert a job poster to the array expected by API requests,
25
     * with all criteria,
26
     * and with translation arrays in both languages.
27
    *
28
     * @param  \App\Models\JobPoster $job Incoming Job Poster object.
29
     * @return mixed[]
30
     */
31 4
    private function jobToArray(JobPoster $job)
32
    {
33 4
        $criteria = Criteria::where('job_poster_id', $job->id)->get();
34 4
        $criteriaTranslated = [];
35 4
        foreach ($criteria as $criterion) {
36 3
            $criteriaTranslated[] = array_merge($criterion->toArray(), $criterion->getTranslationsArray());
37
        }
38 4
        $jobArray = array_merge($job->toApiArray(), ['criteria' => $criteriaTranslated]);
39 4
        return $jobArray;
40
    }
41
    /**
42
     * Display a listing of the resource.
43
     *
44
     * @return \Illuminate\Http\Response
1 ignored issue
show
Coding Style introduced by
Function return type is not void, but function has no return statement
Loading history...
45
     */
46
    public function index()
47
    {
48
        // TODO: complete.
49
    }
50
51
    /**
52
     * Store a newly created resource in storage.
53
     *
54
     * @param  \App\Http\Requests\StoreJobPoster $request Incoming request.
55
     * @return \Illuminate\Http\Response
56
     */
57 1
    public function store(StoreJobPoster $request)
58
    {
59 1
        $data = $request->validated();
60 1
        $job = new JobPoster();
61 1
        $job->manager_id = $request->user()->manager->id;
62 1
        $job->fill($data);
63 1
        $job->save();
64 1
        return response()->json($this->jobToArray($job));
65
    }
66
67
    /**
68
     * Display the specified resource.
69
     *
70
     * @param  \App\Models\JobPoster $job Incoming Job Poster.
71
     * @return \Illuminate\Http\Response
72
     */
73 1
    public function show(JobPoster $job)
74
    {
75 1
        return response()->json($this->jobToArray($job));
76
    }
77
78
    /**
79
     * Update the specified resource in storage.
80
     *
81
     * @param  \App\Http\Requests\UpdateJobPoster $request Validates input.
82
     * @param  \App\Models\JobPoster              $job     Incoming Job Poster.
83
     * @return \Illuminate\Http\Response
84
     */
85 2
    public function update(UpdateJobPoster $request, JobPoster $job)
86
    {
87 2
        $data = $request->validated();
88
        // Only values both in the JobPoster->fillable array,
89
        // and returned by UpdateJobPoster->validatedData(), will be set.
90 2
        $job->fill($data);
91 2
        $job->save();
92 2
        return response()->json($this->jobToArray($job->fresh()));
93
    }
94
95
    /**
96
     * Remove the specified resource from storage.
97
     *
98
     * @param  integer $id Job Poster ID.
99
     * @return \Illuminate\Http\Response
1 ignored issue
show
Coding Style introduced by
Function return type is not void, but function has no return statement
Loading history...
100
     */
101
    public function destroy($id)
1 ignored issue
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

101
    public function destroy(/** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Type hint "int" missing for $id
Loading history...
102
    {
103
        // TODO: complete.
104
    }
105
}
106