Completed
Push — develop ( f73d3c...b4b400 )
by
unknown
09:18
created

ListRestHelper::getIds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Component\Rest\ListBuilder;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
17
/**
18
 * This is an service helper for ListResources accessed
19
 * by an REST-API. It contains a few getters, which
20
 * deliver some values needed by the inheriting controller.
21
 * These values are calculated from the request paramaters.
22
 */
23
class ListRestHelper implements ListRestHelperInterface
24
{
25
    /**
26
     * The current request object.
27
     *
28
     * @var Request
29
     */
30
    protected $requestStack;
31
32
    /**
33
     * The constructor takes the request stack as an argument, which
34
     * is injected by the service container.
35
     *
36
     * @param RequestStack $requestStack
37
     */
38
    public function __construct(RequestStack $requestStack)
39
    {
40
        $this->requestStack = $requestStack;
0 ignored issues
show
Documentation Bug introduced by
It seems like $requestStack of type object<Symfony\Component...oundation\RequestStack> is incompatible with the declared type object<Symfony\Component\HttpFoundation\Request> of property $requestStack.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
    }
42
43
    /**
44
     * Returns the current Request.
45
     *
46
     * @return Request
47
     */
48
    protected function getRequest()
49
    {
50
        return $this->requestStack->getCurrentRequest();
51
    }
52
53
    /**
54
     * Returns an array of ids that specifies which entities should be included in the response.
55
     * If null is returned, entities in the response should not be filtered by their id.
56
     *
57
     * @return array
58
     */
59
    public function getIds()
60
    {
61
        $idsString = $this->getRequest()->get('ids');
62
63
        return (null !== $idsString) ? explode(',', $idsString) : [];
64
    }
65
66
67
    /**
68
     * Returns an array of ids which should be excluded in the response.
69
     *
70
     * @return array
71
     */
72
    public function getExcludedIds()
73
    {
74
        $excludedIdsString = $this->getRequest()->get('excludedIds');
75
76
        return (null !== $excludedIdsString) ? explode(',', $excludedIdsString) : [];
77
    }
78
79
    /**
80
     * Returns the desired sort column.
81
     *
82
     * @return string
83
     */
84
    public function getSortColumn()
85
    {
86
        return $this->getRequest()->get('sortBy', null);
87
    }
88
89
    /**
90
     * Returns desired sort order.
91
     *
92
     * @return string
93
     */
94
    public function getSortOrder()
95
    {
96
        return $this->getRequest()->get('sortOrder', 'asc');
97
    }
98
99
    /**
100
     * Returns the maximum number of elements in a single response.
101
     *
102
     * @return int
103
     */
104
    public function getLimit()
105
    {
106
        $default = 10;
107
        if ('csv' === $this->getRequest()->getRequestFormat()) {
108
            $default = null;
109
        }
110
111
        return $this->getRequest()->get('limit', $default);
112
    }
113
114
    /**
115
     * Returns the calculated value for the starting position based
116
     * on the page and limit values.
117
     *
118
     * @return int|null
119
     */
120 View Code Duplication
    public function getOffset()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122
        $page = $this->getRequest()->get('page', 1);
123
        $limit = $this->getLimit();
124
125
        return (null != $limit) ? $limit * ($page - 1) : null;
126
    }
127
128
    /**
129
     * returns the current page.
130
     *
131
     * @return mixed
132
     */
133
    public function getPage()
134
    {
135
        return $this->getRequest()->get('page', 1);
136
    }
137
138
    /**
139
     * Returns an array with all the fields, which should be contained in the response.
140
     * If null is returned every field should be contained.
141
     *
142
     * @return array|null
143
     */
144
    public function getFields()
145
    {
146
        $fields = $this->getRequest()->get('fields');
147
148
        return (null != $fields) ? explode(',', $fields) : null;
149
    }
150
151
    /**
152
     * Returns the pattern of the search.
153
     *
154
     * @return mixed
155
     */
156
    public function getSearchPattern()
157
    {
158
        return $this->getRequest()->get('search');
159
    }
160
161
    /**
162
     * Returns an array with all the fields the search pattern should be executed on.
163
     *
164
     * @return array|null
165
     */
166
    public function getSearchFields()
167
    {
168
        $searchFields = $this->getRequest()->get('searchFields');
169
170
        return (null != $searchFields) ? explode(',', $searchFields) : [];
171
    }
172
}
173