Passed
Push — task/common-translation-packag... ( 2abece...256e02 )
by Chris
07:03
created

JobController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 29
dl 0
loc 109
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 12 1
A submitForReview() 0 19 3
A __construct() 0 4 1
A store() 0 11 1
A destroy() 0 2 1
A index() 0 2 1
A show() 0 4 1
1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\StoreJobPoster;
7
use App\Http\Requests\UpdateJobPoster;
8
use App\Mail\JobPosterReviewRequested;
9
use App\Models\JobPoster;
10
use App\Models\Lookup\JobTerm;
11
use Illuminate\Http\Resources\Json\JsonResource;
12
use Illuminate\Support\Facades\Auth;
13
use Illuminate\Support\Facades\Log;
14
use Illuminate\Support\Facades\Mail;
15
use Jenssegers\Date\Date;
16
17
class JobController extends Controller
18
{
19
    /**
20
     * Class constructor
21
     */
22
    public function __construct()
23
    {
24
        // This applies the appropriate policy to each resource route.
25
        $this->authorizeResource(JobPoster::class, 'job');
26
    }
27
28
    /**
29
     * Display a listing of the resource.
30
     *
31
     * @return void
32
     */
33
    public function index()
34
    {
35
        // TODO: complete.
36
    }
37
38
    /**
39
     * Store a newly created resource in storage.
40
     *
41
     * @param  \App\Http\Requests\StoreJobPoster $request Incoming request.
42
     * @return \Illuminate\Http\Response
43
     */
44
    public function store(StoreJobPoster $request)
45
    {
46
        $data = $request->validated();
47
        $job = new JobPoster();
48
        $job->manager_id = $request->user()->manager->id;
49
        // Defaulting JPB created Jobs to monthly terms for now.
50
        $job->job_term_id = JobTerm::where('name', 'month')->value('id');
51
        $job->fill($data);
52
        $job->save();
53
        $job->load('criteria');
54
        return new JsonResource($job);
55
    }
56
57
    /**
58
     * Display the specified resource.
59
     *
60
     * @param  \App\Models\JobPoster $job Incoming Job Poster.
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function show(JobPoster $job)
64
    {
65
        $job->load('criteria');
66
        return new JsonResource($job);
67
    }
68
69
    /**
70
     * Update the specified resource in storage.
71
     *
72
     * @param  \App\Http\Requests\UpdateJobPoster $request Validates input.
73
     * @param  \App\Models\JobPoster              $job     Incoming Job Poster.
74
     * @return \Illuminate\Http\Response
75
     */
76
    public function update(UpdateJobPoster $request, JobPoster $job)
77
    {
78
        $data = $request->validated();
79
        // Only values both in the JobPoster->fillable array,
80
        // and returned by UpdateJobPoster->validatedData(), will be set.
81
        $job->fill($data);
82
        // Defaulting JPB updated jobs to monthly for now.
83
        $job->job_term_id = JobTerm::where('name', 'month')->value('id');
84
        $job->save();
85
        $job->fresh();
86
        $job->load('criteria');
87
        return new JsonResource($job);
88
    }
89
90
    /**
91
     * Remove the specified resource from storage.
92
     *
93
     * @param  integer $id Job Poster ID.
94
     * @return void
95
     */
96
    public function destroy(int $id)
0 ignored issues
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

96
    public function destroy(/** @scrutinizer ignore-unused */ int $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...
97
    {
98
        // TODO: complete.
99
    }
100
101
    /**
102
     * Submit the Job Poster for review.
103
     *
104
     * @param  \App\Models\JobPoster $job Job Poster object.
105
     * @return \Illuminate\Http\Response
106
     */
107
    public function submitForReview(JobPoster $job)
108
    {
109
        // Check to avoid submitting for review multiple times.
110
        if ($job->review_requested_at === null) {
111
            // Update review request timestamp.
112
            $job->review_requested_at = new Date();
113
            $job->save();
114
115
            // Send email.
116
            $reviewer_email = config('mail.reviewer_email');
117
            if (isset($reviewer_email)) {
118
                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

118
                Mail::to($reviewer_email)->send(new JobPosterReviewRequested($job, /** @scrutinizer ignore-type */ Auth::user()));
Loading history...
119
            } else {
120
                Log::error('The reviewer email environment variable is not set.');
121
            }
122
        }
123
        $job->load('criteria');
124
125
        return new JsonResource($job);
126
    }
127
}
128