Completed
Push — upgrade-boilerplate ( 903b21 )
by Kamil
17:46
created

ArrayToDefinitionConverter::convertAction()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 16
nop 2
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
final 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
            $grid->addField($this->convertField($name, $fieldConfiguration));
32
        }
33
34
        foreach ($configuration['filters'] as $name => $filterConfiguration) {
35
            $grid->addFilter($this->convertFilter($name, $filterConfiguration));
36
        }
37
38
        foreach ($configuration['actions'] as $name => $actionGroupConfiguration) {
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $actionGroupConfiguration exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
39
            $grid->addActionGroup($this->convertActionGroup($name, $actionGroupConfiguration));
40
        }
41
42
        return $grid;
43
    }
44
45
    /**
46
     * @param string $name
47
     * @param array $configuration
48
     *
49
     * @return Field
50
     */
51
    private function convertField($name, array $configuration)
52
    {
53
        $field = Field::fromNameAndType($name, $configuration['type']);
54
55
        if (array_key_exists('path', $configuration)) {
56
            $field->setPath($configuration['path']);
57
        }
58
        if (array_key_exists('label', $configuration)) {
59
            $field->setLabel($configuration['label']);
60
        }
61
        if (array_key_exists('enabled', $configuration)) {
62
            $field->setEnabled($configuration['enabled']);
63
        }
64
        if (array_key_exists('sortable', $configuration)) {
65
            $field->setSortable($configuration['sortable']);
66
        }
67
        if (array_key_exists('position', $configuration)) {
68
            $field->setPosition($configuration['position']);
69
        }
70
        if (array_key_exists('options', $configuration)) {
71
            $field->setOptions($configuration['options']);
72
        }
73
74
        return $field;
75
    }
76
77
    /**
78
     * @param string $name
79
     * @param array $configuration
80
     *
81
     * @return Filter
82
     */
83
    private function convertFilter($name, array $configuration)
84
    {
85
        $filter = Filter::fromNameAndType($name, $configuration['type']);
86
87
        if (array_key_exists('label', $configuration)) {
88
            $filter->setLabel($configuration['label']);
89
        }
90
        if (array_key_exists('template', $configuration)) {
91
            $filter->setTemplate($configuration['template']);
92
        }
93
        if (array_key_exists('enabled', $configuration)) {
94
            $filter->setEnabled($configuration['enabled']);
95
        }
96
        if (array_key_exists('position', $configuration)) {
97
            $filter->setPosition($configuration['position']);
98
        }
99
        if (array_key_exists('options', $configuration)) {
100
            $filter->setOptions($configuration['options']);
101
        }
102
103
        return $filter;
104
    }
105
106
    /**
107
     * @param string $name
108
     * @param array $configuration
109
     *
110
     * @return ActionGroup
111
     */
112
    private function convertActionGroup($name, array $configuration)
113
    {
114
        $actionGroup = ActionGroup::named($name);
115
116
        foreach ($configuration as $actionName => $actionConfiguration) {
117
            $actionGroup->addAction($this->convertAction($actionName, $actionConfiguration));
118
        }
119
120
        return $actionGroup;
121
    }
122
123
    /**
124
     * @param string $name
125
     * @param array $configuration
126
     *
127
     * @return Action
128
     */
129
    private function convertAction($name, array $configuration)
130
    {
131
        $action = Action::fromNameAndType($name, $configuration['type']);
132
133
        if (array_key_exists('label', $configuration)) {
134
            $action->setLabel($configuration['label']);
135
        }
136
        if (array_key_exists('icon', $configuration)) {
137
            $action->setIcon($configuration['icon']);
138
        }
139
        if (array_key_exists('position', $configuration)) {
140
            $action->setPosition($configuration['position']);
141
        }
142
        if (array_key_exists('options', $configuration)) {
143
            $action->setOptions($configuration['options']);
144
        }
145
146
        return $action;
147
    }
148
}
149