Completed
Push — master ( eef030...971488 )
by Pavel
02:06
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 Symfony\Component\HttpFoundation\Request;
4
5
class RestRequest extends Request
6
{
7
    /**
8
     * Json API type.
9
     */
10
    const JSON_API_CONTENT_TYPE = 'application/vnd.api+json';
11
12
    /**
13
     * Default limit for list.
14
     */
15
    const DEFAULT_LIMIT = 1000;
16
17
    /**
18
     * Authorize rest request.
19
     * Entity will be object for get,update,delete actions.
20
     * Entity will be string for index,create action.
21
     *
22
     * @param object|string $entity
23
     *
24
     * @return mixed
25
     */
26 6
    public function authorize($entity)
0 ignored issues
show
Unused Code introduced by
The parameter $entity is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

26
    public function authorize(/** @scrutinizer ignore-unused */ $entity)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
27
    {
28 6
        return true;
29
    }
30
31
    /**
32
     * @return bool
33
     */
34 6
    public function isAcceptJsonApi()
35
    {
36 6
        return in_array(static::JSON_API_CONTENT_TYPE, $this->getAcceptableContentTypes());
37
    }
38
39
    /**
40
     * @return int|array
41
     */
42
    public function getId()
43
    {
44
        return $this->get('id');
45
    }
46
47
    /**
48
     * Get jsonapi fieldsets.
49
     *
50
     * @return array
51
     */
52 6
    public function getFields()
53
    {
54 6
        return $this->get('fields', []);
55
    }
56
57
    /**
58
     * @return mixed|null
59
     */
60 6
    public function getFilter()
61
    {
62 6
        if ($query = $this->get('filter')) {
63 1
            if (is_string($query) && (null !== ($json = json_decode($query, true)))) {
64 1
                return $json;
65
            }
66
67 1
            return $query;
68
        }
69
70 5
        return null;
71
    }
72
73
    /**
74
     * @return array|null
75
     */
76 6
    public function getOrderBy()
77
    {
78 6
        if ($sort = $this->get('sort')) {
79 1
            $fields = explode(',', $sort);
80 1
            $orderBy = [];
81
82 1
            foreach ($fields as $field) {
83 1
                if (empty($field)) continue;
84
85 1
                $direction = 'ASC';
86 1
                if ($field[0] === '-') {
87 1
                    $field = substr($field, 1);
88 1
                    $direction = 'DESC';
89
                }
90
91 1
                $orderBy[$field] = $direction;
92
            }
93
94 1
            return $orderBy;
95
        }
96
97 5
        return null;
98
    }
99
100
    /**
101
     * @return int|null
102
     */
103 6
    public function getStart()
104
    {
105 6
        if (($page = $this->get('page')) && $this->getLimit()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getLimit() of type null|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
106 2
            if (isset($page['number']) && is_numeric($page['number'])) {
107 1
                return ($page['number'] - 1) * $this->getLimit();
108
            }
109
110 2
            return isset($page['offset']) && is_numeric($page['offset']) ? (int) $page['offset'] : 0;
111
        }
112
113 5
        return null;
114
    }
115
116
    /**
117
     * @return int|null
118
     */
119 6
    public function getLimit()
120
    {
121 6
        if ($page = $this->get('page')) {
122 2
            if (isset($page['number']) && is_numeric($page['number'])) {
123 1
                return isset($page['size']) && is_numeric($page['size']) ?
124 1
                    (int) $page['size'] : $this->getDefaultLimit();
125
            }
126
127 2
            return isset($page['limit']) && is_numeric($page['limit']) ?
128 2
                (int) $page['limit'] : $this->getDefaultLimit();
129
        }
130
131 5
        return null;
132
    }
133
134
    /**
135
     * @return array|string|null
136
     */
137 6
    public function getInclude()
138
    {
139 6
        return $this->get('include');
140
    }
141
142
    /**
143
     * @return array|string|null
144
     */
145 6
    public function getExclude()
146
    {
147 6
        return $this->get('exclude');
148
    }
149
150
    /**
151
     * @return int
152
     */
153 1
    protected function getDefaultLimit()
154
    {
155 1
        return static::DEFAULT_LIMIT;
156
    }
157
}
158