Passed
Pull Request — master (#300)
by Arnaud
14:15 queued 08:05
created

ApplicationConfiguration   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 283
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 121
c 0
b 0
f 0
dl 0
loc 283
rs 9.44
wmc 37

30 Methods

Rating   Name   Duplication   Size   Complexity  
A getDeleteTemplate() 0 3 1
A getDescription() 0 3 1
A getPageParameter() 0 3 1
A getDateFormat() 0 3 1
A isTranslationEnabled() 0 3 1
A getTitle() 0 3 1
A getPager() 0 7 2
A getFieldsMapping() 0 3 1
A getRouteName() 0 12 1
A getMaxPerPage() 0 3 1
A getListTemplate() 0 3 1
A getHomepageRoute() 0 3 1
A getBaseTemplate() 0 3 1
A getResourcesPath() 0 3 1
A getCreateTemplate() 0 3 1
A isSecurityEnabled() 0 3 1
A getTranslationPattern() 0 7 2
A getFieldsMappingNormalizer() 0 8 2
A configureOptions() 0 76 1
A getRoutesPattern() 0 3 1
A getAjaxTemplate() 0 3 1
A getActionClass() 0 3 1
A getPermissions() 0 3 1
A getStringLength() 0 3 1
A getTranslationCatalog() 0 7 2
A isPaginationEnabled() 0 9 2
A getRoutesPatternNormalizer() 0 12 3
A getStringTruncate() 0 3 1
A getAdminClass() 0 3 1
A getEditTemplate() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Admin\Configuration;
6
7
use Closure;
8
use JK\Configuration\ServiceConfiguration;
9
use LAG\AdminBundle\Admin\Action;
10
use LAG\AdminBundle\Admin\Admin;
11
use LAG\AdminBundle\Exception\Exception;
12
use LAG\AdminBundle\Field\ActionCollectionField;
13
use LAG\AdminBundle\Field\ActionField;
14
use LAG\AdminBundle\Field\ArrayField;
15
use LAG\AdminBundle\Field\AutoField;
16
use LAG\AdminBundle\Field\BooleanField;
17
use LAG\AdminBundle\Field\CountField;
18
use LAG\AdminBundle\Field\DateField;
19
use LAG\AdminBundle\Field\FieldInterface;
20
use LAG\AdminBundle\Field\LinkField;
21
use LAG\AdminBundle\Field\MappedField;
22
use LAG\AdminBundle\Field\StringField;
23
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
24
use Symfony\Component\OptionsResolver\Options;
25
use Symfony\Component\OptionsResolver\OptionsResolver;
26
use function Symfony\Component\String\u;
27
28
/**
29
 * Application configuration class. Allow easy configuration manipulation within an Admin.
30
 */
31
class ApplicationConfiguration extends ServiceConfiguration
32
{
33
    public const FIELD_MAPPING = [
34
        FieldInterface::TYPE_STRING => StringField::class,
35
        FieldInterface::TYPE_TEXT => StringField::class,
36
        FieldInterface::TYPE_FLOAT => StringField::class,
37
        FieldInterface::TYPE_INTEGER => StringField::class,
38
        FieldInterface::TYPE_ARRAY => ArrayField::class,
39
        FieldInterface::TYPE_ACTION => ActionField::class,
40
        FieldInterface::TYPE_BOOLEAN => BooleanField::class,
41
        FieldInterface::TYPE_MAPPED => MappedField::class,
42
        FieldInterface::TYPE_ACTION_COLLECTION => ActionCollectionField::class,
43
        FieldInterface::TYPE_LINK => LinkField::class,
44
        FieldInterface::TYPE_DATE => DateField::class,
45
        FieldInterface::TYPE_COUNT => CountField::class,
46
        FieldInterface::TYPE_AUTO => AutoField::class,
47
    ];
48
49
    public function configureOptions(OptionsResolver $resolver): void
50
    {
51
        $resolver
52
            ->setDefault('title', 'Admin Application')
53
            ->setAllowedTypes('title', 'string')
54
            ->setDefault('description', 'Admin Application')
55
            ->setAllowedTypes('description', 'string')
56
57
            // Admins
58
            ->setRequired('resources_path')
59
            ->setAllowedTypes('resources_path', 'string')
60
            ->setDefault('admin_class', Admin::class)
61
            ->setAllowedTypes('admin_class', 'string')
62
            ->setDefault('action_class', Action::class)
63
            ->setAllowedTypes('action_class', 'string')
64
65
            // Templates
66
            ->setDefault('base_template', '@LAGAdmin/base.html.twig')
67
            ->setAllowedTypes('base_template', 'string')
68
            ->setDefault('ajax_template', '@LAGAdmin/empty.html.twig')
69
            ->setAllowedTypes('ajax_template', 'string')
70
            ->setDefault('create_template', '@LAGAdmin/crud/create.html.twig')
71
            ->setAllowedTypes('create_template', 'string')
72
            ->setDefault('edit_template', '@LAGAdmin/crud/edit.html.twig')
73
            ->setAllowedTypes('edit_template', 'string')
74
            ->setDefault('list_template', '@LAGAdmin/crud/list.html.twig')
75
            ->setAllowedTypes('list_template', 'string')
76
            ->setDefault('delete_template', '@LAGAdmin/crud/delete.html.twig')
77
            ->setAllowedTypes('delete_template', 'string')
78
79
            // Routing
80
            ->setDefault('routes_pattern', 'lag_admin.{admin}.{action}')
81
            ->setAllowedTypes('routes_pattern', 'string')
82
            ->setNormalizer('routes_pattern', $this->getRoutesPatternNormalizer())
83
            ->setDefault('homepage_route', 'lag_admin.homepage')
84
            ->setAllowedTypes('homepage_route', 'string')
85
86
            // Dates
87
            ->setDefault('date_format', 'Y/m/d')
88
            ->setAllowedTypes('date_format', 'string')
89
90
            // Pagination
91
            ->setDefault('pager', 'pagerfanta')
92
            ->setAllowedTypes('pager', ['boolean', 'string'])
93
            ->setDefault('max_per_page', 25)
94
            ->setAllowedTypes('max_per_page', 'integer')
95
            ->setDefault('page_parameter', 'page')
96
            ->setAllowedTypes('page_parameter', 'string')
97
98
            // List default parameters
99
            ->setDefault('string_length', 100)
100
            ->setAllowedTypes('string_length', 'integer')
101
            ->setDefault('string_truncate', '...')
102
            ->setAllowedTypes('string_truncate', 'string')
103
104
            // Default permissions
105
            ->setDefault('enable_security', true)
106
            ->setAllowedTypes('enable_security', 'boolean')
107
            ->setDefault('permissions', 'ROLE_ADMIN')
108
            ->setAllowedTypes('permissions', 'string')
109
110
            // Translation
111
            ->setDefault('translation', function (OptionsResolver $translationResolver) {
112
                $translationResolver
113
                    ->setDefault('enabled', true)
114
                    ->setAllowedTypes('enabled', 'boolean')
115
                    ->setDefault('pattern', 'admin.{admin}.{key}')
116
                    ->setAllowedTypes('pattern', 'string')
117
                    ->setDefault('catalog', 'admin')
118
                    ->setAllowedTypes('catalog', 'string')
119
                ;
120
            })
121
122
            // Fields default mapping
123
            ->setDefault('fields_mapping', [])
124
            ->setNormalizer('fields_mapping', $this->getFieldsMappingNormalizer())
125
        ;
126
    }
127
128
    public function getTitle(): string
129
    {
130
        return $this->getString('title');
131
    }
132
133
    public function getDescription(): string
134
    {
135
        return $this->getString('description');
136
    }
137
138
    public function getResourcesPath(): string
139
    {
140
        return $this->getString('resources_path');
141
    }
142
143
    public function getAdminClass(): string
144
    {
145
        return $this->getString('admin_class');
146
    }
147
148
    public function getActionClass(): string
149
    {
150
        return $this->getString('action_class');
151
    }
152
153
    public function getBaseTemplate(): string
154
    {
155
        return $this->getString('base_template');
156
    }
157
158
    public function getAjaxTemplate(): string
159
    {
160
        return $this->getString('ajax_template');
161
    }
162
163
    public function getCreateTemplate(): string
164
    {
165
        return $this->getString('create_template');
166
    }
167
168
    public function getListTemplate(): string
169
    {
170
        return $this->getString('list_template');
171
    }
172
173
    public function getEditTemplate(): string
174
    {
175
        return $this->getString('edit_template');
176
    }
177
178
    public function getDeleteTemplate(): string
179
    {
180
        return $this->getString('delete_template');
181
    }
182
183
    public function getRoutesPattern(): string
184
    {
185
        return $this->get('routes_pattern');
186
    }
187
188
    public function getHomepageRoute(): string
189
    {
190
        return $this->getString('homepage_route');
191
    }
192
193
    public function getDateFormat(): string
194
    {
195
        return $this->getString('date_format');
196
    }
197
198
    public function isPaginationEnabled(): bool
199
    {
200
        $pager = $this->get('pager');
201
202
        if ($pager === false) {
203
            return false;
204
        }
205
206
        return true;
207
    }
208
209
    public function getPager(): string
210
    {
211
        if (!$this->isPaginationEnabled()) {
212
            throw new Exception('The pagination is not enabled');
213
        }
214
215
        return $this->getString('pager');
216
    }
217
218
    public function getMaxPerPage(): int
219
    {
220
        return $this->getInt('max_per_page');
221
    }
222
223
    public function getPageParameter(): string
224
    {
225
        return $this->getString('page_parameter');
226
    }
227
228
    public function getStringLength(): int
229
    {
230
        return $this->getInt('string_length');
231
    }
232
233
    public function getStringTruncate(): string
234
    {
235
        return $this->getString('string_truncate');
236
    }
237
238
    public function getPermissions(): string
239
    {
240
        return $this->getString('permissions');
241
    }
242
243
    public function isTranslationEnabled(): bool
244
    {
245
        return $this->get('translation')['enabled'];
246
    }
247
248
    public function getTranslationPattern(): string
249
    {
250
        if (!$this->isTranslationEnabled()) {
251
            throw new Exception('The translation is not enabled');
252
        }
253
254
        return $this->get('translation')['pattern'];
255
    }
256
257
    public function getTranslationCatalog(): string
258
    {
259
        if (!$this->isTranslationEnabled()) {
260
            throw new Exception('The translation is not enabled');
261
        }
262
263
        return $this->get('translation')['catalog'];
264
    }
265
266
    public function getFieldsMapping(): array
267
    {
268
        return $this->get('fields_mapping');
269
    }
270
271
    public function getRouteName(string $adminName, string $actionName): string
272
    {
273
        $routeName = str_replace(
274
            '{admin}',
275
            strtolower($adminName),
276
            $this->getRoutesPattern()
277
        );
278
279
        return str_replace(
280
            '{action}',
281
            $actionName,
282
            $routeName
283
        );
284
    }
285
286
    public function isSecurityEnabled(): bool
287
    {
288
        return $this->getBool('enable_security');
289
    }
290
291
    private function getRoutesPatternNormalizer(): Closure
292
    {
293
        return function (Options $options, $value) {
294
            if (!u($value)->containsAny('{admin}')) {
295
                throw new InvalidOptionsException('The routes pattern should contains "{admin}" placeholder. Given '.$value);
296
            }
297
298
            if (!u($value)->containsAny('{action}')) {
299
                throw new InvalidOptionsException('The routes pattern should contains "{action}" placeholder. Given '.$value);
300
            }
301
302
            return $value;
303
        };
304
    }
305
306
    private function getFieldsMappingNormalizer(): Closure
307
    {
308
        return function (Options $options, $value) {
309
            if (!\is_array($value)) {
310
                $value = [];
311
            }
312
313
            return array_merge(self::FIELD_MAPPING, $value);
314
        };
315
    }
316
}
317