GridBuilder::buildColumnActions()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 7
cp 1
cc 3
eloc 6
nc 2
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Grid\Model\Builder;
13
14
use Lug\Component\Grid\Exception\ConfigNotFoundException;
15
use Lug\Component\Grid\Model\ActionInterface;
16
use Lug\Component\Grid\Model\BatchInterface;
17
use Lug\Component\Grid\Model\ColumnInterface;
18
use Lug\Component\Grid\Model\FilterInterface;
19
use Lug\Component\Grid\Model\Grid;
20
use Lug\Component\Grid\Model\SortInterface;
21
use Lug\Component\Resource\Model\ResourceInterface;
22
23
/**
24
 * @author GeLo <[email protected]>
25
 */
26
class GridBuilder implements GridBuilderInterface
27
{
28
    /**
29
     * @var ColumnBuilderInterface
30
     */
31
    private $columnBuilder;
32
33
    /**
34
     * @var FilterBuilderInterface
35
     */
36
    private $filterBuilder;
37
38
    /**
39
     * @var SortBuilderInterface
40
     */
41
    private $sortBuilder;
42
43
    /**
44
     * @var ActionBuilderInterface
45
     */
46
    private $actionBuilder;
47
48
    /**
49
     * @var BatchBuilderInterface
50
     */
51
    private $batchBuilder;
52
53
    /**
54
     * @param ColumnBuilderInterface $columnBuilder
55
     * @param FilterBuilderInterface $filterBuilder
56
     * @param SortBuilderInterface   $sortBuilder
57
     * @param ActionBuilderInterface $actionBuilder
58
     * @param BatchBuilderInterface  $batchBuilder
59
     */
60 7
    public function __construct(
61
        ColumnBuilderInterface $columnBuilder,
62
        FilterBuilderInterface $filterBuilder,
63
        SortBuilderInterface $sortBuilder,
64
        ActionBuilderInterface $actionBuilder,
65
        BatchBuilderInterface $batchBuilder
66
    ) {
67 7
        $this->columnBuilder = $columnBuilder;
68 7
        $this->filterBuilder = $filterBuilder;
69 7
        $this->sortBuilder = $sortBuilder;
70 7
        $this->actionBuilder = $actionBuilder;
71 7
        $this->batchBuilder = $batchBuilder;
72 7
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 5
    public function build(array $config)
78
    {
79 5
        $config = $this->prepareConfig($config);
80
81 5
        return new Grid(
82 5
            $this->buildResource($config),
83 4
            $this->buildColumns($config),
84 4
            $this->buildFilters($config),
85 4
            $this->buildSorts($config),
86 4
            $this->buildGlobalActions($config),
87 4
            $this->buildColumnActions($config),
88 4
            $this->buildBatches($config),
89 4
            $this->buildData($config),
90 4
            $this->buildOptions($config)
91 4
        );
92
    }
93
94
    /**
95
     * @param mixed[] $config
96
     *
97
     * @return ResourceInterface
98
     */
99 5
    protected function buildResource(array $config)
100
    {
101 5
        if (!isset($config['resource'])) {
102 1
            throw new ConfigNotFoundException(sprintf('The grid config "resource" could not be found.'));
103
        }
104
105 4
        return $config['resource'];
106
    }
107
108
    /**
109
     * @param mixed[] $config
110
     *
111
     * @return ColumnInterface[]
112
     */
113 4 View Code Duplication
    protected function buildColumns(array $config)
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...
114
    {
115 4
        $columns = [];
116
117 4
        foreach (isset($config['columns']) ? $config['columns'] : [] as $name => $column) {
118 2
            $column = $this->buildColumn(array_merge(['name' => $name], $column), $config);
119 2
            $columns[$column->getName()] = $column;
120 4
        }
121
122 4
        return $columns;
123
    }
124
125
    /**
126
     * @param mixed[] $config
127
     * @param mixed[] $parentConfig
128
     *
129
     * @return ColumnInterface
130
     */
131 2
    protected function buildColumn(array $config, array $parentConfig)
132
    {
133 2
        return $this->columnBuilder->build($this->prepareConfig($config, $parentConfig));
134
    }
135
136
    /**
137
     * @param mixed[] $config
138
     *
139
     * @return FilterInterface[]
140
     */
141 4 View Code Duplication
    protected function buildFilters(array $config)
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...
142
    {
143 4
        $filters = [];
144
145 4
        foreach ((isset($config['filters']) ? $config['filters'] : []) as $name => $filter) {
146 2
            $filter = $this->buildFilter(array_merge(['name' => $name], $filter), $config);
147 2
            $filters[$filter->getName()] = $filter;
148 4
        }
149
150 4
        return $filters;
151
    }
152
153
    /**
154
     * @param mixed[] $config
155
     * @param mixed[] $parentConfig
156
     *
157
     * @return FilterInterface
158
     */
159 2
    protected function buildFilter(array $config, array $parentConfig)
160
    {
161 2
        return $this->filterBuilder->build($this->prepareConfig($config, $parentConfig));
162
    }
163
164
    /**
165
     * @param mixed[] $config
166
     *
167
     * @return SortInterface[]
168
     */
169 4 View Code Duplication
    protected function buildSorts(array $config)
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...
170
    {
171 4
        $sorts = [];
172
173 4
        foreach ((isset($config['sorts']) ? $config['sorts'] : []) as $name => $sort) {
174 2
            $sort = $this->buildSort(array_merge(['name' => $name], $sort), $config);
175 2
            $sorts[$sort->getName()] = $sort;
176 4
        }
177
178 4
        return $sorts;
179
    }
180
181
    /**
182
     * @param mixed[] $config
183
     * @param mixed[] $parentConfig
184
     *
185
     * @return SortInterface
186
     */
187 2
    protected function buildSort(array $config, array $parentConfig)
188
    {
189 2
        return $this->sortBuilder->build($this->prepareConfig($config, $parentConfig));
190
    }
191
192
    /**
193
     * @param mixed[] $config
194
     *
195
     * @return ActionInterface[]
196
     */
197 4 View Code Duplication
    protected function buildGlobalActions(array $config)
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...
198
    {
199 4
        $globalActions = [];
200
201 4
        foreach ((isset($config['global_actions']) ? $config['global_actions'] : []) as $name => $action) {
202 2
            $action = $this->buildGlobalAction(array_merge(['name' => $name], $action), $config);
203 2
            $globalActions[$action->getName()] = $action;
204 4
        }
205
206 4
        return $globalActions;
207
    }
208
209
    /**
210
     * @param mixed[] $config
211
     * @param mixed[] $parentConfig
212
     *
213
     * @return ActionInterface
214
     */
215 2
    protected function buildGlobalAction(array $config, array $parentConfig)
216
    {
217 2
        return $this->actionBuilder->build($this->prepareConfig($config, $parentConfig));
218
    }
219
220
    /**
221
     * @param mixed[] $config
222
     *
223
     * @return ActionInterface[]
224
     */
225 4 View Code Duplication
    protected function buildColumnActions(array $config)
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...
226
    {
227 4
        $columnActions = [];
228
229 4
        foreach ((isset($config['column_actions']) ? $config['column_actions'] : []) as $name => $action) {
230 2
            $action = $this->buildColumnAction(array_merge(['name' => $name], $action), $config);
231 2
            $columnActions[$action->getName()] = $action;
232 4
        }
233
234 4
        return $columnActions;
235
    }
236
237
    /**
238
     * @param mixed[] $config
239
     * @param mixed[] $parentConfig
240
     *
241
     * @return ActionInterface
242
     */
243 2
    protected function buildColumnAction(array $config, array $parentConfig)
244
    {
245 2
        return $this->actionBuilder->build($this->prepareConfig($config, $parentConfig));
246
    }
247
248
    /**
249
     * @param mixed[] $config
250
     *
251
     * @return BatchInterface[]
252
     */
253 4 View Code Duplication
    protected function buildBatches(array $config)
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...
254
    {
255 4
        $batches = [];
256
257 4
        foreach ((isset($config['batches']) ? $config['batches'] : []) as $name => $batch) {
258 2
            $batch = $this->buildBatch(array_merge(['name' => $name], $batch), $config);
259 2
            $batches[$batch->getName()] = $batch;
260 4
        }
261
262 4
        return $batches;
263
    }
264
265
    /**
266
     * @param mixed[] $config
267
     * @param mixed[] $parentConfig
268
     *
269
     * @return BatchInterface
270
     */
271 2
    protected function buildBatch(array $config, array $parentConfig)
272
    {
273 2
        return $this->batchBuilder->build($this->prepareConfig($config, $parentConfig));
274
    }
275
276
    /**
277
     * @param mixed[] $config
278
     *
279
     * @return mixed[]
280
     */
281 4
    protected function buildData(array $config)
282
    {
283 4
        return isset($config['data']) ? $config['data'] : [];
284
    }
285
286
    /**
287
     * @param mixed[] $config
288
     *
289
     * @return mixed[]
290
     */
291 4
    protected function buildOptions(array $config)
292
    {
293 4
        return isset($config['options']) ? $config['options'] : [];
294
    }
295
296
    /**
297
     * @param mixed[] $config
298
     * @param mixed[] $parentConfig
299
     *
300
     * @return mixed[]
301
     */
302 5
    protected function prepareConfig(array $config, array $parentConfig = [])
303
    {
304 5
        return $config;
305
    }
306
}
307