Passed
Push — task/experience-skills-batch-r... ( a4eda0...c2c7c9 )
by Yonathan
05:54
created

BatchStoreExperienceSkill::authorize()   B

Complexity

Conditions 11
Paths 13

Size

Total Lines 37
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 37
rs 7.3166
cc 11
nc 13
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 BatchStoreExperienceSkill extends FormRequest
15
{
16
    /**
17
     * Determine if the user is authorized to make this request.
18
     *
19
     * @return bool
20
     */
21
    public function authorize()
22
    {
23
        $result = true;
24
        $experienceSkills = $this->all();
25
        foreach ($experienceSkills as $experienceSkill) {
26
            $experience_type = $experienceSkill['experience_type'];
27
            $experience_id = (int)$experienceSkill['experience_id'];
28
            $user = $this->user();
29
            $experience = null;
30
            switch ($experience_type) {
31
                case 'experience_work':
32
                    $experience = ExperienceWork::find($experience_id);
33
                    break;
34
                case 'experience_award':
35
                    $experience = ExperienceAward::find($experience_id);
36
                    break;
37
                case 'experience_community':
38
                    $experience = ExperienceCommunity::find($experience_id);
39
                    break;
40
                case 'experience_education':
41
                    $experience = ExperienceEducation::find($experience_id);
42
                    break;
43
                case 'experience_personal':
44
                    $experience = ExperiencePersonal::find($experience_id);
45
                    break;
46
            }
47
48
            if ($experience === null ||
49
                $user === null ||
50
                !$user->can('create', ExperienceSkill::class) ||
51
                !$user->can('update', $experience)
52
            ) {
53
                return false;
54
            }
55
        }
56
57
        return $result;
58
    }
59
60
    /**
61
     * Get the validation rules that apply to the request.
62
     *
63
     * @return array
64
     */
65
    public function rules()
66
    {
67
        return [
68
            '*.skill_id' => 'required|exists:skills,id',
69
            '*.justification' => 'nullable|string',
70
            '*.experience_type' => ['
71
                required',
72
                Rule::in([
73
                    'experience_work',
74
                    'experience_award',
75
                    'experience_community',
76
                    'experience_personal',
77
                    'experience_education'
78
                ])
79
            ],
80
            '*.experience_id' => 'required|integer'
81
        ];
82
    }
83
}
84