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

RatingGuideQuestionController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 51
c 2
b 0
f 0
dl 0
loc 110
ccs 0
cts 48
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 30 2
A destroy() 0 7 1
A show() 0 8 1
A update() 0 28 3
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
        try {
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
        } catch (\Exception $e) {
42
            return response()->json([
43
                'error' => $e->getMessage()
44
            ], 400);
45
        }
46
47
        return [
48
            'success' => "Successfully created rating guide question $ratingGuideQuestion->id",
49
            'rating_guide_question' => $ratingGuideQuestion->toArray(),
50
        ];
51
    }
52
53
    /**
54
     * Display the specified resource.
55
     *
56
     * @param  \App\Models\RatingGuideQuestion $ratingGuideQuestion Incoming object.
57
     * @return mixed
58
     */
59
    public function show(RatingGuideQuestion $ratingGuideQuestion)
60
    {
61
        $this->authorize('view', $ratingGuideQuestion);
62
        $ratingGuideQuestion->load([
63
            'job_poster',
64
            'assessment_type'
65
        ]);
66
        return $ratingGuideQuestion->toArray();
67
    }
68
69
    /**
70
     * Update the specified resource in storage.
71
     *
72
     * @param  \Illuminate\Http\Request        $request             Incoming request.
73
     * @param  \App\Models\RatingGuideQuestion $ratingGuideQuestion Incoming object.
74
     * @throws \InvalidArgumentException For missing $question.
75
     * @return mixed
76
     */
77
    public function update(Request $request, RatingGuideQuestion $ratingGuideQuestion)
78
    {
79
        $this->authorize('update', $ratingGuideQuestion);
80
        try {
81
            $job_poster_id = (int)$request->json('job_poster_id');
82
            $assessment_type_id = (int)$request->json('assessment_type_id');
83
            $question = $request->json('question');
84
85
            JobPoster::findOrFail($job_poster_id);
86
            AssessmentType::findOrFail($assessment_type_id);
87
88
            if (empty($question)) {
89
                throw new \InvalidArgumentException('Question is required.');
90
            }
91
92
            $ratingGuideQuestion->job_poster_id = $job_poster_id;
93
            $ratingGuideQuestion->assessment_type_id = $assessment_type_id;
94
            $ratingGuideQuestion->question = $question;
95
            $ratingGuideQuestion->save();
96
        } catch (\Exception $e) {
97
            return response()->json([
98
                'error' => $e->getMessage()
99
            ], 400);
100
        }
101
102
        return [
103
            'success' => "Successfully updated rating guide question $ratingGuideQuestion->id",
104
            'rating_guide_question' => $ratingGuideQuestion->toArray(),
105
        ];
106
    }
107
108
    /**
109
     * Remove the specified resource from storage.
110
     *
111
     * @param  \App\Models\RatingGuideQuestion $ratingGuideQuestion Incoming object.
112
     * @return mixed
113
     */
114
    public function destroy(RatingGuideQuestion $ratingGuideQuestion)
115
    {
116
        $this->authorize('delete', $ratingGuideQuestion);
117
        $ratingGuideQuestion->delete();
118
119
        return [
120
            'success' => "Successfully deleted rating guide question $ratingGuideQuestion->id"
121
        ];
122
    }
123
}
124