Passed
Push — task/comments-api ( a73e9b...cdddbd )
by Tristan
14:01 queued 11s
created

UniqueSkillDeclarationRule::passes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace App\Services\Validation\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Illuminate\Support\Facades\Lang;
7
use App\Models\Applicant;
8
use App\Models\SkillDeclaration;
9
10
class UniqueSkillDeclarationRule implements Rule
11
{
12
13
    protected $existing_declarations;
14
    protected $skill_declaration_id;
15
16
    /**
17
     * Create a new rule instance.
18
     *
19
     * @param  App\Models\Applicant  $user
0 ignored issues
show
Bug introduced by
The type App\Services\Validation\Rules\App\Models\Applicant was not found. Did you mean App\Models\Applicant? If so, make sure to prefix the type with \.
Loading history...
Coding Style introduced by
Doc comment for parameter $user does not match actual variable name $existing_declarations
Loading history...
20
     * @return void
21
     */
22
    public function __construct($existing_declarations, $skill_declaration_id = null)
0 ignored issues
show
introduced by
Method \App\Services\Validation\Rules\UniqueSkillDeclarationRule::__construct() does not have parameter type hint nor @param annotation for its parameter $existing_declarations.
Loading history...
introduced by
Method \App\Services\Validation\Rules\UniqueSkillDeclarationRule::__construct() does not have parameter type hint nor @param annotation for its parameter $skill_declaration_id.
Loading history...
23
    {
24
        $this->existing_declarations = $existing_declarations;
25
        $this->skill_declaration_id = $skill_declaration_id;
26
    }
27
28
    /**
29
     * This check passes if no there are no previous declarations for this skill,
30
     * or if they are updating a previously existing declaration
31
     * @param  [type] $attribute [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
32
     * @param  [type] $value     [description]
33
     * @return [type]            [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
34
     */
35
    public function passes($attribute, $value)
0 ignored issues
show
introduced by
Method \App\Services\Validation\Rules\UniqueSkillDeclarationRule::passes() does not have parameter type hint nor @param annotation for its parameter $attribute.
Loading history...
introduced by
Method \App\Services\Validation\Rules\UniqueSkillDeclarationRule::passes() does not have parameter type hint nor @param annotation for its parameter $value.
Loading history...
36
    {
37
        $prev_declarations_of_skill = $this->existing_declarations->where('skill_id', $value);
38
39
        return $prev_declarations_of_skill->isEmpty() ||
40
            ($this->skill_declaration_id != null
41
                && $prev_declarations_of_skill->pluck('id')->contains($this->skill_declaration_id));
42
    }
43
44
    public function message()
45
    {
46
        return Lang::get('validation.user_skill_unique');
47
    }
48
}
49