Completed
Push — master ( 321399...55a1a5 )
by Pavel
01:36
created

RestRequest::getFilter()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 0
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest;
2
3
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
4
use Pz\Doctrine\Rest\Exceptions\RestException;
5
use Symfony\Component\HttpFoundation\Request;
6
7
class RestRequest implements RestRequestContract
8
{
9
    /**
10
     * @var Request
11
     */
12
    protected $http;
13
14
    /**
15
     * RestRequest constructor.
16
     *
17
     * @param Request $request
18
     */
19 20
    public function __construct(Request $request)
20
    {
21 20
        $this->http = $request;
22 20
    }
23
24
    /**
25
     * @return Request
26
     */
27 20
    public function http()
28
    {
29 20
        return $this->http;
30
    }
31
32
    /**
33
     * @return string
34
     */
35 11
    public function getBaseUrl()
36
    {
37 11
        return $this->http()->getBaseUrl();
38
    }
39
40
    /**
41
     * @return bool
42
     */
43 13
    public function isAcceptJsonApi()
44
    {
45 13
        return in_array(static::JSON_API_CONTENT_TYPE, $this->http()->getAcceptableContentTypes());
46
    }
47
48
    /**
49
     * @return bool
50
     */
51
    public function isContentJsonApi()
52
    {
53
        return $this->http()->headers->get('CONTENT_TYPE') === static::JSON_API_CONTENT_TYPE;
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public function all()
60
    {
61
        return $this->http()->request->all();
62
    }
63
64
    /**
65
     * @return int|array
66
     */
67 6
    public function getId()
68
    {
69 6
        if ($this->http()->isMethod(Request::METHOD_POST)) {
70
            return $this->http()->request->get('data', [])['id'] ?? null;
71
        }
72
73 6
        return $this->http()->get('id');
74
    }
75
76
    /**
77
     * @return null|string
78
     */
79
    public function getType()
80
    {
81
        return $this->http()->request->get('data', [])['type'] ?? null;
82
    }
83
84
    /**
85
     * @return array
86
     * @throws RestException
87
     */
88 3
    public function getData()
89
    {
90 3
        $request = $this->http()->request;
91
92 3
        if ($request->has('data')) {
93 3
            return $request->get('data');
94
        }
95
96
        throw RestException::missingRootData();
97
    }
98
99
    /**
100
     * Get jsonapi fieldsets.
101
     *
102
     * @return array
103
     */
104 13
    public function getFields()
105
    {
106 13
        return $this->http()->get('fields');
107
    }
108
109
    /**
110
     * @return mixed|null
111
     */
112 12
    public function getFilter()
113
    {
114 12
        if ($query = $this->http()->get('filter')) {
115 6
            if (is_string($query) && (null !== ($json = json_decode($query, true)))) {
116 1
                return $json;
117
            }
118
119 6
            return $query;
120
        }
121
122 6
        return null;
123
    }
124
125
    /**
126
     * @return array|null
127
     */
128 7
    public function getOrderBy()
129
    {
130 7
        if ($sort = $this->http()->get('sort')) {
131 1
            $fields = explode(',', $sort);
132 1
            $orderBy = [];
133
134 1
            foreach ($fields as $field) {
135 1
                $direction = 'ASC';
136 1
                if ($field[0] === '-') {
137 1
                    $field = substr($field, 1);
138 1
                    $direction = 'DESC';
139
                }
140
141 1
                $orderBy[$field] = $direction;
142
            }
143
144 1
            return $orderBy;
145
        }
146
147 6
        return null;
148
    }
149
150
    /**
151
     * @return int|null
152
     */
153 7
    public function getStart()
154
    {
155 7
        if (($page = $this->http()->get('page')) && $this->getLimit() !== null) {
156 2
            if (isset($page['number']) && is_numeric($page['number'])) {
157 1
                return ($page['number'] - 1) * $this->getLimit();
158
            }
159
160 2
            return isset($page['offset']) && is_numeric($page['offset']) ? (int) $page['offset'] : 0;
161
        }
162
163 6
        return null;
164
    }
165
166
    /**
167
     * @return int|null
168
     */
169 7
    public function getLimit()
170
    {
171 7
        if ($page = $this->http()->get('page')) {
172 2
            if (isset($page['number']) && is_numeric($page['number'])) {
173 1
                return isset($page['size']) && is_numeric($page['size']) ?
174 1
                    (int) $page['size'] : static::DEFAULT_LIMIT;
175
            }
176
177 2
            return isset($page['limit']) && is_numeric($page['limit']) ?
178 2
                (int) $page['limit'] : static::DEFAULT_LIMIT;
179
        }
180
181 6
        return null;
182
    }
183
184
    /**
185
     * @return array|string|null
186
     */
187 13
    public function getInclude()
188
    {
189 13
        return $this->http()->get('include');
190
    }
191
192
    /**
193
     * @return array|string|null
194
     */
195 13
    public function getExclude()
196
    {
197 13
        return $this->http()->get('exclude');
198
    }
199
}
200