Completed
Branch master (01610a)
by Arnaud
06:29
created

ApplicationConfiguration::configureOptions()   C

Complexity

Conditions 8
Paths 1

Size

Total Lines 133
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 67
CRAP Score 8.0541

Importance

Changes 5
Bugs 3 Features 2
Metric Value
c 5
b 3
f 2
dl 0
loc 133
ccs 67
cts 74
cp 0.9054
rs 5.2676
cc 8
eloc 70
nc 1
nop 1
crap 8.0541

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 27
    public function configureOptions(OptionsResolver $resolver)
24
    {
25
        // enable or disable extra configuration listener
26 27
        $resolver->setDefault('enable_extra_configuration', true);
27 27
        $resolver->setAllowedTypes('enable_extra_configuration', 'boolean');
28
29
        // application title
30 27
        $resolver->setDefault('title', 'AdminBundle application');
31 27
        $resolver->setAllowedTypes('title', 'string');
32
33
        // application description
34 27
        $resolver->setDefault('description', '');
35 27
        $resolver->setAllowedTypes('description', 'string');
36
37
        // application locale (en by default)
38 27
        $resolver->setDefault('locale', 'en');
39 27
        $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 27
        $resolver->setDefault('base_template', 'LAGAdminBundle::admin.layout.html.twig');
45 27
        $resolver->setAllowedTypes('base_template', 'string');
46
47
        // form block template
48 27
        $resolver->setDefault('block_template', 'LAGAdminBundle:Form:fields.html.twig');
49 27
        $resolver->setAllowedTypes('block_template', 'string');
50
51
        // use bootstrap or not
52 27
        $resolver->setDefault('bootstrap', true);
53 27
        $resolver->setAllowedTypes('bootstrap', 'boolean');
54
55
        // general date format
56 27
        $resolver->setDefault('date_format', 'Y/m/d');
57 27
        $resolver->setAllowedTypes('date_format', 'string');
58
59
        // string length before truncation (0 means no truncation)
60 27
        $resolver->setDefault('string_length', 0);
61 27
        $resolver->setAllowedTypes('string_length', 'integer');
62
63 27
        $resolver->setDefault('string_length_truncate', '...');
64 27
        $resolver->setAllowedTypes('string_length_truncate', 'string');
65
66
        // routing configuration (route name pattern and url name pattern)
67 27
        $resolver->setDefault('routing', [
68 27
            'url_pattern' => '/{admin}/{action}',
69 27
            'name_pattern' => 'lag.admin.{admin}',
70 27
        ]);
71 27
        $resolver->setAllowedTypes('routing', 'array');
72
        $resolver->setNormalizer('routing', function(Options $options, $value) {
73
74
            // url pattern should contain {admin} and {action} token
75 27
            $urlPattern = $value['url_pattern'];
76
77 27
            if (strstr($urlPattern, '{admin}') === false) {
78
                throw new InvalidOptionsException('Admin routing configuration url pattern should contains {admin} placeholder');
79
            }
80 27
            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 27
            $namePattern = $value['name_pattern'];
86
87 27
            if (strstr($namePattern, '{admin}') === false) {
88
                throw new InvalidOptionsException('Admin routing configuration pattern name should contains the {admin} placeholder');
89
            }
90
91 27
            return $value;
92 27
        });
93
94
        // translation configuration
95 27
        $resolver->setDefault('translation', [
96 27
            'enabled' => true,
97
            'pattern' => 'lag.admin.{key}'
98 27
        ]);
99 27
        $resolver->setAllowedTypes('translation', 'array');
100
        $resolver->setNormalizer('translation', function(Options $options, $value) {
101
102 27
            if (!is_bool($value['enabled'])) {
103
                throw new InvalidOptionsException('Admin translation enabled parameter should be a boolean');
104
            }
105
106 27
            if (!array_key_exists('pattern', $value)) {
107
                $value['pattern'] = '{admin}.{key}';
108
            }
109
110 27
            if ($value['enabled'] && strstr($value['pattern'], '{key}') === false) {
111
                throw new InvalidOptionsException('Admin translation pattern should contains the {key} placeholder');
112
            }
113
114 27
            return $value;
115 27
        });
116
117
        // maximum number of elements displayed
118 27
        $resolver->setDefault('max_per_page', 25);
119 27
        $resolver->setAllowedTypes('max_per_page', 'integer');
120
121
        // admin field type mapping
122
        $defaultMapping = [
123 27
            Field::TYPE_STRING => StringField::class,
124 27
            Field::TYPE_ARRAY => Field\ArrayField::class,
125 27
            Field::TYPE_LINK => Field\Link::class,
126 27
            Field::TYPE_DATE => Field\Date::class,
127 27
            Field::TYPE_COUNT => Field\Count::class,
128 27
            Field::TYPE_ACTION => Field\Action::class,
129 27
            Field::TYPE_COLLECTION => Field\Collection::class,
130 27
            Field::TYPE_BOOLEAN => Field\Boolean::class,
131 27
        ];
132
133 27
        $resolver->setDefault('fields_mapping', $defaultMapping);
134 27
        $resolver->setAllowedTypes('fields_mapping', 'array');
135
        $resolver->setNormalizer('fields_mapping', function(Options $options, $value) use ($defaultMapping) {
136
            // merge with default mapping to allow override
137 27
            $value = array_merge($defaultMapping, $value);
138
139 27
            return $value;
140 27
        });
141
142
        // fields templates mapping
143
        $defaultMapping = [
144 27
            Field::TYPE_LINK => 'LAGAdminBundle:Render:link.html.twig',
145 27
        ];
146
147 27
        $resolver->setDefault('fields_template_mapping', $defaultMapping);
148 27
        $resolver->setAllowedTypes('fields_template_mapping', 'array');
149 27
        $resolver->setNormalizer('fields_template_mapping', function(Options $options, $value) use ($defaultMapping) {
150
            // merge with default mapping to allow override
151 27
            $value = array_merge($defaultMapping, $value);
152
153 27
            return $value;
154 27
        });
155 27
    }
156
}
157