Completed
Branch dev (a083dd)
by Arnaud
03:08
created

ApplicationConfiguration::useBootstrap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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