Passed
Push — dev ( 7438a7...58478a )
by Tristan
10:42 queued 05:17
created

StoreExperienceSkill::authorize()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 12
rs 9.9666
cc 4
nc 4
nop 0
1
<?php
2
3
namespace App\Http\Requests;
4
5
use App\Models\Experience;
6
use App\Models\ExperienceSkill;
7
use Illuminate\Foundation\Http\FormRequest;
8
use Illuminate\Validation\Rule;
9
10
class StoreExperienceSkill extends FormRequest
11
{
12
    /**
13
     * The user must have permission to update the Experience object
14
     *  this ExperienceSkill would be attached to.
15
     *
16
     * @return bool
17
     */
18
    public function authorize()
19
    {
20
        $experience_type = $this->input('experience_type');
21
        $experience_id = (int)$this->input('experience_id');
22
        $experience = new Experience();
23
        $experienceInstance = $experience->getExperienceInstance($experience_type, $experience_id);
24
        $user = $this->user();
25
26
        return $experienceInstance !== null
27
            && $user !== null
28
            && $user->can('create', ExperienceSkill::class)
29
            && $user->can('update', $experienceInstance);
30
    }
31
32
    /**
33
     * Get the validation rules that apply to the request.
34
     *
35
     * @return array
36
     */
37
    public function rules()
38
    {
39
        return [
40
            'skill_id' => 'required|exists:skills,id',
41
            'justification' => 'nullable|string',
42
            'experience_type' => ['
43
                required',
44
                Rule::in([
45
                    'experience_work',
46
                    'experience_award',
47
                    'experience_community',
48
                    'experience_personal',
49
                    'experience_education'
50
                ])
51
            ],
52
            'experience_id' => 'required|integer'
53
        ];
54
    }
55
}
56