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

ApplicationConfiguration::configureOptions()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 129
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 67
CRAP Score 6.0119

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 129
ccs 67
cts 72
cp 0.9306
rs 8.1463
cc 6
eloc 71
nc 1
nop 1
crap 6.0119

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