Completed
Push — master ( 99f6c2...5bcbf0 )
by Pavel
05:52
created

RestRequest::isJsonApi()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 9.4285
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 array
23
     */
24
    public function getFields()
25
    {
26
        return $this->get('fields', []);
27
    }
28
29
    /**
30
     * @return bool
31
     */
32
    public function isJsonApi()
33
    {
34
        if (in_array(RestRequestInterface::JSON_API_CONTENT_TYPE, $this->getAcceptableContentTypes())) {
0 ignored issues
show
Bug introduced by
The constant Pz\Doctrine\Rest\RestReq...::JSON_API_CONTENT_TYPE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
35
            return true;
36
        }
37
38
        return $this->getContentType() === static::JSON_API_CONTENT_TYPE;
0 ignored issues
show
Bug introduced by
The constant Pz\LaravelDoctrine\Rest\...::JSON_API_CONTENT_TYPE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
    }
40
41
    /**
42
     * @return $this
43
     */
44
    public function http()
45
    {
46
        return $this;
47
    }
48
49
    /**
50
     * @return array
51
     */
52 2
    public function rules()
53
    {
54
        return [
55 2
            'fields' => 'array'
56
        ];
57
    }
58
59
    /**
60
     * @param object|string $entity
61
     * @throws AuthorizationException
62
     * @return void
63
     */
64 4
    public function authorize($entity)
65
    {
66 4
        $this->gate()->authorize($this->ability(), $entity);
67
    }
68
69
    /**
70
     * @param Validator $validator
71
     *
72
     * @throws ValidationException
73
     */
74 1
    protected function failedValidation(Validator $validator)
75
    {
76 1
        throw (new ValidationException($validator))->errorBag($this->errorBag);
77
    }
78
79
    /**
80
     * Laravel authorization gate.
81
     *
82
     * @return Gate
83
     */
84 4
    public function gate()
85
    {
86 4
        return $this->container->make(Gate::class);
87
    }
88
89
    /**
90
     * Pass default request authorization.
91
     *
92
     * @return bool
93
     */
94 4
    protected function passesAuthorization()
95
    {
96 4
        return true;
97
    }
98
}
99