Completed
Push — master ( bd4cbc...df0c76 )
by Arnaud
9s
created

AdminConfiguration::configureOptions()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 68
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 7.0046

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 68
ccs 42
cts 44
cp 0.9545
rs 6.9654
cc 7
eloc 40
nc 1
nop 1
crap 7.0046

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\Admin\Configuration;
4
5
use LAG\AdminBundle\Application\Configuration\ApplicationConfiguration;
6
use LAG\AdminBundle\Configuration\Configuration;
7
use LAG\AdminBundle\Configuration\ConfigurationInterface;
8
use Symfony\Component\OptionsResolver\Options;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
11
/**
12
 * Ease Admin configuration manipulation.
13
 */
14
class AdminConfiguration extends Configuration implements ConfigurationInterface
15
{
16
    /**
17
     * @var ApplicationConfiguration
18
     */
19
    protected $applicationConfiguration;
20
21
    /**
22
     * AdminConfiguration constructor.
23
     *
24
     * @param ApplicationConfiguration $applicationConfiguration
25
     */
26 28
    public function __construct(ApplicationConfiguration $applicationConfiguration)
27
    {
28 28
        parent::__construct();
29
30 28
        $this->applicationConfiguration = $applicationConfiguration;
31 28
    }
32
33
    /**
34
     * @param OptionsResolver $resolver
35
     */
36 28
    public function configureOptions(OptionsResolver $resolver)
37
    {
38
        // inherited routing configuration from global application configuration
39 28
        $routing = $this
40
            ->applicationConfiguration
41 28
            ->getParameter('routing');
42
43
        // inherited max per page configuration
44 28
        $maxPerPage = $this
45
            ->applicationConfiguration
46 28
            ->getParameter('max_per_page');
47
48
        // optional options
49 28
        $resolver->setDefaults([
50
            'actions' => [
51 28
                'list' => [],
52 28
                'create' => [],
53 28
                'edit' => [],
54 28
                'delete' => [],
55 28
            ],
56 28
            'batch' => true,
57 28
            'routing_url_pattern' => $routing['url_pattern'],
58 28
            'routing_name_pattern' => $routing['name_pattern'],
59 28
            'controller' => 'LAGAdminBundle:CRUD',
60 28
            'max_per_page' => $maxPerPage,
61 28
            'data_provider' => null,
62 28
            'translation_pattern' => $this
63
                ->applicationConfiguration
64 28
                ->getParameter('translation')['pattern'],
65
            'form' => null
66 28
        ]);
67
        // required options
68 28
        $resolver->setRequired([
69 28
            'entity',
70 28
        ]);
71
72 28
        $resolver->setDefault('menu', [
73
            'main' => [
74
                'action' => 'list'
75 28
            ]
76 28
        ]);
77
78 28
        $resolver->setNormalizer('actions', function(Options $options, $actions) {
79 28
            $normalizedActions = [];
80 28
            $addBatchAction = false;
81
82 28
            foreach ($actions as $name => $action) {
83
84
                // action configuration is an array by default
85 27
                if ($action === null) {
86
                    $action = [];
87
                }
88 27
                $normalizedActions[$name] = $action;
89
90
                // in list action, if no batch was configured or disabled, we add a batch action
91 27
                if ($name == 'list' && (!array_key_exists('batch', $action) || $action['batch'] === null)) {
92 7
                    $addBatchAction = true;
93 7
                }
94 28
            }
95
96
            // add empty default batch action
97 28
            if ($addBatchAction) {
98 7
                $normalizedActions['batch'] = [];
99 7
            }
100
101 28
            return $normalizedActions;
102 28
        });
103 28
    }
104
}
105