Completed
Pull Request — dev (#19)
by Arnaud
14:27 queued 06:29
created

MenuItemConfiguration::configureOptions()   C

Complexity

Conditions 12
Paths 1

Size

Total Lines 93
Code Lines 48

Duplication

Lines 30
Ratio 32.26 %

Code Coverage

Tests 37
CRAP Score 12.5799

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 30
loc 93
ccs 37
cts 44
cp 0.8409
rs 5.034
cc 12
eloc 48
nc 1
nop 1
crap 12.5799

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 1
    public function configureOptions(OptionsResolver $resolver)
18
    {
19
        // user can defined an admin name
20
        $resolver
21 1
            ->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 1
                if ($adminName === null
27 1
                    && $options->offsetGet('route') === null
28 1
                    && $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 1
                return $adminName;
37 1
            });
38
39
        // if an admin name is set, an action name can provided. This action will be the menu link
40
        $resolver
41 1
            ->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 1
                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 1
                if ($options->offsetGet('admin') !== null && $action === null) {
53
                    $action = 'list';
54
                }
55
56 1
                return $action;
57 1
            });
58
59
        // a route can also be provided
60
        $resolver
61 1
            ->setDefault('route', null)
62 1
            ->setDefault('url', null)
63 1
            ->setDefault('parameters', [])
64 1
            ->setAllowedTypes('parameters', 'array');
65
66
        // menu item displayed text
67
        $resolver
68 1
            ->setDefault('text', '');
69
70
        // menu item html attributes
71
        $resolver
72 1
            ->setDefault('attr', [])
73 View Code Duplication
            ->setNormalizer('attr', function(Options $options, $attr) {
74
75 1
                if (!is_array($attr)) {
76
                    $attr = [];
77
                }
78
79 1
                if (empty($attr['id'])) {
80 1
                    $attr['id'] = uniqid('admin-menu-');
81
                }
82
83 1
                return $attr;
84 1
            });
85
86
        // menu sub item
87
        $resolver
88 1
            ->setDefault('items', [])
89 1 View Code Duplication
            ->setNormalizer('items', function(Options $options, $items) {
90
91 1
                if (!is_array($items)) {
92
                    $items = [];
93
                }
94 1
                $resolver = new OptionsResolver();
95 1
                $resolvedItems = [];
96
97 1
                foreach ($items as $name => $item) {
98 1
                    $itemConfiguration = new MenuItemConfiguration();
99 1
                    $itemConfiguration->configureOptions($resolver);
100 1
                    $itemConfiguration->setParameters($resolver->resolve($item));
101
102 1
                    $resolvedItems[$name] = $itemConfiguration;
103
                }
104
105 1
                return $resolvedItems;
106 1
            });
107
108 1
        $resolver->setDefault('icon', null);
109 1
    }
110
}
111