Passed
Push — feature/job-builder/step-03-ui ( dabf2e...dabf2e )
by Xander
20:05 queued 13s
created

JobApiController::show()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

103
    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...
104
    {
105
        // TODO: complete.
106
    }
107
}
108