Completed
Push — master ( 53c389...74b379 )
by Pavel
04:47
created

RestRequest::getExclude()   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\Validation\ValidationException;
4
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
5
use Pz\Doctrine\Rest\Exceptions\RestException;
6
7
use Pz\Doctrine\Rest\RestResponse;
8
use Symfony\Component\HttpFoundation\Response;
9
use Illuminate\Foundation\Http\FormRequest;
10
use Illuminate\Contracts\Validation\Validator;
11
12
class RestRequest extends FormRequest implements RestRequestContract
13
{
14
    /**
15
     * @var bool
16
     */
17
    protected $isRelationships = false;
18
19
    /**
20
     * @return array
21
     */
22 8
    public function rules()
23
    {
24
        return [
25 8
            'filter'        => 'sometimes|required',
26
            'include'       => 'sometimes|required|array',
27
            'exclude'       => 'sometimes|required|array',
28
            'fields'        => 'sometimes|required|array',
29
            'sort'          => 'sometimes|required|string',
30
            'page'          => 'sometimes|required|array',
31
            'page.number'   => 'sometimes|required|numeric',
32
            'page.size'     => 'sometimes|required|numeric',
33
            'page.limit'    => 'sometimes|required|numeric',
34
            'page.offset'   => 'sometimes|required|numeric',
35
        ];
36
    }
37
38
    /**
39
     * @param null|bool $value
40
     *
41
     * @return bool|null
42
     */
43 7
    public function isRelationships($value = null)
44
    {
45 7
        if ($value !== null) {
46 1
            $this->isRelationships = $value;
47
        }
48
49 7
        return $this->isRelationships;
50
    }
51
52
    /**
53
     * @return array
54
     * @throws RestException
55
     */
56 4
    public function getData()
57
    {
58 4
        if ((null === $data = $this->get('data')) || !is_array($data)) {
59 1
            throw RestException::missingRootData();
60
        }
61
62 4
        return $data;
63
    }
64
65
    /**
66
     * @return array|null
67
     */
68 4
    public function getOrderBy()
69
    {
70 4
        if ($sort = $this->input('sort')) {
71 1
            $fields = explode(',', $sort);
72 1
            $orderBy = [];
73
74 1
            foreach ($fields as $field) {
75 1
                if (empty($field)) continue;
76
77 1
                $direction = 'ASC';
78 1
                if ($field[0] === '-') {
79 1
                    $field = substr($field, 1);
80 1
                    $direction = 'DESC';
81
                }
82
83 1
                $orderBy[$field] = $direction;
84
            }
85
86 1
            return $orderBy;
87
        }
88
89 4
        return null;
90
    }
91
92
    /**
93
     * @return null|int
94
     */
95 4
    public function getStart()
96
    {
97 4
        if (null !== ($limit = $this->getLimit())) {
98 1
            if ($number = $this->input('page.number')) {
99 1
                return ($number - 1) * $limit;
100
            }
101
102 1
            return $this->input('page.offset', 0);
103
        }
104
105 4
        return null;
106
    }
107
108
    /**
109
     * @return int|null
110
     */
111 4
    public function getLimit()
112
    {
113 4
        if ($this->has('page')) {
114 1
            if ($this->has('page.number')) {
115 1
                return $this->input('page.size', static::DEFAULT_LIMIT);
116
            }
117
118 1
            return $this->input('page.limit', static::DEFAULT_LIMIT);
119
        }
120
121 4
        return null;
122
    }
123
124
    /**
125
     * @return \Illuminate\Routing\Route|object|string
126
     */
127 6
    public function getId()
128
    {
129 6
        return $this->route('id');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->route('id') returns the type object|string|Illuminate\Routing\Route which is incompatible with the return type mandated by Pz\Doctrine\Rest\Contrac...equestContract::getId() of integer|array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
130
    }
131
132
    /**
133
     * @return array|null
134
     */
135 7
    public function getExclude()
136
    {
137 7
        return $this->input('exclude');
138
    }
139
140
    /**
141
     * @return array|null
142
     */
143 7
    public function getInclude()
144
    {
145 7
        return $this->input('include');
146
    }
147
148 7
    public function getFields()
149
    {
150 7
        return $this->input('fields');
151
    }
152
153
    /**
154
     * @return array|string|null
155
     */
156 4
    public function getFilter()
157
    {
158 4
        return $this->input('filter');
159
    }
160
161
    /**
162
     * @return bool
163
     */
164 8
    protected function passesAuthorization()
165
    {
166 8
        return true;
167
    }
168
169
    /**
170
     * @param Validator $validator
171
     *
172
     * @throws ValidationException
173
     */
174 1
    protected function failedValidation(Validator $validator)
175
    {
176 1
        $exception = RestException::create(Response::HTTP_UNPROCESSABLE_ENTITY, 'Validation failed');
177 1
        foreach ($validator->errors()->getMessages() as $pointer => $messages) {
178 1
            foreach ($messages as $message) {
179 1
                $exception->errorValidation($pointer, $message);
180
            }
181
        }
182
183 1
        throw new ValidationException($validator, RestResponse::exception($exception));
184
    }
185
}
186