Completed
Pull Request — dev (#56)
by Arnaud
18:15 queued 10:16
created

ApplicationConfiguration::setFieldsOptions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 36
ccs 22
cts 22
cp 1
rs 8.8571
cc 1
eloc 22
nc 1
nop 1
crap 1
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 29
    public function configureOptions(OptionsResolver $resolver)
24
    {
25
        // enable or disable extra configuration listener
26 29
        $resolver->setDefault('enable_extra_configuration', true);
27 29
        $resolver->setAllowedTypes('enable_extra_configuration', 'boolean');
28
29
        // application title
30 29
        $resolver->setDefault('title', 'AdminBundle application');
31 29
        $resolver->setAllowedTypes('title', 'string');
32
33
        // application description
34 29
        $resolver->setDefault('description', '');
35 29
        $resolver->setAllowedTypes('description', 'string');
36
37
        // application locale (en by default)
38 29
        $resolver->setDefault('locale', 'en');
39 29
        $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 existence here.
44 29
        $resolver->setDefault('base_template', 'LAGAdminBundle::admin.layout.html.twig');
45 29
        $resolver->setAllowedTypes('base_template', 'string');
46
47
        // form block template
48 29
        $resolver->setDefault('block_template', 'LAGAdminBundle:Form:fields.html.twig');
49 29
        $resolver->setAllowedTypes('block_template', 'string');
50
51
        // use bootstrap or not
52 29
        $resolver->setDefault('bootstrap', true);
53 29
        $resolver->setAllowedTypes('bootstrap', 'boolean');
54
55
        // general date format
56 29
        $resolver->setDefault('date_format', 'Y/m/d');
57 29
        $resolver->setAllowedTypes('date_format', 'string');
58
59
        // string length before truncation (0 means no truncation)
60 29
        $resolver->setDefault('string_length', 0);
61 29
        $resolver->setAllowedTypes('string_length', 'integer');
62
63 29
        $resolver->setDefault('string_length_truncate', '...');
64 29
        $resolver->setAllowedTypes('string_length_truncate', 'string');
65
66
        // routing configuration (route name pattern and url name pattern)
67 29
        $this->setRoutingOptions($resolver);
68
69
        // translation configuration
70 29
        $this->setTranslationOptions($resolver);
71
72
        // maximum number of elements displayed
73 29
        $resolver->setDefault('max_per_page', 25);
74 29
        $resolver->setAllowedTypes('max_per_page', 'integer');
75
76
        // admin field type mapping
77 29
        $this->setFieldsOptions($resolver);
78 29
    }
79
80
    /**
81
     * @param OptionsResolver $resolver
82
     */
83 29
    protected function setRoutingOptions(OptionsResolver $resolver)
84
    {
85 29
        $resolver->setDefault('routing', [
86 29
            'url_pattern' => '/{admin}/{action}',
87
            'name_pattern' => 'lag.admin.{admin}.{action}',
88
        ]);
89 29
        $resolver->setAllowedTypes('routing', 'array');
90
        $resolver->setNormalizer('routing', function(Options $options, $value) {
91
92 29
            if (!array_key_exists('url_pattern', $value)) {
93 1
                $value['url_pattern'] = '/{admin}/{action}';
94
            }
95 29
            if (!array_key_exists('name_pattern', $value)) {
96 1
                $value['name_pattern'] = 'lag.admin.{admin}.{action}';
97
            }
98
99
            // url pattern should contain {admin} and {action} token
100 29
            $urlPattern = $value['url_pattern'];
101
102 29
            if (strstr($urlPattern, '{admin}') === false) {
103 1
                throw new InvalidOptionsException('Admin routing configuration url pattern should contains {admin} placeholder');
104
            }
105 29
            if (strstr($urlPattern, '{action}') === false) {
106 1
                throw new InvalidOptionsException('Admin routing configuration url pattern should contains the {action} placeholder');
107
            }
108
109
            // name pattern should contain {admin} token
110 29
            $namePattern = $value['name_pattern'];
111
112 29
            if (strstr($namePattern, '{admin}') === false) {
113 1
                throw new InvalidOptionsException('Admin routing configuration pattern name should contains the {admin} placeholder');
114
            }
115 29
            if (strstr($namePattern, '{action}') === false) {
116 1
                throw new InvalidOptionsException('Admin routing configuration pattern name should contains the {action} placeholder');
117
            }
118
119 29
            return $value;
120 29
        });
121 29
    }
122
123
    /**
124
     * @param OptionsResolver $resolver
125
     */
126 29
    protected function setTranslationOptions(OptionsResolver $resolver)
127
    {
128 29
        $resolver->setDefault('translation', [
129 29
            'enabled' => true,
130
            'pattern' => 'lag.admin.{key}'
131
        ]);
132 29
        $resolver->setAllowedTypes('translation', 'array');
133
        $resolver->setNormalizer('translation', function(Options $options, $value) {
134
135 29
            if (!array_key_exists('enabled', $value)) {
136 1
                throw new InvalidOptionsException('Admin translation enabled parameter should be defined');
137
            }
138
139 29
            if (!is_bool($value['enabled'])) {
140 1
                throw new InvalidOptionsException('Admin translation enabled parameter should be a boolean');
141
            }
142
143 29
            if (!array_key_exists('pattern', $value)) {
144 1
                $value['pattern'] = '{admin}.{key}';
145
            }
146
147 29
            if ($value['enabled'] && strstr($value['pattern'], '{key}') === false) {
148 1
                throw new InvalidOptionsException('Admin translation pattern should contains the {key} placeholder');
149
            }
150
151 29
            return $value;
152 29
        });
153 29
    }
154
155
    /**
156
     * @param OptionsResolver $resolver
157
     */
158 29
    protected function setFieldsOptions(OptionsResolver $resolver)
159
    {
160
        $defaultMapping = [
161 29
            Field::TYPE_STRING => StringField::class,
162 29
            Field::TYPE_ARRAY => Field\ArrayField::class,
163 29
            Field::TYPE_LINK => Field\Link::class,
164 29
            Field::TYPE_DATE => Field\Date::class,
165 29
            Field::TYPE_COUNT => Field\Count::class,
166 29
            Field::TYPE_ACTION => Field\Action::class,
167 29
            Field::TYPE_COLLECTION => Field\Collection::class,
168 29
            Field::TYPE_BOOLEAN => Field\Boolean::class,
169
        ];
170
171 29
        $resolver->setDefault('fields_mapping', $defaultMapping);
172 29
        $resolver->setAllowedTypes('fields_mapping', 'array');
173
        $resolver->setNormalizer('fields_mapping', function(Options $options, $value) use ($defaultMapping) {
174
            // merge with default mapping to allow override
175 29
            $value = array_merge($defaultMapping, $value);
176
177 29
            return $value;
178 29
        });
179
180
        // fields templates mapping
181
        $defaultMapping = [
182 29
            Field::TYPE_LINK => 'LAGAdminBundle:Render:link.html.twig',
183
        ];
184
185 29
        $resolver->setDefault('fields_template_mapping', $defaultMapping);
186 29
        $resolver->setAllowedTypes('fields_template_mapping', 'array');
187 29
        $resolver->setNormalizer('fields_template_mapping', function(Options $options, $value) use ($defaultMapping) {
188
            // merge with default mapping to allow override
189 29
            $value = array_merge($defaultMapping, $value);
190
191 29
            return $value;
192 29
        });
193 29
    }
194
}
195