Passed
Push — dev ( c369d7...065231 )
by Chris
14:17 queued 05:41
created

JobApiController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 9
eloc 27
dl 0
loc 117
ccs 24
cts 28
cp 0.8571
rs 10
c 0
b 0
f 0

8 Methods

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

106
    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...
107
    {
108
        // TODO: complete.
109
    }
110
111
    /**
112
     * Submit the Job Poster for review.
113
     *
114
     * @param  \App\Models\JobPoster    $job Job Poster object.
1 ignored issue
show
Coding Style introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
115
     * @return \Illuminate\Http\Response
116
     */
117
    public function submitForReview(JobPoster $job)
118
    {
119
        // Update review request timestamp.
120
        $job->review_requested_at = new Date();
121
        $job->save();
122
123
        // Send email.
124
        $reviewer_email = config('mail.reviewer_email');
125
        if (isset($reviewer_email)) {
126
            Mail::to($reviewer_email)->send(new JobPosterReviewRequested($job, Auth::user()));
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Facades\Auth::user() can also be of type null; however, parameter $manager of App\Mail\JobPosterReviewRequested::__construct() does only seem to accept App\Models\User, maybe add an additional type check? ( Ignorable by Annotation )

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

126
            Mail::to($reviewer_email)->send(new JobPosterReviewRequested($job, /** @scrutinizer ignore-type */ Auth::user()));
Loading history...
127
        } else {
128
            Log::error('The reviewer email environment variable is not set.');
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Api\Log was not found. Did you mean Log? If so, make sure to prefix the type with \.
Loading history...
129
        }
130
131
        return response()->json($this->jobToArray($job));
132
    }
133
}
134