Completed
Push — master ( d8083a...61164d )
by Arnaud
13s queued 11s
created

AdminConfiguration::configureOptions()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 70
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 50
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 70
ccs 0
cts 40
cp 0
crap 56
rs 8.1575

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\Configuration;
4
5
use JK\Configuration\Configuration;
6
use LAG\AdminBundle\Bridge\Doctrine\ORM\DataProvider\ORMDataProvider;
7
use LAG\AdminBundle\Configuration\Behavior\TranslationConfigurationTrait;
8
use LAG\AdminBundle\Controller\AdminAction;
9
use Symfony\Component\OptionsResolver\Options;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
12
/**
13
 * Ease Admin configuration manipulation.
14
 */
15
class AdminConfiguration extends Configuration
16
{
17
    use TranslationConfigurationTrait;
18
19
    /**
20
     * @var ApplicationConfiguration
21
     */
22
    protected $applicationConfiguration;
23
24
    /**
25
     * AdminConfiguration constructor.
26
     */
27 2
    public function __construct(ApplicationConfiguration $applicationConfiguration)
28
    {
29 2
        parent::__construct();
30
31 2
        $this->applicationConfiguration = $applicationConfiguration;
32 2
    }
33
34
    public function configureOptions(OptionsResolver $resolver)
35
    {
36
        $resolver
37
            ->setDefaults([
38
                'actions' => [
39
                    'list' => [],
40
                    'create' => [],
41
                    'edit' => [],
42
                    'delete' => [],
43
                ],
44
                'batch' => true,
45
                'class' => $this->applicationConfiguration->getParameter('admin_class'),
46
                'routing_url_pattern' => $this->applicationConfiguration->getParameter('routing_url_pattern'),
47
                'routing_name_pattern' => $this->applicationConfiguration->getParameter('routing_name_pattern'),
48
                'controller' => AdminAction::class,
49
                'max_per_page' => $this->applicationConfiguration->getParameter('max_per_page'),
50
                'form' => null,
51
                'form_options' => [],
52
                'pager' => $this->applicationConfiguration->getParameter('pager'),
53
                'permissions' => $this->applicationConfiguration->getParameter('permissions'),
54
                'string_length' => $this->applicationConfiguration->getParameter('string_length'),
55
                'string_length_truncate' => $this->applicationConfiguration->getParameter('string_length_truncate'),
56
                'date_format' => $this->applicationConfiguration->getParameter('date_format'),
57
                'data_provider' => ORMDataProvider::class,
58
                'page_parameter' => $this->applicationConfiguration->getParameter('page_parameter'),
59
                'list_template' => $this->applicationConfiguration->get('list_template'),
60
                'edit_template' => $this->applicationConfiguration->get('edit_template'),
61
                'create_template' => $this->applicationConfiguration->get('create_template'),
62
                'delete_template' => $this->applicationConfiguration->get('delete_template'),
63
            ])
64
            ->setRequired([
65
                'entity',
66
            ])
67
            ->setAllowedTypes('string_length', 'integer')
68
            ->setAllowedTypes('string_length_truncate', 'string')
69
            ->setAllowedTypes('page_parameter', 'string')
70
            ->setAllowedValues('pager', [
71
                null,
72
                'pagerfanta',
73
            ])
74
            ->setNormalizer('actions', function (Options $options, $actions) {
75
                $normalizedActions = [];
76
                $addBatchAction = false;
77
78
                foreach ($actions as $name => $action) {
79
                    // action configuration is an array by default
80
                    if (null === $action) {
81
                        $action = [];
82
                    }
83
                    $normalizedActions[$name] = $action;
84
85
                    // in list action, if no batch was configured or disabled, we add a batch action
86
                    if ('list' == $name && (!array_key_exists('batch', $action) || null === $action['batch'])) {
87
                        $addBatchAction = true;
88
                    }
89
                }
90
91
                // add empty default batch action
92
                if ($addBatchAction) {
93
                    $normalizedActions['batch'] = [];
94
                }
95
96
                return $normalizedActions;
97
            })
98
        ;
99
100
        $this->configureTranslation(
101
            $resolver,
102
            $this->applicationConfiguration->getTranslationPattern(),
103
            $this->applicationConfiguration->getTranslationCatalog()
104
        );
105
    }
106
}
107