Completed
Pull Request — master (#90)
by Arnaud
07:39 queued 05:31
created

ActionCollectionConfiguration::normalizeClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
1
<?php
2
3
namespace LAG\AdminBundle\Field\Configuration;
4
5
use JK\Sam\Configuration\Configuration;
6
use LogicException;
7
use Symfony\Component\OptionsResolver\Options;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
class ActionCollectionConfiguration extends Configuration
11
{
12
    public function configureOptions(OptionsResolver $resolver)
13
    {
14
        $resolver
15
            ->setRequired('actions')
16
            ->setAllowedTypes('actions', 'array')
17
            ->setNormalizer('actions', function (Options $options, $actions) {
18
    
19
                $normalizedActions = [];
20
                
21
                foreach ($actions as $action => $options) {
22
                    $this->normalizeIcon($options, $action);
23
                    $this->normalizeClass($options, $action);
24
                    $this->normalizeText($options, $action);
25
                    
26
                    $normalizedActions[$action] = $options;
27
                }
28
    
29
                return $normalizedActions;
30
            })
31
        ;
32
    }
33
    
34
    protected function normalizeIcon(array &$options, $action)
35
    {
36
        $iconMapping = [
37
            'delete' => 'fa fa-times',
38
            'edit' => 'fa fa-pencil',
39
        ];
40
    
41
        if (!array_key_exists('icon', $options)) {
42
            $options['icon'] = '';
43
        
44
            if (array_key_exists($action, $iconMapping)) {
45
                $options['icon'] = $iconMapping[$action];
46
            }
47
        }
48
    }
49
    
50
    protected function normalizeClass(array &$options, $action)
51
    {
52
        $classMapping = [
53
            'delete' => 'btn btn-danger',
54
            'edit' => 'btn btn-default',
55
        ];
56
    
57
        if (!array_key_exists('class', $options)) {
58
            $options['class'] = '';
59
        
60
            if (array_key_exists($action, $classMapping)) {
61
                $options['class'] = $classMapping[$action];
62
            }
63
        }
64
        $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...
65
    }
66
    
67
    protected function normalizeRoute(array &$options, $action)
68
    {
69
        if (!array_key_exists('route', $options)) {
70
            throw new LogicException(
71
                'You should provide a route for the Action "'.$action.'" in ActionCollection Field'
72
            );
73
        }
74
    
75
        if (array_key_exists('parameters', $options)) {
76
    
77
            if (!is_array($options['parameters'])) {
78
                throw new LogicException(
79
                    'You should provide an array of parameters for route configuration in action "'.$action.'"'
80
                );
81
            }
82
        }
83
    }
84
    
85
    protected function normalizeText(array &$options, $action)
86
    {
87
        if (!array_key_exists('text', $options)) {
88
            $options['text'] = ucfirst($action);
89
        }
90
    }
91
}
92