Passed
Push — master ( 1316c5...63993c )
by Matthew
05:18
created

AbstractGridSource::getParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Dtc\GridBundle\Grid\Source;
4
5
use Dtc\GridBundle\Grid\Column\AbstractGridColumn;
6
use Dtc\GridBundle\Grid\Pager\GridSourcePager;
7
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
abstract class AbstractGridSource implements GridSourceInterface
10
{
11
    protected $limit = 25;
12
    protected $offset = 0;
13
    protected $filter = array();
14
    protected $orderBy = array();
15
    protected $pager = array();
16
    protected $id = 'grid';
17
    protected $columns;
18
    protected $parameters;
19
    protected $defaultSort;
20
21
    public function bind(Request $request)
22
    {
23
        // Change limit, offset.
24
        if ($limit = $request->get('limit')) {
25
            $this->limit = $limit;
26
        }
27
28
        if ($page = $request->get('page')) {
29
            $this->offset = $this->limit * ($page - 1);
30
        }
31
32
        if ($offset = $request->get('offset')) {
33
            $this->offset = $offset;
34
        }
35
36
        if ($filter = $request->get('filter')) {
37
            $this->filter = $filter;
38
        }
39
40
        if ($sortColumn = $request->get('sort_column')) {
41
            $sortOrder = $request->get('sort_order');
42
            $sortOrder = strtoupper($sortOrder);
43
44
            $this->orderBy[$sortColumn] = $sortOrder;
45
        }
46
47
        if ($orderBy = $request->get('order')) {
48
            $this->orderBy = $orderBy;
49
        }
50
    }
51
52
    public function setDefaultSort($sort)
53
    {
54
        $this->defaultSort = $sort;
55
    }
56
57
    public function getDefaultSort()
58
    {
59
        return $this->defaultSort;
60
    }
61
62
    public function getId()
63
    {
64
        return $this->id;
65
    }
66
67
    private $divId = null;
68
69
    public function getDivId()
70
    {
71
        if (!$this->divId) {
72
            $this->divId = preg_replace('/[^a-zA-Z0-9\-]/', '', $this->id);
73
        }
74
75
        return $this->divId;
76
    }
77
78
    public function setId($value)
79
    {
80
        $this->id = $value;
81
    }
82
83
    public function getColumns()
84
    {
85
        return $this->columns;
86
    }
87
88
    public function setColumns($columns)
89
    {
90
        /** @var AbstractGridColumn $col */
91
        foreach ($columns as $col) {
92
            $this->columns[$col->getField()] = $col;
93
        }
94
    }
95
96
    public function removeColumn($field)
97
    {
98
        $this->removeColumns(func_get_args());
99
    }
100
101
    public function selectColumns(array $fields)
102
    {
103
        $selectedCols = array();
104
        foreach ($fields as $field) {
105
            if (isset($this->columns[$field])) {
106
                $selectedCols[$field] = $this->columns[$field];
107
            }
108
        }
109
110
        $this->columns = $selectedCols;
111
    }
112
113
    public function removeColumns(array $fields)
114
    {
115
        foreach ($fields as $field) {
116
            unset($this->columns[$field]);
117
        }
118
    }
119
120
    public function getPager()
121
    {
122
        if (!$this->pager) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->pager of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
123
            $this->pager = new GridSourcePager($this);
124
        }
125
126
        return $this->pager;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->pager returns the type array which is incompatible with the return type mandated by Dtc\GridBundle\Grid\Sour...ceInterface::getPager() of Dtc\GridBundle\Grid\Pager\GridSourcePager.

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...
127
    }
128
129
    /**
130
     * @return int $limit
131
     */
132
    public function getLimit()
133
    {
134
        return $this->limit;
135
    }
136
137
    /**
138
     * @param int $limit
139
     */
140
    public function setLimit($limit)
141
    {
142
        $this->limit = $limit;
143
    }
144
145
    /**
146
     * @return int $offset
147
     */
148
    public function getOffset()
149
    {
150
        return $this->offset;
151
    }
152
153
    /**
154
     * @param int $offset
155
     */
156
    public function setOffset($offset)
157
    {
158
        $this->offset = $offset;
159
    }
160
161
    /**
162
     * @return array $filter
163
     */
164
    public function getFilter()
165
    {
166
        return $this->filter;
167
    }
168
169
    /**
170
     * @param array $filter
171
     */
172
    public function setFilter(array $filter)
173
    {
174
        $this->filter = $filter;
175
    }
176
177
    /**
178
     * @return array $orderBy
179
     */
180
    public function getOrderBy()
181
    {
182
        return $this->orderBy;
183
    }
184
185
    /**
186
     * @param array $orderBy
187
     */
188
    public function setOrderBy(array $orderBy)
189
    {
190
        $this->orderBy = $orderBy;
191
    }
192
193
    public function getLastModified()
194
    {
195
        return null;
196
    }
197
198
    public function getParameter($key)
199
    {
200
        return $this->parameters[$key];
201
    }
202
203
    public function setParameter($key, $value)
204
    {
205
        $this->parameters[$key] = $value;
206
    }
207
208
    /**
209
     * @return array $parameters
210
     */
211
    public function getParameters()
212
    {
213
        return $this->parameters;
214
    }
215
216
    /**
217
     * @param array $parameters
218
     */
219
    public function setParameters(array $parameters)
220
    {
221
        $this->parameters = $parameters;
222
    }
223
224
    /**
225
     * @return bool returns true if this GridSource has an ID or false otherwise
226
     */
227
    public function hasIdColumn()
228
    {
229
        return false;
230
    }
231
232
    /**
233
     * @param $id
234
     *
235
     * @return mixed|null returns the row identified by Id if found
236
     */
237
    public function find($id)
238
    {
239
        return null;
240
    }
241
}
242