Completed
Push — master ( 734085...6aef37 )
by De Cramer
03:02 queued 23s
created

GridBuilder::build()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 77
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 49
CRAP Score 7

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 77
ccs 49
cts 49
cp 1
rs 6.5755
cc 7
eloc 50
nc 20
nop 2
crap 7

How to fix   Long Method   

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
namespace eXpansion\Framework\Core\Model\Gui\Grid;
4
use eXpansion\Framework\Core\Model\Gui\Action;
5
use eXpansion\Framework\Core\Model\Gui\Factory\LabelFactory;
6
use eXpansion\Framework\Core\Model\Gui\Factory\LineFactory;
7
use eXpansion\Framework\Core\Model\Gui\Factory\PagerFactory;
8
use eXpansion\Framework\Core\Model\Gui\Grid\Column\AbstractColumn;
9
use eXpansion\Framework\Core\Model\Gui\Grid\Column\ActionColumn;
10
use eXpansion\Framework\Core\Model\Gui\Grid\Column\TextColumn;
11
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
12
use eXpansion\Framework\Core\Plugins\Gui\ActionFactory;
13
use eXpansion\Framework\Core\Plugins\Gui\ManialinkFactory;
14
use FML\Controls\Frame;
15
use FML\Controls\Label;
16
use FML\Controls\Quads\Quad_Icons64x64_1;
17
18
19
/**
20
 * Class GridBuilder
21
 *
22
 * @TODO Add possibility to add actions on elements.
23
 *
24
 * @package eXpansion\Framework\Core\Model\Gui\Grid;
25
 * @author  oliver de Cramer <[email protected]>
26
 */
27
class GridBuilder
28
{
29
    /** @var  ActionFactory */
30
    protected $actionFactory;
31
32
    /** @var LineFactory */
33
    protected $titleLineFactory;
34
35
    /** @var LineFactory */
36
    protected $lineFactory;
37
38
    /** @var PagerFactory */
39
    protected $pagerFactory;
40
41
    /** @var DataCollectionInterface */
42
    protected $dataCollection;
43
44
    /** @var ManialinkInterface */
45
    protected $manialink;
46
47
    /** @var ManialinkFactory */
48
    protected $manialinkFactory;
49
50
    /** @var AbstractColumn[] */
51
    protected $columns;
52
53
    /** @var  float */
54
    protected $totalWidthCoefficency = 0;
55
56
    /** @var int */
57
    protected $currentPage = 1;
58
59
    /** @var string */
60
    protected $pageKey;
61
62
    /** @var Action */
63
    protected $actionPreviousPage;
64
    /** @var Action */
65
    protected $actionNextPage;
66
    /** @var Action */
67
    protected $actionLastPage;
68
    /** @var Action */
69
    protected $actionFirstPage;
70
71
    /** @var Action[] */
72
    protected $temporaryActions = [];
73
74
    /**
75
     * GridBuilder constructor.
76
     *
77
     * @param ActionFactory $actionFactory
78
     * @param LineFactory   $lineFactory
79
     * @param LineFactory   $titleLineFactory
80
     * @param PagerFactory  $pagerFactory
81
     */
82 5 View Code Duplication
    public function __construct(
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...
83
        ActionFactory $actionFactory,
84
        LineFactory $lineFactory,
85
        LineFactory $titleLineFactory,
86
        PagerFactory $pagerFactory
87
    ) {
88 5
        $this->actionFactory = $actionFactory;
89 5
        $this->titleLineFactory = $titleLineFactory;
90 5
        $this->lineFactory = $lineFactory;
91 5
        $this->pagerFactory = $pagerFactory;
92
93 5
        $this->pageKey = spl_object_hash($this) . "_key";
94 5
    }
95
96
    /**
97
     * Set the data collection.
98
     *
99
     * @param DataCollectionInterface $dataCollection
100
     *
101
     * @return $this
102
     */
103 5
    public function setDataCollection(DataCollectionInterface $dataCollection)
104
    {
105 5
        $this->dataCollection = $dataCollection;
106
107 5
        return $this;
108
    }
109
110
    /**
111
     * Set the manialink the content is generated for.
112
     *
113
     * @param ManialinkInterface $manialink
114
     *
115
     * @return $this
116
     */
117 5
    public function setManialink(ManialinkInterface $manialink)
118
    {
119 5
        $this->manialink = $manialink;
120
121 5
        $this->actionFirstPage = $this->actionFactory
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->actionFactory->cr...ToFirstPage'), array()) of type string is incompatible with the declared type object<eXpansion\Framework\Core\Model\Gui\Action> of property $actionFirstPage.

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...
122 5
            ->createManialinkAction($manialink, array($this, 'goToFirstPage'), []);
123 5
        $this->actionPreviousPage = $this->actionFactory
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->actionFactory->cr...reviousPage'), array()) of type string is incompatible with the declared type object<eXpansion\Framework\Core\Model\Gui\Action> of property $actionPreviousPage.

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...
124 5
            ->createManialinkAction($manialink, array($this, 'goToPreviousPage'), []);
125 5
        $this->actionNextPage = $this->actionFactory
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->actionFactory->cr...oToNextPage'), array()) of type string is incompatible with the declared type object<eXpansion\Framework\Core\Model\Gui\Action> of property $actionNextPage.

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...
126 5
            ->createManialinkAction($manialink, array($this, 'goToNextPage'), []);
127 5
        $this->actionLastPage = $this->actionFactory
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->actionFactory->cr...oToLastPage'), array()) of type string is incompatible with the declared type object<eXpansion\Framework\Core\Model\Gui\Action> of property $actionLastPage.

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...
128 5
            ->createManialinkAction($manialink, array($this, 'goToLastPage'), []);
129
130 5
        return $this;
131
    }
132
133
    /**
134
     * Set the manialink factory responsible with the manialink.
135
     *
136
     * @param ManialinkFactory $manialinkFactory
137
     *
138
     * @return $this
139
     */
140 5
    public function setManialinkFactory($manialinkFactory)
141
    {
142 5
        $this->manialinkFactory = $manialinkFactory;
143
144 5
        return $this;
145
    }
146
147
    /**
148
     * @param      $key
149
     * @param      $name
150
     * @param      $widthCoefficiency
151
     * @param bool $sortable
152
     * @param bool $translatable
153
     *
154
     * @return $this
155
     */
156 5
    public function addTextColumn($key, $name, $widthCoefficiency, $sortable = false, $translatable = false)
157
    {
158 5
        $this->columns[] = new TextColumn($key, $name, $widthCoefficiency, $sortable, $translatable);
159
160 5
        return $this;
161
    }
162
163
    /**
164
     * Add an action into a column.
165
     *
166
     * @param $key
167
     * @param $name
168
     * @param $widthCoefficiency
169
     * @param $action
170
     * @param $renderer
171
     */
172 5
    public function addActionColumn($key, $name, $widthCoefficiency, $action, $renderer)
173
    {
174 5
        $this->columns[] = new ActionColumn($key, $name, $widthCoefficiency, $action, $renderer);
175 5
    }
176
177
    /**
178
     * Remove all columns.
179
     */
180 1
    public function resetColumns()
181
    {
182 1
        $this->columns = [];
183 1
        $this->totalWidthCoefficency = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $totalWidthCoefficency was declared of type double, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
184 1
    }
185
186
    /**
187
     * Build a grid.
188
     *
189
     * @param $width
190
     * @param $height
191
     *
192
     * @return Frame
193
     */
194 5
    public function build($width, $height)
195
    {
196 5
        foreach ($this->temporaryActions as $action) {
197 1
            $this->actionFactory->destroyAction($action);
198
        }
199
200 5
        $lineHeight = 5 + 0.5;
201
202 5
        $frame = new Frame();
203 5
        $frame->setPosition(0,0);
204 5
        $frame->setSize($width, $height);
205
206 5
        $posY = 0;
207
        // Generating headers.
208
        // TODO if sortable create actions...
209 5
        $data = [];
210 5
        foreach ($this->columns as $columnData) {
211 5
            $data[] = [
212 5
                'text' => $columnData->getName(),
213 5
                'width' => $columnData->getWidthCoeficiency(),
214
                'translatable' => true
215
            ];
216
        }
217
218 5
        $frame->addChild($this->titleLineFactory->create($frame->getWidth(), $data));
219 5
        $posY -= $lineHeight + 1;
220
221
        /*
222
         * Display the main content.
223
         */
224 5
        $this->dataCollection->setPageSize(floor(($frame->getHeight() + $posY - $lineHeight - 2) / $lineHeight));
225
226 5
        $lines = $this->dataCollection->getData($this->currentPage);
227 5
        foreach ($lines as $i => $lineData) {
228 5
            $data = [];
229 5
            foreach ($this->columns as $columnData) {
230 5
                if ($columnData instanceof TextColumn) {
231 5
                    $data[] = [
232 5
                        'text' => $this->dataCollection->getLineData($lineData, $columnData->getKey()),
233 5
                        'width' => $columnData->getWidthCoeficiency(),
234 5
                        'translatable' => $columnData->getTranslatable()
235
                    ];
236 5
                } elseif($columnData instanceof ActionColumn) {
237 5
                    $action = $this->actionFactory
238 5
                        ->createManialinkAction($this->manialink, $columnData->getCallable(), $lineData);
239 5
                    $this->temporaryActions[] = $action;
240 5
                    $data[] = [
241 5
                        'renderer' => clone $columnData->getRenderer(),
242 5
                        'width' => $columnData->getWidthCoeficiency(),
243 5
                        'action' => $action,
244
                    ];
245
                }
246
            }
247 5
            $line = $this->lineFactory->create($frame->getWidth(), $data, $i);
248 5
            $line->setPosition(0, $posY);
249 5
            $frame->addChild($line);
250 5
            $posY -= $lineHeight;
251
        }
252
253
        /*
254
         * Handle the pager.
255
         */
256 5
        $posY = ($frame->getHeight() -7) * -1;
257 5
        $pager = $this->pagerFactory->create(
258 5
            $frame->getWidth(),
259 5
            $this->currentPage,
260 5
            $this->dataCollection->getLastPageNumber(),
261 5
            $this->actionFirstPage,
262 5
            $this->actionPreviousPage,
263 5
            $this->actionNextPage,
264 5
            $this->actionLastPage
265
        );
266 5
        $pager->setPosition(0, $posY);
267 5
        $frame->addChild($pager);
268
269 5
        return $frame;
270
    }
271
272
    /**
273
     * Action callback to go to the first page.
274
     */
275 1
    public function goToFirstPage()
276
    {
277 1
        $this->changePage(1);
278 1
    }
279
280
    /**
281
     * Action callback to go to the previous page.
282
     */
283 1
    public function goToPreviousPage()
284
    {
285 1
        if ($this->currentPage - 1 >= 1) {
286 1
            $this->changePage($this->currentPage - 1);
287
        }
288 1
    }
289
290
    /**
291
     * Action callback to go to the next page.
292
     */
293 3
    public function goToNextPage()
294
    {
295 3
        if ($this->currentPage + 1 <= $this->dataCollection->getLastPageNumber()) {
296 3
            $this->changePage($this->currentPage + 1);
297
        }
298 3
    }
299
300
    /**
301
     * Action callback to go to the last page.
302
     */
303 1
    public function goToLastPage()
304
    {
305 1
        $this->changePage($this->dataCollection->getLastPageNumber());
306 1
    }
307
308
    /**
309
     * Handle page change & refresh user window.
310
     *
311
     * @param $page
312
     */
313 4
    protected function changePage($page)
314
    {
315 4
        $this->currentPage = $page;
316 4
        $this->manialinkFactory->update($this->manialink->getUserGroup());
317
    }
318
}