Completed
Push — master ( cec9f3...d27170 )
by Kamil
22:53
created

ArrayToDefinitionConverter::convert()   D

Complexity

Conditions 12
Paths 324

Size

Total Lines 55
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 55
rs 4.9336
cc 12
eloc 29
nc 324
nop 2

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 Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Component\Grid\Definition;
13
14
/**
15
 * @author Paweł Jędrzejewski <[email protected]>
16
 */
17
class ArrayToDefinitionConverter implements ArrayToDefinitionConverterInterface
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function convert($code, array $configuration)
23
    {
24
        $grid = Grid::fromCodeAndDriverConfiguration($code, $configuration['driver']['name'], $configuration['driver']['options']);
25
26
        if (array_key_exists('sorting', $configuration)) {
27
            $grid->setSorting($configuration['sorting']);
28
        }
29
30
        foreach ($configuration['fields'] as $name => $fieldConfiguration) {
31
            $field = Field::fromNameAndType($name, $fieldConfiguration['type']);
32
33
            if (array_key_exists('path', $fieldConfiguration)) {
34
                $field->setPath($fieldConfiguration['path']);
35
            }
36
            if (array_key_exists('label', $fieldConfiguration)) {
37
                $field->setLabel($fieldConfiguration['label']);
38
            }
39
            if (array_key_exists('options', $fieldConfiguration)) {
40
                $field->setOptions($fieldConfiguration['options']);
41
            }
42
43
            $grid->addField($field);
44
        }
45
46
        foreach ($configuration['filters'] as $name => $filterConfiguration) {
47
            $filter = Filter::fromNameAndType($name, $filterConfiguration['type']);
48
49
            if (array_key_exists('label', $filterConfiguration)) {
50
                $filter->setLabel($filterConfiguration['label']);
51
            }
52
53
            $grid->addFilter($filter);
54
        }
55
56
        foreach ($configuration['actions'] as $groupName => $actions) {
57
            $actionGroup = ActionGroup::named($groupName);
58
59
            foreach ($actions as $name => $actionConfiguration) {
60
                $action = Action::fromNameAndType($name, $actionConfiguration['type']);
61
62
                if (array_key_exists('label', $actionConfiguration)) {
63
                    $action->setLabel($actionConfiguration['label']);
64
                }
65
                if (array_key_exists('options', $actionConfiguration)) {
66
                    $action->setOptions($actionConfiguration['options']);
67
                }
68
69
                $actionGroup->addAction($action);
70
            }
71
72
            $grid->addActionGroup($actionGroup);
73
        }
74
75
        return $grid;
76
    }
77
}
78