Passed
Push — feature/screening-plan-redux ( 8ed68a...044f96 )
by Chris
08:45
created

RatingGuideAnswerController::update()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 25
ccs 0
cts 17
cp 0
rs 9.6666
c 0
b 0
f 0
cc 3
nc 8
nop 2
crap 12
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\RatingGuideAnswer;
6
use App\Models\RatingGuideQuestion;
7
use App\Models\Skill;
8
9
use Illuminate\Http\Request;
10
use Illuminate\Http\Response;
11
12
class RatingGuideAnswerController extends Controller
13
{
14
    /**
15
     * Store a newly created resource in storage.
16
     *
17
     * @param  \Illuminate\Http\Request $request Incoming request.
18
     * @throws \InvalidArgumentException For missing $expected_answer.
19
     * @return mixed
20
     */
21
    public function store(Request $request)
22
    {
23
        dump($request->json());
0 ignored issues
show
Bug introduced by
The call to dump() has too few arguments starting with valid. ( Ignorable by Annotation )

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

23
        /** @scrutinizer ignore-call */ 
24
        dump($request->json());

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
24
        try {
25
            $rating_guide_question_id = (int)$request->json('rating_guide_question_id');
26
            $skill_id = (int)$request->json('skill_id');
27
            $expected_answer = $request->json('expected_answer');
28
29
            RatingGuideQuestion::findOrFail($rating_guide_question_id);
30
            Skill::findOrFail($skill_id);
31
32
            if (empty($expected_answer)) {
33
                throw new \InvalidArgumentException('Expected answer is required.');
34
            }
35
36
            $ratingGuideAnswer = new RatingGuideAnswer([
37
                'rating_guide_question_id' => $rating_guide_question_id,
38
                'skill_id' => $skill_id,
39
                'expected_answer' => $expected_answer,
40
            ]);
41
            $ratingGuideAnswer->save();
42
            $ratingGuideAnswer->refresh();
43
        } catch (\Exception $e) {
44
            return response()->json([
45
                'error' => $e->getMessage()
46
            ], 400);
47
        }
48
49
        return [
50
            'success' => "Successfully created rating guide answer $ratingGuideAnswer->id"
51
        ];
52
    }
53
54
    /**
55
     * Display the specified resource.
56
     *
57
     * @param  \App\Models\RatingGuideAnswer $ratingGuideAnswer Incoming object.
58
     * @return mixed
59
     */
60
    public function show(RatingGuideAnswer $ratingGuideAnswer)
61
    {
62
        $ratingGuideAnswer->load([
63
            'skill',
64
            'rating_guide_question'
65
        ]);
66
        return $ratingGuideAnswer->toArray();
67
    }
68
69
    /**
70
     * Update the specified resource in storage.
71
     *
72
     * @param  \Illuminate\Http\Request      $request           Incoming request.
73
     * @param  \App\Models\RatingGuideAnswer $ratingGuideAnswer Incoming object.
74
     * @throws \InvalidArgumentException For empty $expected_answer.
75
     * @return mixed
76
     */
77
    public function update(Request $request, RatingGuideAnswer $ratingGuideAnswer)
78
    {
79
        try {
80
            $rating_guide_question_id = (int)$request->json('rating_guide_question_id');
81
            $skill_id = (int)$request->json('skill_id');
82
            $expected_answer = $request->json('expected_answer');
83
84
            RatingGuideQuestion::findOrFail($rating_guide_question_id);
85
            Skill::findOrFail($skill_id);
86
87
            if (empty($expected_answer)) {
88
                throw new \InvalidArgumentException('Expected answer is required.');
89
            }
90
            $ratingGuideAnswer->rating_guide_question_id = $rating_guide_question_id;
91
            $ratingGuideAnswer->skill_id = $skill_id;
92
            $ratingGuideAnswer->expected_answer = $expected_answer;
93
            $ratingGuideAnswer->save();
94
        } catch (\Exception $e) {
95
            return response()->json([
96
                'error' => $e->getMessage()
97
            ], 400);
98
        }
99
100
        return [
101
            'success' => "Successfully updated rating guide answer $ratingGuideAnswer->id"
102
        ];
103
    }
104
105
    /**
106
     * Remove the specified resource from storage.
107
     *
108
     * @param  \App\Models\RatingGuideAnswer $ratingGuideAnswer Incoming object.
109
     * @return mixed
110
     */
111
    public function destroy(RatingGuideAnswer $ratingGuideAnswer)
112
    {
113
        $ratingGuideAnswer->delete();
114
115
        return [
116
            'success' => "Successfully deleted rating guide answer $ratingGuideAnswer->id"
117
        ];
118
    }
119
}
120