Completed
Push — master ( fca7c2...7b8261 )
by Pavel
02:00
created

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