Passed
Push — master ( 3b518a...cd2a02 )
by Adam
11:23
created

CommentRequest::authorize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Http\Requests;
4
5
use Coyote\Comment;
6
use Coyote\Job;
7
use Coyote\Guide;
8
use Illuminate\Foundation\Http\FormRequest;
9
use Illuminate\Validation\Rule;
10
11
class CommentRequest extends FormRequest
12
{
13
    /**
14
     * Determine if the user is authorized to make this request.
15
     *
16
     * @return bool
17
     */
18
    public function authorize()
19
    {
20
        return true;
21
    }
22
23
    /**
24
     * Get the validation rules that apply to the request.
25
     *
26
     * @return array
27
     */
28
    public function rules()
29
    {
30
        /** @var Comment $comment */
31
        $comment = $this->route('comment');
32
        $rule = Rule::requiredIf(!$comment->exists && !$this->input('parent_id'));
33
34
        return [
35
            'text' => 'required|string',
36
            'parent_id' => [
37
                'nullable',
38
                'int',
39
                Rule::exists('comments', 'id')->whereNull('parent_id')
40
            ],
41
            'resource_id' => [
42
                'bail',
43
                $rule,
44
                'nullable',
45
                'int'
46
            ],
47
            'resource_type' => [
48
                'bail',
49
                $rule,
50
                Rule::in([Guide::class, Job::class])
51
            ]
52
        ];
53
    }
54
}
55