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

MenuItemConfiguration   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 14.81 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 0
cbo 4
dl 12
loc 81
ccs 0
cts 48
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C configureOptions() 12 73 9

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 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