Completed
Push — master ( 6f962e...549eec )
by Kamil
21:17
created

ArrayToDefinitionConverter::convert()   F

Complexity

Conditions 13
Paths 540

Size

Total Lines 58
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 0
loc 58
rs 3.9655
c 3
b 0
f 1
cc 13
eloc 31
nc 540
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
            if (array_key_exists('options', $filterConfiguration)) {
53
                $filter->setOptions($filterConfiguration['options']);
54
            }
55
56
            $grid->addFilter($filter);
57
        }
58
59
        foreach ($configuration['actions'] as $groupName => $actions) {
60
            $actionGroup = ActionGroup::named($groupName);
61
62
            foreach ($actions as $name => $actionConfiguration) {
63
                $action = Action::fromNameAndType($name, $actionConfiguration['type']);
64
65
                if (array_key_exists('label', $actionConfiguration)) {
66
                    $action->setLabel($actionConfiguration['label']);
67
                }
68
                if (array_key_exists('options', $actionConfiguration)) {
69
                    $action->setOptions($actionConfiguration['options']);
70
                }
71
72
                $actionGroup->addAction($action);
73
            }
74
75
            $grid->addActionGroup($actionGroup);
76
        }
77
78
        return $grid;
79
    }
80
}
81