Completed
Pull Request — master (#90)
by Arnaud
16:42
created

ApplicationConfiguration::setRoutingOptions()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7.9936

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 16
cts 22
cp 0.7272
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 21
nc 1
nop 1
crap 7.9936
1
<?php
2
3
namespace LAG\AdminBundle\Application\Configuration;
4
5
use JK\Configuration\Configuration;
6
use LAG\AdminBundle\Admin\Admin;
7
use LAG\AdminBundle\Field\AbstractField;
8
use LAG\AdminBundle\Field\Field\Action;
9
use LAG\AdminBundle\Field\Field\ActionCollection;
10
use LAG\AdminBundle\Field\Field\ArrayField;
11
use LAG\AdminBundle\Field\Field\Boolean;
12
use LAG\AdminBundle\Field\Field\Collection;
13
use LAG\AdminBundle\Field\Field\Mapped;
14
use LAG\AdminBundle\Field\Field\StringField;
15
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
16
use Symfony\Component\OptionsResolver\Options;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
19
/**
20
 * Application configuration class. Allow easy configuration manipulation within an Admin.
21
 */
22
class ApplicationConfiguration extends Configuration
23
{
24
    /**
25
     * Configure configuration allowed parameters.
26
     *
27
     * @param OptionsResolver $resolver
28
     */
29 4
    public function configureOptions(OptionsResolver $resolver)
30
    {
31
        $resolver
32 4
            ->setDefaults([
33
                // enable or disable extra configuration listener
34 4
                'enable_extra_configuration' => true,
35
                'title' => 'AdminBundle application',
36
                'description' => '',
37
                'locale' => 'en',
38
                // main base template
39
                // as bundles are not loaded when reading the configuration, the kernel locateResources will always failed.
40
                // So we must not check resource existence here.
41
                'base_template' => 'LAGAdminBundle::admin.layout.html.twig',
42
                'block_template' => 'LAGAdminBundle:Form:fields.html.twig',
43
                'bootstrap' => true,
44
                'date_format' => 'Y/m/d',
45
                // string length before truncation (0 means no truncation)
46
                'string_length' => 0,
47
                'string_length_truncate' =>  '...',
48
                'max_per_page' => 25,
49
                'admin_class' => Admin::class,
50
            ])
51 4
            ->setAllowedTypes('enable_extra_configuration', 'boolean')
52 4
            ->setAllowedTypes('title', 'string')
53 4
            ->setAllowedTypes('description', 'string')
54 4
            ->setAllowedTypes('locale', 'string')
55 4
            ->setAllowedTypes('base_template', 'string')
56 4
            ->setAllowedTypes('block_template', 'string')
57 4
            ->setAllowedTypes('bootstrap', 'boolean')
58 4
            ->setAllowedTypes('date_format', 'string')
59 4
            ->setAllowedTypes('string_length', 'integer')
60 4
            ->setAllowedTypes('string_length_truncate', 'string')
61 4
            ->setAllowedTypes('max_per_page', 'integer')
62 4
            ->setAllowedTypes('admin_class', 'string')
63
        ;
64
65
        // routing configuration (route name pattern and url name pattern)
66 4
        $this->setRoutingOptions($resolver);
67
68
        // translation configuration
69 4
        $this->setTranslationOptions($resolver);
70
71
        // admin field type mapping
72 4
        $this->setFieldsOptions($resolver);
73 4
    }
74
75
    /**
76
     * @param OptionsResolver $resolver
77
     */
78 4
    protected function setRoutingOptions(OptionsResolver $resolver)
79
    {
80 4
        $resolver->setDefault('routing', [
81 4
            'url_pattern' => '/{admin}/{action}',
82
            'name_pattern' => 'lag.admin.{admin}.{action}',
83
        ]);
84 4
        $resolver->setAllowedTypes('routing', 'array');
85 4
        $resolver->setNormalizer('routing', function(Options $options, $value) {
86
87 4
            if (!array_key_exists('url_pattern', $value)) {
88
                $value['url_pattern'] = '/{admin}/{action}';
89
            }
90 4
            if (!array_key_exists('name_pattern', $value)) {
91
                $value['name_pattern'] = 'lag.admin.{admin}.{action}';
92
            }
93
94
            // url pattern should contain {admin} and {action} token
95 4
            $urlPattern = $value['url_pattern'];
96
97 4
            if (strstr($urlPattern, '{admin}') === false) {
98
                throw new InvalidOptionsException('Admin routing configuration url pattern should contains {admin} placeholder');
99
            }
100 4
            if (strstr($urlPattern, '{action}') === false) {
101
                throw new InvalidOptionsException('Admin routing configuration url pattern should contains the {action} placeholder');
102
            }
103
104
            // name pattern should contain {admin} token
105 4
            $namePattern = $value['name_pattern'];
106
107 4
            if (strstr($namePattern, '{admin}') === false) {
108
                throw new InvalidOptionsException('Admin routing configuration pattern name should contains the {admin} placeholder');
109
            }
110 4
            if (strstr($namePattern, '{action}') === false) {
111
                throw new InvalidOptionsException('Admin routing configuration pattern name should contains the {action} placeholder');
112
            }
113
114 4
            return $value;
115 4
        });
116 4
    }
117
118
    /**
119
     * @param OptionsResolver $resolver
120
     */
121 4
    protected function setTranslationOptions(OptionsResolver $resolver)
122
    {
123 4
        $resolver->setDefault('translation', [
124 4
            'enabled' => true,
125
            'pattern' => 'lag.admin.{key}'
126
        ]);
127 4
        $resolver->setAllowedTypes('translation', 'array');
128 4
        $resolver->setNormalizer('translation', function(Options $options, $value) {
129
130 4
            if (!array_key_exists('enabled', $value)) {
131
                throw new InvalidOptionsException('Admin translation enabled parameter should be defined');
132
            }
133
134 4
            if (!is_bool($value['enabled'])) {
135
                throw new InvalidOptionsException('Admin translation enabled parameter should be a boolean');
136
            }
137
138 4
            if (!array_key_exists('pattern', $value)) {
139 1
                $value['pattern'] = '{admin}.{key}';
140
            }
141
142 4
            if ($value['enabled'] && strstr($value['pattern'], '{key}') === false) {
143
                throw new InvalidOptionsException('Admin translation pattern should contains the {key} placeholder');
144
            }
145
146 4
            return $value;
147 4
        });
148 4
    }
149
150
    /**
151
     * @param OptionsResolver $resolver
152
     */
153 4
    protected function setFieldsOptions(OptionsResolver $resolver)
154
    {
155
        $defaultMapping = [
156 4
            AbstractField::TYPE_STRING => StringField::class,
157 4
            AbstractField::TYPE_ARRAY => ArrayField::class,
158 4
            AbstractField::TYPE_ACTION => Action::class,
159 4
            AbstractField::TYPE_COLLECTION => Collection::class,
160 4
            AbstractField::TYPE_BOOLEAN => Boolean::class,
161 4
            AbstractField::TYPE_MAPPED => Mapped::class,
162 4
            AbstractField::TYPE_ACTION_COLLECTION => ActionCollection::class,
163
        ];
164
165 4
        $resolver->setDefault('fields_mapping', $defaultMapping);
166 4
        $resolver->setAllowedTypes('fields_mapping', 'array');
167 4
        $resolver->setNormalizer('fields_mapping', function(Options $options, $value) use ($defaultMapping) {
168
            // merge with default mapping to allow override
169 4
            $value = array_merge($defaultMapping, $value);
170
171 4
            return $value;
172 4
        });
173
174
        // fields templates mapping
175
        $defaultMapping = [
176 4
            AbstractField::TYPE_LINK => 'LAGAdminBundle:Render:link.html.twig',
177
        ];
178
179 4
        $resolver->setDefault('fields_template_mapping', $defaultMapping);
180 4
        $resolver->setAllowedTypes('fields_template_mapping', 'array');
181 4
        $resolver->setNormalizer('fields_template_mapping', function(Options $options, $value) use ($defaultMapping) {
182
            // merge with default mapping to allow override
183 4
            $value = array_merge($defaultMapping, $value);
184
185 4
            return $value;
186 4
        });
187
    }
188
}
189