Passed
Push — master ( 14187f...0a5e59 )
by Pavel
02:45
created

RestRequest::passesAuthorization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php namespace Pz\LaravelDoctrine\Rest;
2
3
use Illuminate\Auth\Access\AuthorizationException;
4
use Illuminate\Contracts\Auth\Access\Gate;
5
use Illuminate\Contracts\Validation\Validator;
6
use Illuminate\Foundation\Http\FormRequest;
7
8
use Illuminate\Validation\ValidationException;
9
use Pz\Doctrine\Rest\RestRequestInterface;
10
11
abstract class RestRequest extends FormRequest implements RestRequestInterface
12
{
13
    /**
14
     * Authorization gateway ability.
15
     * Return `false` for pass auth.
16
     *
17
     * @return null|string
18
     */
19
    abstract public function ability();
20
21
    /**
22
     * @return $this
23
     */
24 5
    public function http()
25
    {
26 5
        return $this;
27
    }
28
29
    /**
30
     * @return array
31
     */
32 4
    public function rules()
33
    {
34 4
        return [];
35
    }
36
37
    /**
38
     * @param object|string $entity
39
     * @throws AuthorizationException
40
     * @return void
41
     */
42 5
    public function authorize($entity)
43
    {
44 5
        $this->gate()->authorize($this->ability(), $entity);
45 5
    }
46
47
    /**
48
     * @param Validator $validator
49
     *
50
     * @throws ValidationException
51
     */
52 2
    protected function failedValidation(Validator $validator)
53
    {
54 2
        throw (new ValidationException($validator))->errorBag($this->errorBag);
55
    }
56
57
    /**
58
     * Laravel authorization gate.
59
     *
60
     * @return Gate
61
     */
62 5
    public function gate()
63
    {
64 5
        return $this->container->make(Gate::class);
65
    }
66
67
    /**
68
     * Pass default request authorization.
69
     *
70
     * @return bool
71
     */
72 5
    protected function passesAuthorization()
73
    {
74 5
        return true;
75
    }
76
}
77