Passed
Push — master ( 383b45...c02d9e )
by Pavel
01:44
created

RestRequest   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 155
ccs 52
cts 52
cp 1
rs 9.6
c 0
b 0
f 0
wmc 32

12 Methods

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