Passed
Push — feature/micro-ref-email ( cb4bd3...6f3c5c )
by Tristan
05:11
created

SkillDeclarationValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Services\Validation;
4
5
use Illuminate\Support\Facades\Validator;
6
use App\Models\SkillDeclaration;
7
use App\Services\Validation\Rules\PolyExistsRule;
8
use App\Services\Validation\Rules\UniqueSkillDeclarationRule;
9
use App\Services\Validation\Rules\WordLimitRule;
10
use Illuminate\Validation\Validator as ValidationValidator;
11
12
class SkillDeclarationValidator
13
{
14
    /**
15
     * The maximum amount of words the skill description can reach.
16
     *
17
     * @var number
18
     */
19
    protected $description_max_words;
20
21
    /**
22
     * Skill declaration validator constructor.
23
     * @param number $description_max_words Max amount of words for skill description.
24
     * @return void
25
     */
26
    public function __construct($description_max_words = 100)
27
    {
28
        $this->description_max_words = $description_max_words;
29
    }
30
31
    /**
32
     * Skill declaration validator constructor.
33
     * @param SkillDeclaration $skillDeclaration The skill declaration input data.
34
     * @return ValidationValidator
35
    */
36
    public function validator(SkillDeclaration $skillDeclaration)
37
    {
38
        $uniqueSkillRule = new UniqueSkillDeclarationRule($skillDeclaration->skillable->skill_declarations, $skillDeclaration->id);
39
40
        // Validate basic data is filled in
41
        $validator = Validator::make($skillDeclaration->getAttributes(), [
42
            'skill_id' => [
43
                'required',
44
                'exists:skills,id',
45
                $uniqueSkillRule,
46
            ],
47
            'skillable_id' => [
48
                'required',
49
                new PolyExistsRule($skillDeclaration->skillable_type),
50
            ],
51
            'skillable_type' => 'required',
52
            'skill_status_id' => [
53
                'required',
54
                'exists:skill_statuses,id',
55
            ],
56
            'skill_level_id' => [
57
                'nullable',
58
                'exists:skill_levels,id',
59
            ],
60
            'description' => [
61
                'required',
62
                'string',
63
                new WordLimitRule($this->description_max_words),
64
            ],
65
        ]);
66
        return $validator;
67
    }
68
69
    public function validate(SkillDeclaration $skillDeclaration)
70
    {
71
        return $this->validator($skillDeclaration)->validate();
72
    }
73
}
74