Passed
Push — task/applicant-skill-save-api ( 2ebd5d...fa7a9c )
by
unknown
06:31 queued 10s
created

BatchStoreExperienceSkill::authorize()   B

Complexity

Conditions 11
Paths 14

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 29
c 3
b 0
f 0
dl 0
loc 39
rs 7.3166
cc 11
nc 14
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
        $user = $this->user();
24
        if (!$user->can('create', ExperienceSkill::class)) {
25
            return false;
26
        }
27
28
        $experienceSkills = $this->all();
29
        foreach ($experienceSkills as $experienceSkill) {
30
            $experience_type = $experienceSkill['experience_type'];
31
            $experience_id = (int)$experienceSkill['experience_id'];
32
            $experience = null;
33
            switch ($experience_type) {
34
                case 'experience_work':
35
                    $experience = ExperienceWork::find($experience_id);
36
                    break;
37
                case 'experience_award':
38
                    $experience = ExperienceAward::find($experience_id);
39
                    break;
40
                case 'experience_community':
41
                    $experience = ExperienceCommunity::find($experience_id);
42
                    break;
43
                case 'experience_education':
44
                    $experience = ExperienceEducation::find($experience_id);
45
                    break;
46
                case 'experience_personal':
47
                    $experience = ExperiencePersonal::find($experience_id);
48
                    break;
49
            }
50
51
            if ($experience === null ||
52
                $user === null ||
53
                !$user->can('update', $experience)
54
            ) {
55
                return false;
56
            }
57
        }
58
59
        return true;
60
    }
61
62
    /**
63
     * Get the validation rules that apply to the request.
64
     *
65
     * @return array
66
     */
67
    public function rules()
68
    {
69
        return [
70
            '*.skill_id' => 'required|exists:skills,id',
71
            '*.justification' => 'nullable|string',
72
            '*.experience_type' => ['
73
                required',
74
                Rule::in([
75
                    'experience_work',
76
                    'experience_award',
77
                    'experience_community',
78
                    'experience_personal',
79
                    'experience_education'
80
                ])
81
            ],
82
            '*.experience_id' => 'required|integer'
83
        ];
84
    }
85
}
86