RatingGuideQuestionController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
eloc 39
dl 0
loc 96
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 0 8 1
A store() 0 25 1
A destroy() 0 7 1
A update() 0 19 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\RatingGuideQuestion;
6
use App\Models\JobPoster;
7
use App\Models\Lookup\AssessmentType;
8
9
use Illuminate\Http\Request;
10
11
class RatingGuideQuestionController extends Controller
12
{
13
    /**
14
     * Store a newly created resource in storage.
15
     *
16
     * @param  \Illuminate\Http\Request $request Incoming request.
17
     * @throws \InvalidArgumentException For missing $question.
18
     * @return mixed
19
     */
20
    public function store(Request $request)
21
    {
22
        $this->authorize('create', RatingGuideQuestion::class);
23
24
        $job_poster_id = (int)$request->json('job_poster_id');
25
        $assessment_type_id = (int)$request->json('assessment_type_id');
26
        $question = $request->json('question');
27
28
        JobPoster::findOrFail($job_poster_id);
29
        AssessmentType::findOrFail($assessment_type_id);
30
31
        $ratingGuideQuestion = new RatingGuideQuestion([
32
            'job_poster_id' => $job_poster_id,
33
            'assessment_type_id' => $assessment_type_id,
34
            'question' => $question,
35
        ]);
36
        // Check that this user is allowed to create an Assessment for this criterion.
37
        $this->authorize('update', $ratingGuideQuestion);
38
39
        $ratingGuideQuestion->save();
40
        $ratingGuideQuestion->refresh();
41
42
        return [
43
            'success' => "Successfully created rating guide question $ratingGuideQuestion->id",
44
            'rating_guide_question' => $ratingGuideQuestion->toArray(),
45
        ];
46
    }
47
48
    /**
49
     * Display the specified resource.
50
     *
51
     * @param  \App\Models\RatingGuideQuestion $ratingGuideQuestion Incoming object.
52
     * @return mixed
53
     */
54
    public function show(RatingGuideQuestion $ratingGuideQuestion)
55
    {
56
        $this->authorize('view', $ratingGuideQuestion);
57
        $ratingGuideQuestion->load([
58
            'job_poster',
59
            'assessment_type'
60
        ]);
61
        return $ratingGuideQuestion->toArray();
62
    }
63
64
    /**
65
     * Update the specified resource in storage.
66
     *
67
     * @param  \Illuminate\Http\Request        $request             Incoming request.
68
     * @param  \App\Models\RatingGuideQuestion $ratingGuideQuestion Incoming object.
69
     * @throws \InvalidArgumentException For missing $question.
70
     * @return mixed
71
     */
72
    public function update(Request $request, RatingGuideQuestion $ratingGuideQuestion)
73
    {
74
        $this->authorize('update', $ratingGuideQuestion);
75
76
        $job_poster_id = (int)$request->json('job_poster_id');
77
        $assessment_type_id = (int)$request->json('assessment_type_id');
78
        $question = $request->json('question');
79
80
        JobPoster::findOrFail($job_poster_id);
81
        AssessmentType::findOrFail($assessment_type_id);
82
83
        $ratingGuideQuestion->job_poster_id = $job_poster_id;
84
        $ratingGuideQuestion->assessment_type_id = $assessment_type_id;
85
        $ratingGuideQuestion->question = $question;
86
        $ratingGuideQuestion->save();
87
88
        return [
89
            'success' => "Successfully updated rating guide question $ratingGuideQuestion->id",
90
            'rating_guide_question' => $ratingGuideQuestion->toArray(),
91
        ];
92
    }
93
94
    /**
95
     * Remove the specified resource from storage.
96
     *
97
     * @param  \App\Models\RatingGuideQuestion $ratingGuideQuestion Incoming object.
98
     * @return mixed
99
     */
100
    public function destroy(RatingGuideQuestion $ratingGuideQuestion)
101
    {
102
        $this->authorize('delete', $ratingGuideQuestion);
103
        $ratingGuideQuestion->delete();
104
105
        return [
106
            'success' => "Successfully deleted rating guide question $ratingGuideQuestion->id"
107
        ];
108
    }
109
}
110