GridHelper   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 230
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 82
dl 0
loc 230
ccs 0
cts 149
cp 0
rs 9.52
c 0
b 0
f 0
wmc 36

25 Methods

Rating   Name   Duplication   Size   Complexity  
A isInfiniteGrid() 0 3 1
A getRpage() 0 3 1
A getExtraOpts() 0 3 1
A setRoute() 0 4 1
A setRouteParams() 0 4 1
A setIterable() 0 4 1
A setId() 0 4 1
A setMaxResult() 0 4 1
A setExtraOpts() 0 4 1
A getId() 0 3 1
A getRstart() 0 3 1
A getRouteParams() 0 3 1
A getRlast() 0 3 1
A getRlength() 0 3 1
A getPerPage() 0 3 1
A setPerPage() 0 4 1
A getPage() 0 3 1
A getResultset() 0 3 1
A getIterable() 0 3 1
A setInfiniteGrid() 0 4 1
A getMaxResult() 0 3 1
A getRoute() 0 3 1
A setQueryBuilder() 0 7 1
C createView() 0 42 11
A __construct() 0 6 2
1
<?php
2
3
namespace LoginCidadao\CoreBundle\Helper;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Doctrine\ORM\QueryBuilder;
7
use LoginCidadao\InfiniteScrollBundle\Model\AbstractInfiniteIterable;
8
use LoginCidadao\InfiniteScrollBundle\Model\QueryBuilderIterator;
9
10
class GridHelper
11
{
12
13
    /** @var integer */
14
    protected $perPage;
15
16
    /** @var integer */
17
    protected $maxResult = 25;
18
19
    /** @var integer */
20
    protected $page;
21
22
    /** @var boolean */
23
    protected $infiniteGrid = false;
24
    protected $resultset;
25
26
    /** @var QueryBuilder */
27
    protected $queryBuilder;
28
    protected $rlength;
29
    protected $rstart;
30
    protected $rlast;
31
    protected $rpage;
32
    protected $id;
33
34
    /** @var string */
35
    protected $route;
36
37
    /** @var array */
38
    protected $routeParams;
39
40
    /** @var array */
41
    protected $extraOpts;
42
43
    /** @var AbstractInfiniteIterable */
44
    protected $iterable;
45
46
    public function __construct(AbstractInfiniteIterable $iterable = null)
47
    {
48
        if (null !== $iterable) {
49
            $this->setIterable($iterable);
50
            $this->setMaxResult($this->getIterable()->getPerIteration());
51
            $this->setPerPage($this->getIterable()->getPerIteration());
52
        }
53
    }
54
55
    /**
56
     * @param boolean $infinite
57
     * @return GridHelper
58
     */
59
    public function setInfiniteGrid($infinite)
60
    {
61
        $this->infiniteGrid = $infinite;
62
        return $this;
63
    }
64
65
    public function setPerPage($var)
66
    {
67
        $this->perPage = $var;
68
        return $this;
69
    }
70
71
    public function setMaxResult($var)
72
    {
73
        $this->maxResult = $var;
74
        return $this;
75
    }
76
77
    /**
78
     * @deprecated since version 1.1.0
79
     * @param QueryBuilder $var
80
     * @return GridHelper
81
     */
82
    public function setQueryBuilder(QueryBuilder & $queryBuilder)
83
    {
84
        $this->queryBuilder = $queryBuilder;
85
        // create QueryBuilderIterator for legacy code
86
        $iterable = new QueryBuilderIterator($queryBuilder, $this->getPerPage());
87
        $this->setIterable($iterable);
88
        return $this;
89
    }
90
91
    public function createView(Request $request)
92
    {
93
        if ($request->get('page')) {
94
            $this->page = $request->get('page');
95
            if (null !== $this->queryBuilder) {
96
                $this->queryBuilder->setFirstResult(($this->page - 1) * $this->maxResult);
97
            }
98
            if (null !== $this->getIterable()) {
99
                $this->getIterable()->setInitialOffset(($this->page - 1) * $this->maxResult);
100
                if (null !== $this->queryBuilder) {
101
                    $this->getIterable()->setOffset($this->getIterable()->getInitialOffset());
102
                }
103
            }
104
        } else {
105
            $this->page = 1;
106
        }
107
        if ($this->infiniteGrid) {
108
            $this->perPage = $this->maxResult;
109
        }
110
        if (null !== $this->getIterable()) {
111
            //$this->queryBuilder->setMaxResults($this->maxResult + 1);
112
            $this->resultset = $this->getIterable()->current();
113
            //$this->resultset = $this->queryBuilder->getQuery()->getResult();
114
        } else {
115
            $this->resultset = array();
116
        }
117
        $this->rlength = count($this->resultset);
118
        $this->rstart = ($this->page * $this->maxResult) / $this->perPage;
119
        $this->rlast = ($this->rlength < $this->maxResult);
120
121
        $this->rpage = (integer) (($this->rlength / $this->perPage) - (($this->rlength - $this->maxResult) > 0 ? 1 : 0));
122
        $this->rpage = $this->rpage > 0 ? $this->rpage : 0;
123
124
        if ($this->routeParams) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->routeParams 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...
125
            foreach ($this->routeParams as $val) {
126
                $a[$val] = $request->get($val);
127
            }
128
            $this->routeParams = $a;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $a seems to be defined by a foreach iteration on line 125. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
129
        } else {
130
            $this->routeParams = array();
131
        }
132
        return $this;
133
    }
134
135
    public function getPage()
136
    {
137
        return $this->page;
138
    }
139
140
    public function getMaxResult()
141
    {
142
        return $this->maxResult;
143
    }
144
145
    public function getPerPage()
146
    {
147
        return $this->perPage;
148
    }
149
150
    public function getResultset()
151
    {
152
        return $this->resultset;
153
    }
154
155
    public function getRlength()
156
    {
157
        return $this->rlength;
158
    }
159
160
    public function getRstart()
161
    {
162
        return $this->rstart;
163
    }
164
165
    public function getRlast()
166
    {
167
        return $this->rlast;
168
    }
169
170
    public function getRpage()
171
    {
172
        return $this->rpage;
173
    }
174
175
    public function isInfiniteGrid()
176
    {
177
        return $this->infiniteGrid;
178
    }
179
180
    public function setId($var)
181
    {
182
        $this->id = $var;
183
        return $this;
184
    }
185
186
    public function getId()
187
    {
188
        return $this->id;
189
    }
190
191
    public function setRoute($var)
192
    {
193
        $this->route = $var;
194
        return $this;
195
    }
196
197
    public function setRouteParams($var)
198
    {
199
        $this->routeParams = $var;
200
        return $this;
201
    }
202
203
    public function getRoute()
204
    {
205
        return $this->route;
206
    }
207
208
    public function getRouteParams()
209
    {
210
        return $this->routeParams;
211
    }
212
213
    public function setExtraOpts($var)
214
    {
215
        $this->extraOpts = $var;
216
        return $this;
217
    }
218
219
    public function getExtraOpts()
220
    {
221
        return $this->extraOpts;
222
    }
223
224
    /**
225
     * @return AbstractInfiniteIterable
226
     */
227
    public function getIterable()
228
    {
229
        return $this->iterable;
230
    }
231
232
    /**
233
     * @param AbstractInfiniteIterable $iterable
234
     * @return GridHelper
235
     */
236
    public function setIterable(AbstractInfiniteIterable $iterable)
237
    {
238
        $this->iterable = $iterable;
239
        return $this;
240
    }
241
242
}
243