Completed
Pull Request — dev (#9)
by Arnaud
02:57
created

ApplicationConfiguration::configureOptions()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 143
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 74
CRAP Score 6.0091

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 143
ccs 74
cts 79
cp 0.9367
rs 8.1463
cc 6
eloc 78
nc 1
nop 1
crap 6.0091

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\Application\Configuration;
4
5
use LAG\AdminBundle\Configuration\Configuration;
6
use LAG\AdminBundle\Configuration\ConfigurationInterface;
7
use LAG\AdminBundle\Field\Field;
8
use LAG\AdminBundle\Field\Field\StringField;
9
use Symfony\Component\HttpKernel\KernelInterface;
10
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
11
use Symfony\Component\OptionsResolver\Options;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
14
/**
15
 * Application configuration class. Allow easy configuration manipulation within an Admin.
16
 */
17
class ApplicationConfiguration extends Configuration implements ConfigurationInterface
18
{
19
    /**
20
     * @var KernelInterface
21
     */
22
    protected $kernel;
23
24
    /**
25
     * ApplicationConfiguration constructor.
26
     *
27
     * @param KernelInterface $kernel
28
     */
29 21
    public function __construct(KernelInterface $kernel)
30
    {
31
        // call parent constructor
32 21
        parent::__construct();
33
34
        // kernel is required to validate resources location
35 21
        $this->kernel = $kernel;
36 21
    }
37
38
    /**
39
     * Configure configuration allowed parameters.
40
     *
41
     * @param OptionsResolver $resolver
42
     */
43 20
    public function configureOptions(OptionsResolver $resolver)
44
    {
45
        // enable or disable extra configuration listener
46 20
        $resolver->setDefault('enable_extra_configuration', true);
47 20
        $resolver->setAllowedTypes('enable_extra_configuration', 'boolean');
48
49
        // application title
50 20
        $resolver->setDefault('title', 'AdminBundle application');
51 20
        $resolver->setAllowedTypes('title', 'string');
52
53
        // application description
54 20
        $resolver->setDefault('description', '');
55 20
        $resolver->setAllowedTypes('description', 'string');
56
57
        // application locale (en by default)
58 20
        $resolver->setDefault('locale', 'en');
59 20
        $resolver->setAllowedTypes('locale', 'string');
60
61
        // main base template
62 20
        $resolver->setDefault('base_template', 'LAGAdminBundle::admin.layout.html.twig');
63 20
        $resolver->setAllowedTypes('base_template', 'string');
64
        $resolver->setNormalizer('base_template', function (Options $options, $value) {
65
            // resource must exists
66 20
             $this
67
                ->kernel
68 20
                ->locateResource($value);
69
70 20
            return $value;
71 20
        });
72
73
        // form block template
74 20
        $resolver->setDefault('block_template', 'LAGAdminBundle:Form:fields.html.twig');
75 20
        $resolver->setAllowedTypes('block_template', 'string');
76
        $resolver->setNormalizer('block_template', function (Options $options, $value) {
77
            // resource must exists
78 20
            $this
79
                ->kernel
80 20
                ->locateResource($value);
81
82 20
            return $value;
83 20
        });
84
85
        // use bootstrap or not
86 20
        $resolver->setDefault('bootstrap', true);
87 20
        $resolver->setAllowedTypes('bootstrap', 'boolean');
88
89
        // general date format
90 20
        $resolver->setDefault('date_format', 'Y/m/d');
91 20
        $resolver->setAllowedTypes('date_format', 'string');
92
93
        // string length before truncation (0 means no truncation)
94 20
        $resolver->setDefault('string_length', 0);
95 20
        $resolver->setAllowedTypes('string_length', 'integer');
96
97 20
        $resolver->setDefault('string_length_truncate', '...');
98 20
        $resolver->setAllowedTypes('string_length_truncate', 'string');
99
100
        // routing configuration (route name pattern and url name pattern)
101 20
        $resolver->setDefault('routing', [
102 20
            'url_pattern' => '/{admin}/{action}',
103 20
            'name_pattern' => 'lag.admin.{admin}',
104 20
        ]);
105 20
        $resolver->setAllowedTypes('routing', 'array');
106
        $resolver->setNormalizer('routing', function (Options $options, $value) {
107
108
            // url pattern should contain {admin} and {action} token
109 20
            $urlPattern = $value['url_pattern'];
110
111 20
            if (strstr($urlPattern, '{admin}') === false) {
112
                throw new InvalidOptionsException('Admin routing configuration url pattern should contains {admin} placeholder');
113
            }
114 20
            if (strstr($urlPattern, '{action}') === false) {
115
                throw new InvalidOptionsException('Admin routing configuration url pattern should contains the {action} placeholder');
116
            }
117
118
            // name pattern should contain {admin} token
119 20
            $namePattern = $value['name_pattern'];
120
121 20
            if (strstr($namePattern, '{admin}') === false) {
122
                throw new InvalidOptionsException('Admin routing configuration pattern name should contains the {admin} placeholder');
123
            }
124
125 20
            return $value;
126 20
        });
127
128
        // translation configuration
129 20
        $resolver->setDefault('translation', [
130 20
            'enabled' => true,
131
            'pattern' => 'lag.admin.{key}'
132 20
        ]);
133 20
        $resolver->setAllowedTypes('translation', 'array');
134
        $resolver->setNormalizer('translation', function (Options $options, $value) {
135
136 20
            if (!is_bool($value['enabled'])) {
137
                throw new InvalidOptionsException('Admin translation enabled parameter should be a boolean');
138
            }
139
140 20
            if (strstr($value['pattern'], '{key}') === false) {
141
                throw new InvalidOptionsException('Admin translation pattern should contains the {key} placeholder');
142
            }
143
144 20
            return $value;
145 20
        });
146
147
        // maximum number of elements displayed
148 20
        $resolver->setDefault('max_per_page', 25);
149 20
        $resolver->setAllowedTypes('max_per_page', 'integer');
150
151
        // admin field type mapping
152
        $defaultMapping = [
153 20
            Field::TYPE_STRING => StringField::class,
154 20
            Field::TYPE_ARRAY => Field\ArrayField::class,
155 20
            Field::TYPE_LINK => Field\Link::class,
156 20
            Field::TYPE_DATE => Field\Date::class,
157 20
            Field::TYPE_COUNT => Field\Count::class,
158 20
            Field::TYPE_ACTION => Field\Action::class,
159 20
            Field::TYPE_COLLECTION => Field\Collection::class,
160 20
            Field::TYPE_BOOLEAN => Field\Boolean::class,
161 20
        ];
162
163 20
        $resolver->setDefault('fields_mapping', $defaultMapping);
164 20
        $resolver->setAllowedTypes('fields_mapping', 'array');
165
        $resolver->setNormalizer('fields_mapping', function (Options $options, $value) use ($defaultMapping) {
166
            // merge with default mapping to allow override
167 20
            $value = array_merge($defaultMapping, $value);
168
169 20
            return $value;
170 20
        });
171
172
        // fields templates mapping
173
        $defaultMapping = [
174 20
            Field::TYPE_LINK => 'LAGAdminBundle:Render:link.html.twig',
175 20
        ];
176
177 20
        $resolver->setDefault('fields_template_mapping', $defaultMapping);
178 20
        $resolver->setAllowedTypes('fields_template_mapping', 'array');
179 20
        $resolver->setNormalizer('fields_template_mapping', function (Options $options, $value) use ($defaultMapping) {
180
            // merge with default mapping to allow override
181 20
            $value = array_merge($defaultMapping, $value);
182
183 20
            return $value;
184 20
        });
185 20
    }
186
}
187