Completed
Pull Request — master (#90)
by Arnaud
03:18
created

ActionCollection   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 6
dl 0
loc 162
ccs 0
cts 101
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 21 2
A configureOptions() 0 21 2
A getType() 0 4 1
A setEntity() 0 4 1
A setTwig() 0 4 1
A normalizeIcon() 0 15 3
A normalizeClass() 0 16 3
B normalizeRoute() 0 24 5
A normalizeText() 0 6 2
1
<?php
2
3
namespace LAG\AdminBundle\Field\Field;
4
5
use LAG\AdminBundle\Field\AbstractField;
6
use LAG\AdminBundle\Field\EntityAwareInterface;
7
use LAG\AdminBundle\Field\TwigAwareInterface;
8
use LogicException;
9
use Symfony\Component\OptionsResolver\Options;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
use Symfony\Component\PropertyAccess\PropertyAccess;
12
use Twig_Environment;
13
14
class ActionCollection extends AbstractField implements TwigAwareInterface, EntityAwareInterface
15
{
16
    /**
17
     * @var Twig_Environment
18
     */
19
    protected $twig;
20
    
21
    /**
22
     * @var mixed
23
     */
24
    protected $entity;
25
    
26
    /**
27
     * Render value of the field.
28
     *
29
     * @param mixed $value Value to render
30
     *
31
     * @return mixed
32
     */
33
    public function render($value)
34
    {
35
        $actions = $this
36
            ->options
37
            ->get('actions')
38
        ;
39
        $normalizedActions = [];
40
        
41
        foreach ($actions as $action => $options) {
42
            $this->normalizeRoute($options, $action);
43
            $normalizedActions[$action] = $options;
44
        }
45
        
46
        
47
        return $this
48
            ->twig
49
            ->render('@LAGAdmin/Field/actionCollection.html.twig', [
50
                'actions' => $normalizedActions,
51
            ])
52
        ;
53
    }
54
    
55
    public function configureOptions(OptionsResolver $resolver)
56
    {
57
        $resolver
58
            ->setRequired('actions')
59
            ->setAllowedTypes('actions', 'array')
60
            ->setNormalizer('actions', function (Options $options, $actions) {
61
    
62
                $normalizedActions = [];
63
                
64
                foreach ($actions as $action => $options) {
65
                    $this->normalizeIcon($options, $action);
66
                    $this->normalizeClass($options, $action);
67
                    $this->normalizeText($options, $action);
68
                    
69
                    $normalizedActions[$action] = $options;
70
                }
71
    
72
                return $normalizedActions;
73
            })
74
        ;
75
    }
76
    
77
    /**
78
     * Return field type.
79
     *
80
     * @return string
81
     */
82
    public function getType()
83
    {
84
        return AbstractField::TYPE_ACTION_COLLECTION;
85
    }
86
    
87
    /**
88
     * Defines entity for field.
89
     *
90
     * @param $entity
91
     *
92
     * @return void
93
     */
94
    public function setEntity($entity)
95
    {
96
        $this->entity = $entity;
97
    }
98
    
99
    /**
100
     * Define twig environment.
101
     *
102
     * @param Twig_Environment $twig
103
     *
104
     * @return void
105
     */
106
    public function setTwig(Twig_Environment $twig)
107
    {
108
        $this->twig = $twig;
109
    }
110
    
111
    protected function normalizeIcon(array &$options, $action)
112
    {
113
        $iconMapping = [
114
            'delete' => 'fa fa-times',
115
            'edit' => 'fa fa-pencil',
116
        ];
117
    
118
        if (!array_key_exists('icon', $options)) {
119
            $options['icon'] = '';
120
        
121
            if (array_key_exists($action, $iconMapping)) {
122
                $options['icon'] = $iconMapping[$action];
123
            }
124
        }
125
    }
126
    
127
    protected function normalizeClass(array &$options, $action)
128
    {
129
        $classMapping = [
130
            'delete' => 'btn btn-danger',
131
            'edit' => 'btn btn-default',
132
        ];
133
    
134
        if (!array_key_exists('class', $options)) {
135
            $options['class'] = '';
136
        
137
            if (array_key_exists($action, $classMapping)) {
138
                $options['class'] = $classMapping[$action];
139
            }
140
        }
141
        $normalizedActions[$action] = $options;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$normalizedActions was never initialized. Although not strictly required by PHP, it is generally a good practice to add $normalizedActions = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
142
    }
143
    
144
    protected function normalizeRoute(array &$options, $action)
145
    {
146
        if (!array_key_exists('route', $options)) {
147
            throw new LogicException(
148
                'You should provide a route for the Action "'.$action.'" in ActionCollection Field'
149
            );
150
        }
151
    
152
        if (array_key_exists('parameters', $options)) {
153
    
154
            if (!is_array($options['parameters'])) {
155
                throw new LogicException(
156
                    'You should provide an array of parameters for route configuration in action "'.$action.'"'
157
                );
158
            }
159
            $accessor = PropertyAccess::createPropertyAccessor();
160
            $normalizedParameters = [];
161
            
162
            foreach ($options['parameters'] as $parameter => $parameterOptions) {
163
                $normalizedParameters[$parameter] = $accessor->getValue($this->entity, $parameter);
164
            }
165
            $options['parameters'] = $normalizedParameters;
166
        }
167
    }
168
    
169
    protected function normalizeText(array &$options, $action)
170
    {
171
        if (!array_key_exists('text', $options)) {
172
            $options['text'] = ucfirst($action);
173
        }
174
    }
175
}
176