Completed
Push — dev ( 884ea2...72842e )
by Arnaud
02:50
created

ApplicationConfiguration::getRoutingNamePattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace LAG\AdminBundle\Admin\Configuration;
4
5
use LAG\AdminBundle\Field\Field;
6
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
7
use Symfony\Component\OptionsResolver\Options;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
class ApplicationConfiguration
11
{
12
    /**
13
     * Indicate wether or not the extra configuration should be enabled or not.
14
     *
15
     * @var bool
16
     */
17
    protected $enableExtraConfiguration = true;
18
19
    /**
20
     * Application title.
21
     *
22
     * @var string
23
     */
24
    protected $title;
25
26
    /**
27
     * Application description.
28
     *
29
     * @var string
30
     */
31
    protected $description;
32
33
    /**
34
     * Application locale.
35
     *
36
     * @var string
37
     */
38
    protected $locale;
39
40
    /**
41
     * Admin main twig layout.
42
     *
43
     * @var string
44
     */
45
    protected $layout;
46
47
    /**
48
     * Twig template use for rendering block in forms.
49
     *
50
     * @var string
51
     */
52
    protected $blockTemplate;
53
54
    /**
55
     * Use bootstrap integration.
56
     *
57
     * @var bool
58
     */
59
    protected $bootstrap = false;
60
61
    /**
62
     * Application main date format.
63
     *
64
     * @var string
65
     */
66
    protected $dateFormat;
67
68
    /**
69
     * String length before truncate it (if null, no truncation).
70
     *
71
     * @var int
72
     */
73
    protected $stringLength;
74
75
    /**
76
     * Replace string in truncation.
77
     *
78
     * @var int
79
     */
80
    protected $stringLengthTruncate;
81
82
    /**
83
     * Url routing pattern.
84
     *
85
     * @var string
86
     */
87
    protected $routingUrlPattern;
88
89
    /**
90
     * Generated route name pattern.
91
     *
92
     * @var string
93
     */
94
    protected $routingNamePattern;
95
96
    /**
97
     * Default number of displayed records in list.
98
     *
99
     * @var int
100
     */
101
    protected $maxPerPage;
102
103
    /**
104
     * Define wether if translator should be used or not.
105
     *
106
     * @var bool
107
     */
108
    protected $useTranslation = true;
109
110
    /**
111
     * Pattern use for translation key (ie: lag.admin.{key}, admin will.
112
     *
113
     * @var string
114
     */
115
    protected $translationPattern;
116
117
    /**
118
     * Contains a array of fqcn field classes indexed by field name.
119
     *
120
     * @var array
121
     */
122
    protected $fieldsMapping = [];
123
124
    /**
125
     * ApplicationConfiguration constructor.
126
     *
127
     * @param array $applicationConfiguration
128
     * @param $locale
129
     */
130 3
    public function __construct(array $applicationConfiguration = [], $locale)
131
    {
132 3
        $resolver = new OptionsResolver();
133 3
        $resolver->setDefaults([
134 3
            'enable_extra_configuration' => true,
135 3
            'title' => '',
136 3
            'description' => '',
137 3
            'locale' => $locale,
138 3
            'layout' => 'LAGAdminBundle::admin.layout.html.twig',
139 3
            'block_template' => 'LAGAdminBundle:Form:fields.html.twig',
140 3
            'bootstrap' => false,
141 3
            'date_format' => 'd/m/Y',
142 3
            'string_length' => 0,
143 3
            'string_length_truncate' => '...',
144
            'routing' => [
145 3
                'url_pattern' => '/{admin}/{action}',
146 3
                'name_pattern' => 'lag.admin.{admin}',
147 3
            ],
148
            'translation' => [
149 3
                'enabled' => true,
150 3
                'pattern' => 'lag.admin.{key}',
151 3
            ],
152 3
            'max_per_page' => 25,
153
            'fields_mapping' => [
154 3
            ],
155 3
        ]);
156 3
        $resolver->setAllowedValues('enable_extra_configuration', [true, false]);
157 3
        $applicationConfiguration = $resolver->resolve($applicationConfiguration);
158
        // merge default field configuration
159 3
        $applicationConfiguration['fields_mapping'] = array_merge([
160 3
            Field::TYPE_STRING => 'LAG\AdminBundle\Field\Field\StringField',
161 3
            Field::TYPE_ARRAY => 'LAG\AdminBundle\Field\Field\ArrayField',
162 3
            Field::TYPE_LINK => 'LAG\AdminBundle\Field\Field\Link',
163 3
            Field::TYPE_DATE => 'LAG\AdminBundle\Field\Field\Date',
164 3
            Field::TYPE_COUNT => 'LAG\AdminBundle\Field\Field\Count',
165 3
            Field::TYPE_ACTION => 'LAG\AdminBundle\Field\Field\Action',
166 3
            Field::TYPE_COLLECTION => 'LAG\AdminBundle\Field\Field\Collection',
167 3
            Field::TYPE_BOOLEAN => 'LAG\AdminBundle\Field\Field\Boolean',
168 3
        ], $applicationConfiguration['fields_mapping']);
169
170
        // resolving routing options
171 3
        $routingConfiguration = $applicationConfiguration['routing'];
172 3
        $resolver->clear();
173 3
        $resolver->setRequired([
174 3
            'url_pattern',
175 3
            'name_pattern',
176 3
        ]);
177
        $resolver->setNormalizer('url_pattern', function (Options $options, $value) {
178 3
            if (strstr($value, '{admin}') === false) {
179
                throw new InvalidOptionsException('Admin routing configuration url pattern should contains {admin} placeholder');
180
            }
181 3
            if (strstr($value, '{action}') === false) {
182
                throw new InvalidOptionsException('Admin routing configuration url pattern should contains {action} placeholder');
183
            }
184
185 3
            return $value;
186 3
        });
187 View Code Duplication
        $resolver->setNormalizer('name_pattern', function (Options $options, $value) {
188 3
            if (strstr($value, '{admin}') === false) {
189
                throw new InvalidOptionsException('Admin routing configuration pattern name should contains {admin} placeholder');
190
            }
191
192 3
            return $value;
193 3
        });
194 3
        $routingConfiguration = $resolver->resolve($routingConfiguration);
195
        // routing configuration
196 3
        $this->routingUrlPattern = $routingConfiguration['url_pattern'];
197 3
        $this->routingNamePattern = $routingConfiguration['name_pattern'];
198
199
        // resolving translation configuration
200 3
        $translationConfiguration = $applicationConfiguration['translation'];
201
        $resolver
202 3
            ->clear()
203 3
            ->setDefault('enabled', true)
204 3
            ->setDefault('pattern', 'lag.admin.{key}');
205 3 View Code Duplication
        $resolver->setNormalizer('pattern', function (Options $options, $value) {
206 3
            if (strstr($value, 'key') === false) {
207
                throw new InvalidOptionsException('Admin translation configuration pattern should contains {key} placeholder');
208
            }
209
210 3
            return $value;
211 3
        });
212 3
        $translationConfiguration = $resolver->resolve($translationConfiguration);
213
        // translation configuration
214 3
        $this->useTranslation = $translationConfiguration['enabled'];
215 3
        $this->translationPattern = $translationConfiguration['pattern'];
216
217
        // application main configuration
218 3
        $this->title = $applicationConfiguration['title'];
219 3
        $this->description = $applicationConfiguration['description'];
220 3
        $this->locale = $applicationConfiguration['locale'];
221 3
        $this->title = $applicationConfiguration['title'];
222 3
        $this->layout = $applicationConfiguration['layout'];
223 3
        $this->blockTemplate = $applicationConfiguration['block_template'];
224 3
        $this->bootstrap = $applicationConfiguration['bootstrap'];
225 3
        $this->dateFormat = $applicationConfiguration['date_format'];
226 3
        $this->stringLength = $applicationConfiguration['string_length'];
227 3
        $this->stringLengthTruncate = $applicationConfiguration['string_length_truncate'];
228 3
        $this->maxPerPage = $applicationConfiguration['max_per_page'];
229 3
        $this->fieldsMapping = $applicationConfiguration['fields_mapping'];
230 3
    }
231
232
    /**
233
     * @return mixed
234
     */
235 1
    public function getTitle()
236
    {
237 1
        return $this->title;
238
    }
239
240
    /**
241
     * @param mixed $title
242
     */
243
    public function setTitle($title)
244
    {
245
        $this->title = $title;
246
    }
247
248
    /**
249
     * @return mixed
250
     */
251 1
    public function getLayout()
252
    {
253 1
        return $this->layout;
254
    }
255
256
    /**
257
     * @param mixed $layout
258
     */
259
    public function setLayout($layout)
260
    {
261
        $this->layout = $layout;
262
    }
263
264
    /**
265
     * @return mixed
266
     */
267 1
    public function getBlockTemplate()
268
    {
269 1
        return $this->blockTemplate;
270
    }
271
272
    /**
273
     * @param mixed $blockTemplate
274
     */
275
    public function setBlockTemplate($blockTemplate)
276
    {
277
        $this->blockTemplate = $blockTemplate;
278
    }
279
280
    /**
281
     * @return mixed
282
     */
283 1
    public function getDescription()
284
    {
285 1
        return $this->description;
286
    }
287
288
    /**
289
     * @param mixed $description
290
     */
291
    public function setDescription($description)
292
    {
293
        $this->description = $description;
294
    }
295
296
    /**
297
     * @return bool
298
     */
299 1
    public function useBootstrap()
300
    {
301 1
        return $this->bootstrap;
302
    }
303
304
    /**
305
     * @param bool $bootstrap
306
     */
307
    public function setBootstrap($bootstrap)
308
    {
309
        $this->bootstrap = $bootstrap;
310
    }
311
312
    /**
313
     * @return mixed
314
     */
315 1
    public function getDateFormat()
316
    {
317 1
        return $this->dateFormat;
318
    }
319
320
    /**
321
     * @return string
322
     */
323
    public function getJavascriptDateFormat()
324
    {
325
        $jsDateFormat = str_replace('Y', 'yyyy', $this->getDateFormat());
326
        $jsDateFormat = str_replace('mm', 'ii', $jsDateFormat);
327
        $jsDateFormat = str_replace('MM', 'mm', $jsDateFormat);
328
        $jsDateFormat = str_replace('HH', 'hh', $jsDateFormat);
329
        $jsDateFormat = str_replace('d', 'dd', $jsDateFormat);
330
331
        return $jsDateFormat;
332
    }
333
334
    /**
335
     * @param mixed $dateFormat
336
     */
337
    public function setDateFormat($dateFormat)
338
    {
339
        $this->dateFormat = $dateFormat;
340
    }
341
342
    /**
343
     * @return string
344
     */
345 1
    public function getRoutingUrlPattern()
346
    {
347 1
        return $this->routingUrlPattern;
348
    }
349
350
    /**
351
     * @param string $routingUrlPattern
352
     */
353
    public function setRoutingUrlPattern($routingUrlPattern)
354
    {
355
        $this->routingUrlPattern = $routingUrlPattern;
356
    }
357
358
    /**
359
     * @return string
360
     */
361 1
    public function getRoutingNamePattern()
362
    {
363 1
        return $this->routingNamePattern;
364
    }
365
366
    /**
367
     * @param string $routingNamePattern
368
     */
369
    public function setRoutingNamePattern($routingNamePattern)
370
    {
371
        $this->routingNamePattern = $routingNamePattern;
372
    }
373
374
    /**
375
     * @return string
376
     */
377 1
    public function getLocale()
378
    {
379 1
        return $this->locale;
380
    }
381
382
    /**
383
     * @param string $locale
384
     */
385
    public function setLocale($locale)
386
    {
387
        $this->locale = $locale;
388
    }
389
390
    /**
391
     * @return int
392
     */
393 1
    public function getStringLength()
394
    {
395 1
        return $this->stringLength;
396
    }
397
398
    /**
399
     * @param int $stringLength
400
     */
401
    public function setStringLength($stringLength)
402
    {
403
        $this->stringLength = $stringLength;
404
    }
405
406
    /**
407
     * @return mixed
408
     */
409 1
    public function getStringLengthTruncate()
410
    {
411 1
        return $this->stringLengthTruncate;
412
    }
413
414
    /**
415
     * @param mixed $stringLengthTruncate
416
     */
417
    public function setStringLengthTruncate($stringLengthTruncate)
418
    {
419
        $this->stringLengthTruncate = $stringLengthTruncate;
420
    }
421
422
    /**
423
     * @return bool
424
     */
425 1
    public function isBootstrap()
426
    {
427 1
        return $this->bootstrap;
428
    }
429
430
    /**
431
     * @return int
432
     */
433 1
    public function getMaxPerPage()
434
    {
435 1
        return $this->maxPerPage;
436
    }
437
438
    /**
439
     * @param int $maxPerPage
440
     */
441
    public function setMaxPerPage($maxPerPage)
442
    {
443
        $this->maxPerPage = $maxPerPage;
444
    }
445
446
    /**
447
     * @return string
448
     */
449 1
    public function getTranslationPattern()
450
    {
451 1
        return $this->translationPattern;
452
    }
453
454
    /**
455
     * @param string $translationPattern
456
     */
457
    public function setTranslationPattern($translationPattern)
458
    {
459
        $this->translationPattern = $translationPattern;
460
    }
461
462
    /**
463
     * @param $key
464
     * @param string $adminName
465
     * @return string
466
     */
467 1
    public function getTranslationKey($key, $adminName = null)
468
    {
469 1
        $translationKey = $this->translationPattern;
470
471 1
        if (strstr($this->translationPattern, '{admin}') && $adminName != null) {
472
            $translationKey = str_replace('{admin}', $adminName, $translationKey);
473
        }
474 1
        $translationKey = str_replace('{key}', $key, $translationKey);
475
476 1
        return $translationKey;
477
    }
478
479
    /**
480
     * @return bool
481
     */
482
    public function useTranslation()
483
    {
484
        return $this->useTranslation;
485
    }
486
487
    /**
488
     * @param bool $useTranslation
489
     */
490
    public function setUseTranslation($useTranslation)
491
    {
492
        $this->useTranslation = $useTranslation;
493
    }
494
495
    /**
496
     * Return array field mapping
497
     *
498
     * @return array
499
     */
500 1
    public function getFieldsMapping()
501
    {
502 1
        return $this->fieldsMapping;
503
    }
504
505
    /**
506
     * @return boolean
507
     */
508 1
    public function isExtraConfigurationEnabled()
509
    {
510 1
        return $this->enableExtraConfiguration;
511
    }
512
}
513