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

Authorized::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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