Completed
Push — master ( 5bcbf0...e28d31 )
by Pavel
02:54
created

RestRequest::getFields()   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\LaravelDoctrine\Rest;
2
3
use Illuminate\Foundation\Http\FormRequest;
4
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
5
6
class RestRequest extends FormRequest implements RestRequestContract
7
{
8
    /**
9
     * @return array
10
     */
11 5
    public function rules()
12
    {
13
        return [
14 5
            'filter'        => 'sometimes|required',
15
            'include'       => 'sometimes|required|array',
16
            'exclude'       => 'sometimes|required|array',
17
            'fields'        => 'sometimes|required|array',
18
            'sort'          => 'sometimes|required|string',
19
            'page'          => 'sometimes|required|array',
20
            'page.number'   => 'sometimes|required|numeric',
21
            'page.size'     => 'sometimes|required|numeric',
22
            'page.limit'    => 'sometimes|required|numeric',
23
            'page.offset'   => 'sometimes|required|numeric',
24
        ];
25
    }
26
27
    /**
28
     * @return array|null
29
     */
30 2
    public function getOrderBy()
31
    {
32 2
        if ($sort = $this->input('sort')) {
33 1
            $fields = explode(',', $sort);
34 1
            $orderBy = [];
35
36 1
            foreach ($fields as $field) {
37 1
                if (empty($field)) continue;
38
39 1
                $direction = 'ASC';
40 1
                if ($field[0] === '-') {
41 1
                    $field = substr($field, 1);
42 1
                    $direction = 'DESC';
43
                }
44
45 1
                $orderBy[$field] = $direction;
46
            }
47
48 1
            return $orderBy;
49
        }
50
51 2
        return null;
52
    }
53
54
    /**
55
     * @return null|int
56
     */
57 2
    public function getStart()
58
    {
59 2
        if (null !== ($limit = $this->getLimit())) {
60 1
            if ($number = $this->input('page.number')) {
61 1
                return ($number - 1) * $limit;
62
            }
63
64 1
            return $this->input('page.offset', 0);
65
        }
66
67 2
        return null;
68
    }
69
70
    /**
71
     * @return int|null
72
     */
73 2
    public function getLimit()
74
    {
75 2
        if ($this->has('page')) {
76 1
            if ($this->has('page.number')) {
77 1
                return $this->input('page.size', static::DEFAULT_LIMIT);
78
            }
79
80 1
            return $this->input('page.limit', static::DEFAULT_LIMIT);
81
        }
82
83 2
        return null;
84
    }
85
86
    /**
87
     * @return bool
88
     */
89 5
    public function isAcceptJsonApi()
90
    {
91 5
        return in_array(RestRequestContract::JSON_API_CONTENT_TYPE, $this->getAcceptableContentTypes());
92
    }
93
94
    /**
95
     * @return bool
96
     */
97 2
    public function isContentJsonApi()
98
    {
99 2
        return $this->headers->get('CONTENT_TYPE') === static::JSON_API_CONTENT_TYPE;
100
    }
101
102
    /**
103
     * @return \Illuminate\Routing\Route|object|string
104
     */
105 4
    public function getId()
106
    {
107 4
        return $this->route('id');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->route('id') returns the type object|string|Illuminate\Routing\Route which is incompatible with the return type mandated by Pz\Doctrine\Rest\Contrac...equestContract::getId() of integer|array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
108
    }
109
110
    /**
111
     * @return array|null
112
     */
113 5
    public function getExclude()
114
    {
115 5
        return $this->input('exclude');
116
    }
117
118
    /**
119
     * @return array|null
120
     */
121 5
    public function getInclude()
122
    {
123 5
        return $this->input('include');
124
    }
125
126 5
    public function getFields()
127
    {
128 5
        return $this->input('fields');
129
    }
130
131
    /**
132
     * @return array|string|null
133
     */
134 2
    public function getFilter()
135
    {
136 2
        return $this->input('filter');
137
    }
138
139
    /**
140
     * @return bool
141
     */
142 5
    protected function passesAuthorization()
143
    {
144 5
        return true;
145
    }
146
}
147