Completed
Push — master ( 9bb6c5...e30e80 )
by Freek
15:07 queued 13:46
created

Authorized::passes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.0261
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
    /** @var string */
17
    protected $className;
18
19
    /** @var string */
20
    protected $attribute;
21
22 30
    public function __construct(string $ability, string $className)
23
    {
24 30
        $this->ability = $ability;
25
26 30
        $this->className = $className;
27 30
    }
28
29 30
    public function passes($attribute, $value): bool
30
    {
31 30
        $this->attribute = $attribute;
32
33 30
        if (! $user = Auth::user()) {
34 24
            return false;
35
        }
36
37 6
        if (! $model = $this->className::find($value)) {
0 ignored issues
show
Bug introduced by
The method find cannot be called on $this->className (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...
38
            return false;
39
        }
40
41 6
        return $user->can($this->ability, $model);
42
    }
43
44 6
    public function message(): string
45
    {
46 6
        $classBasename = class_basename($this->className);
47
48 6
        return __('validation.authorized', [
49 6
            'attribute' => $this->attribute,
50 6
            'ability' => $this->ability,
51 6
            'className' => $classBasename,
52
        ]);
53
    }
54
}
55