Passed
Push — dev ( 5fcb34...dfcc73 )
by Yonathan
05:50 queued 11s
created

SkillDeclarationValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 25
c 1
b 0
f 0
dl 0
loc 60
rs 10
ccs 22
cts 22
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validator() 0 31 1
A __construct() 0 3 1
A validate() 0 3 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 1
     * @param number $description_max_words Max amount of words for skill description.
24
     * @return void
25 1
     */
26 1
    public function __construct($description_max_words = 100)
27 1
    {
28 1
        $this->description_max_words = $description_max_words;
29
    }
30 1
31
    /**
32 1
     * Skill declaration validator constructor.
33 1
     * @param SkillDeclaration $skillDeclaration The skill declaration input data.
34
     * @return ValidationValidator
35
    */
36 1
    public function validator(SkillDeclaration $skillDeclaration)
37
    {
38 1
        $uniqueSkillRule = new UniqueSkillDeclarationRule($skillDeclaration->skillable->skill_declarations, $skillDeclaration->id);
39 1
40 1
        // Validate basic data is filled in
41
        $validator = Validator::make($skillDeclaration->getAttributes(), [
42
            'skill_id' => [
43 1
                'required',
44 1
                'exists:skills,id',
45
                $uniqueSkillRule,
46
            ],
47 1
            'skillable_id' => [
48 1
                'required',
49
                new PolyExistsRule($skillDeclaration->skillable_type),
50
            ],
51 1
            'skillable_type' => 'required',
52 1
            'skill_status_id' => [
53
                'required',
54 1
                'exists:skill_statuses,id',
55
            ],
56 1
            'skill_level_id' => [
57
                'nullable',
58
                'exists:skill_levels,id',
59 1
            ],
60
            'description' => [
61 1
                '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