Completed
Push — master ( 62bb6b...dc3688 )
by Pavel
01:45
created

RestRequest::getLimit()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 7
nc 9
nop 0
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 8
rs 7.7777
c 0
b 0
f 0
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
                $direction = 'ASC';
105 1
                if ($field[0] === '-') {
106 1
                    $field = substr($field, 1);
107 1
                    $direction = 'DESC';
108
                }
109
110 1
                $orderBy[$field] = $direction;
111
            }
112
113 1
            return $orderBy;
114
        }
115
116 6
        return null;
117
    }
118
119
    /**
120
     * @return int|null
121
     */
122 7
    public function getStart()
123
    {
124 7
        if (($page = $this->http()->get('page')) && $this->getLimit() !== null) {
125 2
            if (isset($page['number']) && is_numeric($page['number'])) {
126 1
                return ($page['number'] - 1) * $this->getLimit();
127
            }
128
129 2
            return isset($page['offset']) && is_numeric($page['offset']) ? (int) $page['offset'] : 0;
130
        }
131
132 6
        return null;
133
    }
134
135
    /**
136
     * @return int|null
137
     */
138 7
    public function getLimit()
139
    {
140 7
        if ($page = $this->http()->get('page')) {
141 2
            if (isset($page['number']) && is_numeric($page['number'])) {
142 1
                return isset($page['size']) && is_numeric($page['size']) ?
143 1
                    (int) $page['size'] : static::DEFAULT_LIMIT;
144
            }
145
146 2
            return isset($page['limit']) && is_numeric($page['limit']) ?
147 2
                (int) $page['limit'] : static::DEFAULT_LIMIT;
148
        }
149
150 6
        return null;
151
    }
152
153
    /**
154
     * @return array|string|null
155
     */
156 13
    public function getInclude()
157
    {
158 13
        return $this->http()->get('include');
159
    }
160
161
    /**
162
     * @return array|string|null
163
     */
164 13
    public function getExclude()
165
    {
166 13
        return $this->http()->get('exclude');
167
    }
168
}
169