Completed
Push — master ( 082c11...100fd0 )
by Freek
14:50
created

ModelsExist   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 31
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A passes() 0 10 1
A message() 0 4 1
1
<?php
2
3
namespace Spatie\ValidationRules\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
7
class ModelsExist implements Rule
8
{
9
    /** @var string */
10
    protected $modelClassName;
11
12
    /** @var string */
13
    protected $attribute;
14
15
    public function __construct(string $modelClassName, string $attribute = 'id')
16
    {
17
        $this->modelClassName = $modelClassName;
18
19
        $this->attribute = $attribute;
20
    }
21
22
    public function passes($attribute, $value)
23
    {
24
        $value = array_filter($value);
25
26
        $modelIds = array_unique($value);
27
28
        $modelCount = $this->modelClassName::whereIn($this->attribute, $modelIds)->count();
0 ignored issues
show
Bug introduced by Alex Vanderbist
The method whereIn cannot be called on $this->modelClassName (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
29
30
        return count($modelIds) === $modelCount;
31
    }
32
33
    public function message()
34
    {
35
        return __('validationRules.model_ids');
36
    }
37
}
38