Completed
Push — master ( 2700ef...362ace )
by Pavel
03:09
created

RestRequest::getBasePath()   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\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 21
    public function __construct(Request $request)
20
    {
21 21
        $this->http = $request;
22 21
    }
23
24
    /**
25
     * @return Request
26
     */
27 21
    public function http()
28
    {
29 21
        return $this->http;
30
    }
31
32
    /**
33
     * @return string
34
     */
35 2
    public function getBasePath()
36
    {
37 2
        return $this->http()->getPathInfo();
38
    }
39
40
    /**
41
     * @return string
42
     */
43 13
    public function getBaseUrl()
44
    {
45 13
        return $this->http()->getBaseUrl();
46
    }
47
48
    /**
49
     * @return int|array
50
     */
51 7
    public function getId()
52
    {
53 7
        return $this->http()->get('id');
54
    }
55
56
    /**
57
     * @return array
58
     * @throws RestException
59
     */
60 5
    public function getData()
61
    {
62 5
        $request = $this->http()->request;
63
64 5
        if ($request->has('data')) {
65 4
            return $request->get('data');
66
        }
67
68 1
        throw RestException::missingRootData();
69
    }
70
71
    /**
72
     * Get jsonapi fieldsets.
73
     *
74
     * @return array
75
     */
76 13
    public function getFields()
77
    {
78 13
        return $this->http()->get('fields');
79
    }
80
81
    /**
82
     * @return mixed|null
83
     */
84 12
    public function getFilter()
85
    {
86 12
        if ($query = $this->http()->get('filter')) {
87 6
            if (is_string($query) && (null !== ($json = json_decode($query, true)))) {
88 1
                return $json;
89
            }
90
91 6
            return $query;
92
        }
93
94 6
        return null;
95
    }
96
97
    /**
98
     * @return array|null
99
     */
100 7
    public function getOrderBy()
101
    {
102 7
        if ($sort = $this->http()->get('sort')) {
103 1
            $fields = explode(',', $sort);
104 1
            $orderBy = [];
105
106 1
            foreach ($fields as $field) {
107 1
                $direction = 'ASC';
108 1
                if ($field[0] === '-') {
109 1
                    $field = substr($field, 1);
110 1
                    $direction = 'DESC';
111
                }
112
113 1
                $orderBy[$field] = $direction;
114
            }
115
116 1
            return $orderBy;
117
        }
118
119 6
        return null;
120
    }
121
122
    /**
123
     * @return int|null
124
     */
125 7
    public function getStart()
126
    {
127 7
        if (($page = $this->http()->get('page')) && $this->getLimit() !== null) {
128 2
            if (isset($page['number']) && is_numeric($page['number'])) {
129 2
                return ($page['number'] - 1) * $this->getLimit();
130
            }
131
132 1
            return isset($page['offset']) && is_numeric($page['offset']) ? (int) $page['offset'] : 0;
133
        }
134
135 6
        return null;
136
    }
137
138
    /**
139
     * @return int|null
140
     */
141 7
    public function getLimit()
142
    {
143 7
        if ($page = $this->http()->get('page')) {
144 2
            if (isset($page['number']) && is_numeric($page['number'])) {
145 2
                return isset($page['size']) && is_numeric($page['size']) ?
146 2
                    (int) $page['size'] : static::DEFAULT_LIMIT;
147
            }
148
149 1
            return isset($page['limit']) && is_numeric($page['limit']) ?
150 1
                (int) $page['limit'] : static::DEFAULT_LIMIT;
151
        }
152
153 6
        return null;
154
    }
155
156
    /**
157
     * @return array|string|null
158
     */
159 13
    public function getInclude()
160
    {
161 13
        $include = $this->http()->get('include');
162
163 13
        if (!is_array($include)) {
164 13
            RestException::invalidInclude();
165
        }
166
167 13
        return $include;
168
    }
169
170
    /**
171
     * @return array|string|null
172
     */
173 13
    public function getExclude()
174
    {
175 13
        return $this->http()->get('exclude');
176
    }
177
}
178