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

MenuItemConfiguration::configureOptions()   C

Complexity

Conditions 9
Paths 1

Size

Total Lines 73
Code Lines 33

Duplication

Lines 12
Ratio 16.44 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 12
loc 73
ccs 0
cts 48
cp 0
rs 5.9846
cc 9
eloc 33
nc 1
nop 1
crap 90

How to fix   Long Method   

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
29
                    throw new InvalidOptionsException(
30
                        'You should either defined an admin name, or route name or an url'
31
                    );
32
                }
33
34
                return $adminName;
35
            })
36
        ;
37
38
        // if an admin name is set, an action name can provided. This action will be the menu link
39
        $resolver
40
            ->setDefault('action', null)
41
            ->setNormalizer('action', function(Options $options, $action) {
42
43
                // if an action name is provided, an admin name should be defined too
44
                if ($action !== null && $options->offsetGet('admin') === null) {
45
                    throw new InvalidOptionsException(
46
                        'You should provide an admin name for this action ' . $action
47
                    );
48
                }
49
50
                // default to list action
51
                if ($options->offsetGet('admin') !== null && $action === null) {
52
                    $action = 'list';
53
                }
54
55
                return $action;
56
            })
57
        ;
58
59
        // a route can also be provided
60
        $resolver
61
            ->setDefault('route', null)
62
            ->setDefault('parameters', [])
63
            ->setAllowedTypes('parameters', 'array')
64
        ;
65
66
        // menu item displayed text
67
        $resolver
68
            ->setDefault('text', '')
69
        ;
70
71
        // menu item html attributes
72
        $resolver
73
            ->setDefault('attr', [])
74 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...
75
76
                if (!is_array($attr)) {
77
                    $attr = [];
78
                }
79
80
                if (empty($attr['id'])) {
81
                    $attr['id'] = uniqid('admin-menu-');
82
                }
83
84
                return $attr;
85
            })
86
        ;
87
88
        $resolver->setDefault('icon', null);
89
    }
90
}
91