Completed
Push — feature/policy_unit_tests ( 0404ac...6499e2 )
by
unknown
07:31
created

SkillDeclarationValidator::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 26
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace App\Services\Validation;
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
use Illuminate\Support\Facades\Validator;
4
use Illuminate\Validation\Rule;
5
use App\Models\Skill;
6
use App\Models\SkillDeclaration;
7
use App\Models\Lookup\SkillLevel;
8
use App\Models\Lookup\SkillStatus;
9
use App\Models\Applicant;
10
use App\Services\Validation\Rules\UniqueApplicantSkillRule;
11
class SkillDeclarationValidator
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SkillDeclarationValidator
Loading history...
12
{
13
    protected $applicant;
14
    protected $skill_ids;
15
    protected $skill_status_ids;
16
    protected $skill_level_ids;
17
    public function __construct(Applicant $applicant)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
18
    {
19
        $this->applicant = $applicant;
20
        $this->skill_ids = Skill::all()->pluck('id');
21
        $this->skill_status_ids = SkillStatus::all()->pluck('id');
22
        $this->skill_level_ids = SkillLevel::all()->pluck('id');
23
    }
24
    public function validate(SkillDeclaration $skillDeclaration)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function validate()
Loading history...
25
    {
26
        $uniqueSkillRule = new UniqueApplicantSkillRule($this->applicant, $skillDeclaration->id);
27
        //This array is reset every time because applicants table can change frequently
28
        $applicant_ids = Applicant::all()->pluck('id');
29
        //Validate basic data is filled in
30
        Validator::make($skillDeclaration->getAttributes(), [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
31
            'skill_id' => [
32
                'required',
33
                Rule::in($this->skill_ids->toArray()),
34
                $uniqueSkillRule,
35
            ],
36
            'applicant_id' => [
37
                'required',
38
                Rule::in($applicant_ids->toArray()),
39
            ],
40
            'skill_status_id' => [
41
                'required',
42
                Rule::in($this->skill_status_ids->toArray()),
43
            ],
44
            'skill_level_id' => [
45
                'required',
46
                Rule::in($this->skill_level_ids->toArray()),
47
            ],
48
            'description' => 'string|required',
49
        ])->validate();
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
50
    }
51
}