Passed
Push — feature/experience-skills-api ( 61a84a )
by Tristan
07:02
created

StoreExperienceSkill   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 36
c 1
b 0
f 0
dl 0
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B authorize() 0 27 9
A rules() 0 16 1
1
<?php
2
3
namespace App\Http\Requests;
4
5
use App\Models\ExperienceAward;
6
use App\Models\ExperienceCommunity;
7
use App\Models\ExperienceEducation;
8
use App\Models\ExperiencePersonal;
9
use App\Models\ExperienceSkill;
10
use App\Models\ExperienceWork;
11
use Illuminate\Foundation\Http\FormRequest;
12
use Illuminate\Validation\Rule;
13
14
class StoreExperienceSkill extends FormRequest
15
{
16
    /**
17
     * The user must have permission to update the Experience object
18
     *  this ExperienceSkill would be attached to.
19
     *
20
     * @return bool
21
     */
22
    public function authorize()
23
    {
24
        $experience_type = $this->input('experience_type');
25
        $experience_id = (int)$this->input('experience_id');
26
        $user = $this->user();
27
        $experience = null;
28
        switch ($experience_type) {
29
            case 'experience_work':
30
                $experience = ExperienceWork::find($experience_id);
31
                break;
32
            case 'experience_award':
33
                $experience = ExperienceAward::find($experience_id);
34
                break;
35
            case 'experience_community':
36
                $experience = ExperienceCommunity::find($experience_id);
37
                break;
38
            case 'experience_education':
39
                $experience = ExperienceEducation::find($experience_id);
40
                break;
41
            case 'experience_personal':
42
                $experience = ExperiencePersonal::find($experience_id);
43
                break;
44
        }
45
        return $experience !== null
46
            && $user !== null
47
            && $user->can('create', ExperienceSkill::class)
48
            && $user->can('update', $experience);
49
    }
50
51
    /**
52
     * Get the validation rules that apply to the request.
53
     *
54
     * @return array
55
     */
56
    public function rules()
57
    {
58
        return [
59
            'skill_id' => 'required|exists:skills,id',
60
            'justification' => 'required|nullable|string',
61
            'experience_type' => ['
62
                required',
63
                Rule::in([
64
                    'experience_work',
65
                    'experience_award',
66
                    'experience_community',
67
                    'experience_personal',
68
                    'experience_education'
69
                ])
70
            ],
71
            'experience_id' => 'required|integer'
72
        ];
73
    }
74
}
75