Completed
Pull Request — dev (#19)
by Arnaud
02:50
created

MenuItemConfiguration::configureOptions()   C

Complexity

Conditions 12
Paths 1

Size

Total Lines 99
Code Lines 48

Duplication

Lines 30
Ratio 30.3 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 30
loc 99
ccs 0
cts 67
cp 0
rs 5.034
cc 12
eloc 48
nc 1
nop 1
crap 156

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
namespace LAG\AdminBundle\Menu\Configuration;
4
5
use LAG\AdminBundle\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
40
        // if an admin name is set, an action name can provided. This action will be the menu link
41
        $resolver
42
            ->setDefault('action', null)
43
            ->setNormalizer('action', function(Options $options, $action) {
44
45
                // if an action name is provided, an admin name should be defined too
46
                if ($action !== null && $options->offsetGet('admin') === null) {
47
                    throw new InvalidOptionsException(
48
                        'You should provide an admin name for this action ' . $action
49
                    );
50
                }
51
52
                // default to list action
53
                if ($options->offsetGet('admin') !== null && $action === null) {
54
                    $action = 'list';
55
                }
56
57
                return $action;
58
            })
59
        ;
60
61
        // a route can also be provided
62
        $resolver
63
            ->setDefault('route', null)
64
            ->setDefault('url', null)
65
            ->setDefault('parameters', [])
66
            ->setAllowedTypes('parameters', 'array')
67
        ;
68
69
        // menu item displayed text
70
        $resolver
71
            ->setDefault('text', '')
72
        ;
73
74
        // menu item html attributes
75
        $resolver
76
            ->setDefault('attr', [])
77 View Code Duplication
            ->setNormalizer('attr', function(Options $options, $attr) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
79
                if (!is_array($attr)) {
80
                    $attr = [];
81
                }
82
83
                if (empty($attr['id'])) {
84
                    $attr['id'] = uniqid('admin-menu-');
85
                }
86
87
                return $attr;
88
            })
89
        ;
90
91
        // menu sub item
92
        $resolver
93
            ->setDefault('items', [])
94 View Code Duplication
            ->setNormalizer('items', function(Options $options, $items) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
96
                if (!is_array($items)) {
97
                    $items = [];
98
                }
99
                $resolver = new OptionsResolver();
100
                $resolvedItems = [];
101
102
                foreach ($items as $name => $item) {
103
                    $itemConfiguration = new MenuItemConfiguration();
104
                    $itemConfiguration->configureOptions($resolver);
105
                    $itemConfiguration->setParameters($resolver->resolve($item));
106
107
                    $resolvedItems[$name] = $itemConfiguration;
108
                }
109
110
                return $resolvedItems;
111
            })
112
        ;
113
114
        $resolver->setDefault('icon', null);
115
    }
116
}
117