Completed
Push — master ( d0fcc8...c6c542 )
by Pavel
02:20
created

RestRequest   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 136
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0
wmc 21

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getExclude() 0 3 1
A passesAuthorization() 0 3 1
A getInclude() 0 3 1
A getFields() 0 3 1
B getOrderBy() 0 22 5
A getId() 0 3 1
A getFilter() 0 3 1
A getData() 0 7 3
A getLimit() 0 11 3
A rules() 0 13 1
A getStart() 0 11 3
1
<?php namespace Pz\LaravelDoctrine\Rest;
2
3
use Illuminate\Foundation\Http\FormRequest;
4
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
5
use Pz\Doctrine\Rest\Exceptions\RestException;
6
7
class RestRequest extends FormRequest implements RestRequestContract
8
{
9
    /**
10
     * @return array
11
     */
12 5
    public function rules()
13
    {
14
        return [
15 5
            'filter'        => 'sometimes|required',
16
            'include'       => 'sometimes|required|array',
17
            'exclude'       => 'sometimes|required|array',
18
            'fields'        => 'sometimes|required|array',
19
            'sort'          => 'sometimes|required|string',
20
            'page'          => 'sometimes|required|array',
21
            'page.number'   => 'sometimes|required|numeric',
22
            'page.size'     => 'sometimes|required|numeric',
23
            'page.limit'    => 'sometimes|required|numeric',
24
            'page.offset'   => 'sometimes|required|numeric',
25
        ];
26
    }
27
28
    /**
29
     * @return array
30
     * @throws RestException
31
     */
32 2
    public function getData()
33
    {
34 2
        if ((null === $data = $this->get('data')) || !is_array($data)) {
35 1
            throw RestException::missingRootData();
36
        }
37
38 2
        return $data;
39
    }
40
41
    /**
42
     * @return array|null
43
     */
44 2
    public function getOrderBy()
45
    {
46 2
        if ($sort = $this->input('sort')) {
47 1
            $fields = explode(',', $sort);
48 1
            $orderBy = [];
49
50 1
            foreach ($fields as $field) {
51 1
                if (empty($field)) continue;
52
53 1
                $direction = 'ASC';
54 1
                if ($field[0] === '-') {
55 1
                    $field = substr($field, 1);
56 1
                    $direction = 'DESC';
57
                }
58
59 1
                $orderBy[$field] = $direction;
60
            }
61
62 1
            return $orderBy;
63
        }
64
65 2
        return null;
66
    }
67
68
    /**
69
     * @return null|int
70
     */
71 2
    public function getStart()
72
    {
73 2
        if (null !== ($limit = $this->getLimit())) {
74 1
            if ($number = $this->input('page.number')) {
75 1
                return ($number - 1) * $limit;
76
            }
77
78 1
            return $this->input('page.offset', 0);
79
        }
80
81 2
        return null;
82
    }
83
84
    /**
85
     * @return int|null
86
     */
87 2
    public function getLimit()
88
    {
89 2
        if ($this->has('page')) {
90 1
            if ($this->has('page.number')) {
91 1
                return $this->input('page.size', static::DEFAULT_LIMIT);
92
            }
93
94 1
            return $this->input('page.limit', static::DEFAULT_LIMIT);
95
        }
96
97 2
        return null;
98
    }
99
100
    /**
101
     * @return \Illuminate\Routing\Route|object|string
102
     */
103 4
    public function getId()
104
    {
105 4
        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...
106
    }
107
108
    /**
109
     * @return array|null
110
     */
111 5
    public function getExclude()
112
    {
113 5
        return $this->input('exclude');
114
    }
115
116
    /**
117
     * @return array|null
118
     */
119 5
    public function getInclude()
120
    {
121 5
        return $this->input('include');
122
    }
123
124 5
    public function getFields()
125
    {
126 5
        return $this->input('fields');
127
    }
128
129
    /**
130
     * @return array|string|null
131
     */
132 2
    public function getFilter()
133
    {
134 2
        return $this->input('filter');
135
    }
136
137
    /**
138
     * @return bool
139
     */
140 5
    protected function passesAuthorization()
141
    {
142 5
        return true;
143
    }
144
}
145