Completed
Branch master (b7eb35)
by Arnaud
02:59
created

AdminConfiguration::configureOptions()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 69
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 7.0014

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 69
ccs 31
cts 32
cp 0.9688
rs 6.9081
cc 7
eloc 41
nc 1
nop 1
crap 7.0014

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