Completed
Push — master ( c6c542...53c389 )
by Pavel
07:10
created

RestRequest::failedValidation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
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
     * @return array
16
     */
17 7
    public function rules()
18
    {
19
        return [
20 7
            'filter'        => 'sometimes|required',
21
            'include'       => 'sometimes|required|array',
22
            'exclude'       => 'sometimes|required|array',
23
            'fields'        => 'sometimes|required|array',
24
            'sort'          => 'sometimes|required|string',
25
            'page'          => 'sometimes|required|array',
26
            'page.number'   => 'sometimes|required|numeric',
27
            'page.size'     => 'sometimes|required|numeric',
28
            'page.limit'    => 'sometimes|required|numeric',
29
            'page.offset'   => 'sometimes|required|numeric',
30
        ];
31
    }
32
33
    /**
34
     * @return array
35
     * @throws RestException
36
     */
37 3
    public function getData()
38
    {
39 3
        if ((null === $data = $this->get('data')) || !is_array($data)) {
40 1
            throw RestException::missingRootData();
41
        }
42
43 3
        return $data;
44
    }
45
46
    /**
47
     * @return array|null
48
     */
49 3
    public function getOrderBy()
50
    {
51 3
        if ($sort = $this->input('sort')) {
52 1
            $fields = explode(',', $sort);
53 1
            $orderBy = [];
54
55 1
            foreach ($fields as $field) {
56 1
                if (empty($field)) continue;
57
58 1
                $direction = 'ASC';
59 1
                if ($field[0] === '-') {
60 1
                    $field = substr($field, 1);
61 1
                    $direction = 'DESC';
62
                }
63
64 1
                $orderBy[$field] = $direction;
65
            }
66
67 1
            return $orderBy;
68
        }
69
70 3
        return null;
71
    }
72
73
    /**
74
     * @return null|int
75
     */
76 3
    public function getStart()
77
    {
78 3
        if (null !== ($limit = $this->getLimit())) {
79 1
            if ($number = $this->input('page.number')) {
80 1
                return ($number - 1) * $limit;
81
            }
82
83 1
            return $this->input('page.offset', 0);
84
        }
85
86 3
        return null;
87
    }
88
89
    /**
90
     * @return int|null
91
     */
92 3
    public function getLimit()
93
    {
94 3
        if ($this->has('page')) {
95 1
            if ($this->has('page.number')) {
96 1
                return $this->input('page.size', static::DEFAULT_LIMIT);
97
            }
98
99 1
            return $this->input('page.limit', static::DEFAULT_LIMIT);
100
        }
101
102 3
        return null;
103
    }
104
105
    /**
106
     * @return \Illuminate\Routing\Route|object|string
107
     */
108 5
    public function getId()
109
    {
110 5
        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...
111
    }
112
113
    /**
114
     * @return array|null
115
     */
116 6
    public function getExclude()
117
    {
118 6
        return $this->input('exclude');
119
    }
120
121
    /**
122
     * @return array|null
123
     */
124 6
    public function getInclude()
125
    {
126 6
        return $this->input('include');
127
    }
128
129 6
    public function getFields()
130
    {
131 6
        return $this->input('fields');
132
    }
133
134
    /**
135
     * @return array|string|null
136
     */
137 3
    public function getFilter()
138
    {
139 3
        return $this->input('filter');
140
    }
141
142
    /**
143
     * @return bool
144
     */
145 7
    protected function passesAuthorization()
146
    {
147 7
        return true;
148
    }
149
150
    /**
151
     * @param Validator $validator
152
     *
153
     * @throws ValidationException
154
     */
155 1
    protected function failedValidation(Validator $validator)
156
    {
157 1
        $exception = RestException::create(Response::HTTP_UNPROCESSABLE_ENTITY, 'Validation failed');
158 1
        foreach ($validator->errors()->getMessages() as $pointer => $messages) {
159 1
            foreach ($messages as $message) {
160 1
                $exception->errorValidation($pointer, $message);
161
            }
162
        }
163
164 1
        throw new ValidationException($validator, RestResponse::exception($exception));
165
    }
166
}
167