AbstractGridSource::setId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
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 = [];
14
    protected $orderBy = [];
15
    protected $pager = [];
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 = [];
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
    public function setFilter(array $filter)
170
    {
171
        $this->filter = $filter;
172
    }
173
174
    /**
175
     * @return array $orderBy
176
     */
177
    public function getOrderBy()
178
    {
179
        return $this->orderBy;
180
    }
181
182
    public function setOrderBy(array $orderBy)
183
    {
184
        $this->orderBy = $orderBy;
185
    }
186
187
    public function getLastModified()
188
    {
189
        return null;
190
    }
191
192
    public function getParameter($key)
193
    {
194
        return $this->parameters[$key];
195
    }
196
197
    public function setParameter($key, $value)
198
    {
199
        $this->parameters[$key] = $value;
200
    }
201
202
    /**
203
     * @return array $parameters
204
     */
205
    public function getParameters()
206
    {
207
        return $this->parameters;
208
    }
209
210
    public function setParameters(array $parameters)
211
    {
212
        $this->parameters = $parameters;
213
    }
214
215
    /**
216
     * @return bool returns true if this GridSource has an ID or false otherwise
217
     */
218
    public function hasIdColumn()
219
    {
220
        return false;
221
    }
222
223
    /**
224
     * @param $id
225
     *
226
     * @return mixed|null returns the row identified by Id if found
227
     */
228
    public function find($id)
229
    {
230
        return null;
231
    }
232
}
233