Completed
Pull Request — dev (#9)
by Arnaud
03:07
created

ApplicationConfiguration::configureOptions()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 129
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 66
CRAP Score 6.0125

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 129
ccs 66
cts 71
cp 0.9296
rs 8.1463
cc 6
eloc 68
nc 1
nop 1
crap 6.0125

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