Completed
Pull Request — master (#90)
by Arnaud
04:05
created

MenuItemConfiguration   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 29.7 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 4
dl 30
loc 101
ccs 0
cts 61
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C configureOptions() 30 93 12

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace LAG\AdminBundle\Menu\Configuration;
4
5
use JK\Configuration\Configuration;
6
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
7
use Symfony\Component\OptionsResolver\Options;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
class MenuItemConfiguration extends Configuration
11
{
12
    /**
13
     * Define allowed parameters and values for this configuration, using optionsResolver component.
14
     *
15
     * @param OptionsResolver $resolver
16
     */
17
    public function configureOptions(OptionsResolver $resolver)
18
    {
19
        // user can defined an admin name
20
        $resolver
21
            ->setDefault('admin', null)
22
            ->setNormalizer('admin', function(Options $options, $adminName) {
23
24
                // user has to defined either an admin name and an action name, or a route name with optional
25
                // parameters, or an url
26
                if ($adminName === null
27
                    && $options->offsetGet('route') === null
28
                    && $options->offsetGet('url') === null
29
                ) {
30
31
                    throw new InvalidOptionsException(
32
                        'You should either defined an admin name, or route name or an uri'
33
                    );
34
                }
35
36
                return $adminName;
37
            });
38
39
        // if an admin name is set, an action name can provided. This action will be the menu link
40
        $resolver
41
            ->setDefault('action', null)
42
            ->setNormalizer('action', function(Options $options, $action) {
43
44
                // if an action name is provided, an admin name should be defined too
45
                if ($action !== null && $options->offsetGet('admin') === null) {
46
                    throw new InvalidOptionsException(
47
                        'You should provide an admin name for this action '.$action
48
                    );
49
                }
50
51
                // default to list action
52
                if ($options->offsetGet('admin') !== null && $action === null) {
53
                    $action = 'list';
54
                }
55
56
                return $action;
57
            });
58
59
        // a route can also be provided
60
        $resolver
61
            ->setDefault('route', null)
62
            ->setDefault('url', null)
63
            ->setDefault('parameters', [])
64
            ->setAllowedTypes('parameters', 'array');
65
66
        // menu item displayed text
67
        $resolver
68
            ->setDefault('text', '');
69
70
        // menu item html attributes
71
        $resolver
72
            ->setDefault('attr', [])
73 View Code Duplication
            ->setNormalizer('attr', function(Options $options, $attr) {
74
75
                if (!is_array($attr)) {
76
                    $attr = [];
77
                }
78
79
                if (empty($attr['id'])) {
80
                    $attr['id'] = uniqid('admin-menu-');
81
                }
82
83
                return $attr;
84
            });
85
86
        // menu sub item
87
        $resolver
88
            ->setDefault('items', [])
89 View Code Duplication
            ->setNormalizer('items', function(Options $options, $items) {
90
91
                if (!is_array($items)) {
92
                    $items = [];
93
                }
94
                $resolver = new OptionsResolver();
95
                $resolvedItems = [];
96
97
                foreach ($items as $name => $item) {
98
                    $itemConfiguration = new MenuItemConfiguration();
99
                    $itemConfiguration->configureOptions($resolver);
100
                    $itemConfiguration->setParameters($resolver->resolve($item));
101
102
                    $resolvedItems[$name] = $itemConfiguration;
103
                }
104
105
                return $resolvedItems;
106
            });
107
108
        $resolver->setDefault('icon', null);
109
    }
110
}
111