Completed
Push — 3.x ( 4109c4...feed83 )
by Jordi Sala
03:54
created

Datagrid::buildPager()   F

Complexity

Conditions 15
Paths 281

Size

Total Lines 77
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 3.9271
c 0
b 0
f 0
cc 15
eloc 46
nc 281
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\AdminBundle\Datagrid;
13
14
use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
15
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
16
use Sonata\AdminBundle\Filter\FilterInterface;
17
use Symfony\Component\Form\CallbackTransformer;
18
use Symfony\Component\Form\Exception\UnexpectedTypeException;
19
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
20
use Symfony\Component\Form\FormBuilderInterface;
21
use Symfony\Component\Form\FormInterface;
22
23
/**
24
 * @author Thomas Rabaix <[email protected]>
25
 */
26
class Datagrid implements DatagridInterface
27
{
28
    /**
29
     * The filter instances.
30
     *
31
     * @var array
32
     */
33
    protected $filters = [];
34
35
    /**
36
     * @var array
37
     */
38
    protected $values;
39
40
    /**
41
     * @var FieldDescriptionCollection
42
     */
43
    protected $columns;
44
45
    /**
46
     * @var PagerInterface
47
     */
48
    protected $pager;
49
50
    /**
51
     * @var bool
52
     */
53
    protected $bound = false;
54
55
    /**
56
     * @var ProxyQueryInterface
57
     */
58
    protected $query;
59
60
    /**
61
     * @var FormBuilderInterface
62
     */
63
    protected $formBuilder;
64
65
    /**
66
     * @var FormInterface
67
     */
68
    protected $form;
69
70
    /**
71
     * @var array
72
     */
73
    protected $results;
74
75
    public function __construct(
76
        ProxyQueryInterface $query,
77
        FieldDescriptionCollection $columns,
78
        PagerInterface $pager,
79
        FormBuilderInterface $formBuilder,
80
        array $values = []
81
    ) {
82
        $this->pager = $pager;
83
        $this->query = $query;
84
        $this->values = $values;
85
        $this->columns = $columns;
86
        $this->formBuilder = $formBuilder;
87
    }
88
89
    public function getPager()
90
    {
91
        return $this->pager;
92
    }
93
94
    public function getResults()
95
    {
96
        $this->buildPager();
97
98
        if (null === $this->results) {
99
            $this->results = $this->pager->getResults();
100
        }
101
102
        return $this->results;
103
    }
104
105
    public function buildPager()
106
    {
107
        if ($this->bound) {
108
            return;
109
        }
110
111
        foreach ($this->getFilters() as $name => $filter) {
112
            list($type, $options) = $filter->getRenderSettings();
113
114
            $this->formBuilder->add($filter->getFormName(), $type, $options);
115
        }
116
117
        $hiddenType = HiddenType::class;
118
119
        $this->formBuilder->add('_sort_by', $hiddenType);
120
        $this->formBuilder->get('_sort_by')->addViewTransformer(new CallbackTransformer(
121
            function ($value) {
122
                return $value;
123
            },
124
            function ($value) {
125
                return $value instanceof FieldDescriptionInterface ? $value->getName() : $value;
126
            }
127
        ));
128
129
        $this->formBuilder->add('_sort_order', $hiddenType);
130
        $this->formBuilder->add('_page', $hiddenType);
131
        $this->formBuilder->add('_per_page', $hiddenType);
132
133
        $this->form = $this->formBuilder->getForm();
134
        $this->form->submit($this->values);
135
136
        $data = $this->form->getData();
137
138
        foreach ($this->getFilters() as $name => $filter) {
139
            $this->values[$name] = isset($this->values[$name]) ? $this->values[$name] : null;
140
            if ($filter->isActive()) {
141
                $filter->apply($this->query, $data[$filter->getFormName()]);
142
            }
143
        }
144
145
        if (isset($this->values['_sort_by'])) {
146
            if (!$this->values['_sort_by'] instanceof FieldDescriptionInterface) {
147
                throw new UnexpectedTypeException($this->values['_sort_by'], FieldDescriptionInterface::class);
148
            }
149
150
            if ($this->values['_sort_by']->isSortable()) {
151
                $this->query->setSortBy($this->values['_sort_by']->getSortParentAssociationMapping(), $this->values['_sort_by']->getSortFieldMapping());
152
                $this->query->setSortOrder(isset($this->values['_sort_order']) ? $this->values['_sort_order'] : null);
153
            }
154
        }
155
156
        $maxPerPage = 25;
157
        if (isset($this->values['_per_page'])) {
158
            if (isset($this->values['_per_page']['value'])) {
159
                $maxPerPage = $this->values['_per_page']['value'];
160
            } else {
161
                $maxPerPage = $this->values['_per_page'];
162
            }
163
        }
164
        $this->pager->setMaxPerPage($maxPerPage);
165
166
        $page = 1;
167
        if (isset($this->values['_page'])) {
168
            if (isset($this->values['_page']['value'])) {
169
                $page = $this->values['_page']['value'];
170
            } else {
171
                $page = $this->values['_page'];
172
            }
173
        }
174
175
        $this->pager->setPage($page);
176
177
        $this->pager->setQuery($this->query);
178
        $this->pager->init();
179
180
        $this->bound = true;
181
    }
182
183
    public function addFilter(FilterInterface $filter)
184
    {
185
        $this->filters[$filter->getName()] = $filter;
186
    }
187
188
    public function hasFilter($name)
189
    {
190
        return isset($this->filters[$name]);
191
    }
192
193
    public function removeFilter($name)
194
    {
195
        unset($this->filters[$name]);
196
    }
197
198
    public function getFilter($name)
199
    {
200
        return $this->hasFilter($name) ? $this->filters[$name] : null;
201
    }
202
203
    public function getFilters()
204
    {
205
        return $this->filters;
206
    }
207
208
    public function reorderFilters(array $keys)
209
    {
210
        $this->filters = array_merge(array_flip($keys), $this->filters);
211
    }
212
213
    public function getValues()
214
    {
215
        return $this->values;
216
    }
217
218
    public function setValue($name, $operator, $value)
219
    {
220
        $this->values[$name] = [
221
            'type' => $operator,
222
            'value' => $value,
223
        ];
224
    }
225
226
    public function hasActiveFilters()
227
    {
228
        foreach ($this->filters as $name => $filter) {
229
            if ($filter->isActive()) {
230
                return true;
231
            }
232
        }
233
234
        return false;
235
    }
236
237
    public function hasDisplayableFilters()
238
    {
239
        foreach ($this->filters as $name => $filter) {
240
            $showFilter = $filter->getOption('show_filter', null);
241
            if (($filter->isActive() && null === $showFilter) || (true === $showFilter)) {
242
                return true;
243
            }
244
        }
245
246
        return false;
247
    }
248
249
    public function getColumns()
250
    {
251
        return $this->columns;
252
    }
253
254
    public function getQuery()
255
    {
256
        return $this->query;
257
    }
258
259
    public function getForm()
260
    {
261
        $this->buildPager();
262
263
        return $this->form;
264
    }
265
}
266