Completed
Pull Request — dev (#9)
by Arnaud
12:24
created

ApplicationConfiguration   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 156
rs 10

2 Methods

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