Completed
Push — master ( b11651...b97203 )
by
unknown
55:27 queued 54:03
created

Authorized::passes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.0416
1
<?php
2
3
namespace Spatie\ValidationRules\Rules;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Contracts\Validation\Rule;
7
8
class Authorized implements Rule
9
{
10
    /** @var string */
11
    protected $ability;
12
13
    /** @var array */
14
    protected $arguments;
15
16 24
    public function __construct(string $ability, string $className)
17
    {
18 24
        $this->ability = $ability;
19
20 24
        $this->className = $className;
0 ignored issues
show
Bug introduced by
The property className does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21 24
    }
22
23 24
    public function passes($attribute, $value)
24
    {
25 24
        if (! $user = Auth::user()) {
26 18
            return false;
27
        }
28
29 6
        if (! $model = $this->className::find($value)) {
30
            return false;
31
        }
32
33 6
        return $user->can($this->ability, $model);
34
    }
35
36
    public function message()
37
    {
38
        return __('validationRules.authorized');
39
    }
40
}
41