Passed
Pull Request — master (#138)
by Arnaud
08:18 queued 05:22
created

ApplicationConfiguration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 91
dl 0
loc 129
rs 10
c 6
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B configureOptions() 0 80 5
A setFieldsOptions() 0 40 1
1
<?php
2
3
namespace LAG\AdminBundle\Configuration;
4
5
use JK\Configuration\Configuration;
6
use LAG\AdminBundle\Admin\Action;
7
use LAG\AdminBundle\Admin\Admin;
8
use LAG\AdminBundle\Configuration\Behavior\TranslationConfigurationTrait;
9
use LAG\AdminBundle\Field\AbstractField;
10
use LAG\AdminBundle\Field\ActionCollectionField;
11
use LAG\AdminBundle\Field\ActionField;
12
use LAG\AdminBundle\Field\ArrayField;
13
use LAG\AdminBundle\Field\AutoField;
14
use LAG\AdminBundle\Field\BooleanField;
15
use LAG\AdminBundle\Field\CollectionField;
16
use LAG\AdminBundle\Field\CountField;
17
use LAG\AdminBundle\Field\DateField;
18
use LAG\AdminBundle\Field\LinkField;
19
use LAG\AdminBundle\Field\MappedField;
20
use LAG\AdminBundle\Field\StringField;
21
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
22
use Symfony\Component\OptionsResolver\Options;
23
use Symfony\Component\OptionsResolver\OptionsResolver;
24
25
/**
26
 * Application configuration class. Allow easy configuration manipulation within an Admin.
27
 */
28
class ApplicationConfiguration extends Configuration
29
{
30
    use TranslationConfigurationTrait;
31
32
    /**
33
     * Configure configuration allowed parameters.
34
     */
35
    public function configureOptions(OptionsResolver $resolver)
36
    {
37
        $resolver
38
            ->setDefaults([
39
                // enable or disable extra configuration listener
40
                'enable_extra_configuration' => true,
41
                'enable_security' => true,
42
                'enable_menus' => true,
43
                'enable_homepage' => true,
44
                'title' => 'AdminBundle application',
45
                'description' => '',
46
                'locale' => 'en',
47
                // Main base template as bundles are not loaded when reading the configuration, the kernel
48
                // locateResources will always failed. So we must not check resource existence here.
49
                'base_template' => '@LAGAdmin/base.html.twig',
50
                'ajax_template' => '@LAGAdmin/empty.html.twig',
51
                'block_template' => '@LAGAdmin/Form/fields.html.twig',
52
                'menu_template' => '@LAGAdmin/Menu/menu.html.twig',
53
                'list_template' => '@LAGAdmin/CRUD/list.html.twig',
54
                'edit_template' => '@LAGAdmin/CRUD/edit.html.twig',
55
                'create_template' => '@LAGAdmin/CRUD/create.html.twig',
56
                'delete_template' => '@LAGAdmin/CRUD/delete.html.twig',
57
                'homepage_template' => '@LAGAdmin/Pages/home.html.twig',
58
                'homepage_route' => 'lag.admin.homepage',
59
                'routing_url_pattern' => '/{admin}/{action}',
60
                'routing_name_pattern' => 'lag.admin.{admin}.{action}',
61
                'bootstrap' => true,
62
                'date_format' => 'Y/m/d',
63
                'pager' => 'pagerfanta',
64
                // string length before truncation (0 means no truncation)
65
                'string_length' => 200,
66
                'string_length_truncate' => '...',
67
                'max_per_page' => 20,
68
                'admin_class' => Admin::class,
69
                'action_class' => Action::class,
70
                'permissions' => 'ROLE_ADMIN',
71
                'page_parameter' => 'page',
72
            ])
73
            ->setAllowedTypes('enable_extra_configuration', 'boolean')
74
            ->setAllowedTypes('enable_security', 'boolean')
75
            ->setAllowedTypes('enable_menus', 'boolean')
76
            ->setAllowedTypes('title', 'string')
77
            ->setAllowedTypes('description', 'string')
78
            ->setAllowedTypes('locale', 'string')
79
            ->setAllowedTypes('base_template', 'string')
80
            ->setAllowedTypes('block_template', 'string')
81
            ->setAllowedTypes('bootstrap', 'boolean')
82
            ->setAllowedTypes('date_format', 'string')
83
            ->setAllowedTypes('string_length', 'integer')
84
            ->setAllowedTypes('string_length_truncate', 'string')
85
            ->setAllowedTypes('max_per_page', 'integer')
86
            ->setAllowedTypes('admin_class', 'string')
87
            ->setAllowedTypes('routing_name_pattern', 'string')
88
            ->setAllowedTypes('routing_url_pattern', 'string')
89
            ->setAllowedTypes('page_parameter', 'string')
90
            ->setNormalizer('routing_name_pattern', function (Options $options, $value) {
91
                if (false === strstr($value, '{admin}')) {
92
                    throw new InvalidOptionsException('Admin routing configuration pattern name should contains the {admin} placeholder');
93
                }
94
                if (false === strstr($value, '{action}')) {
95
                    throw new InvalidOptionsException('Admin routing configuration pattern name should contains the {action} placeholder');
96
                }
97
98
                return $value;
99
            })
100
            ->setNormalizer('routing_url_pattern', function (Options $options, $value) {
101
                if (false === strstr($value, '{admin}')) {
102
                    throw new InvalidOptionsException('Admin routing configuration url pattern should contains {admin} placeholder');
103
                }
104
105
                if (false === strstr($value, '{action}')) {
106
                    throw new InvalidOptionsException('Admin routing configuration url pattern should contains the {action} placeholder');
107
                }
108
109
                return $value;
110
            })
111
        ;
112
113
        $this->setFieldsOptions($resolver);
114
        $this->setTranslationOptions($resolver);
115
    }
116
117
    protected function setFieldsOptions(OptionsResolver $resolver)
118
    {
119
        $defaultMapping = [
120
            AbstractField::TYPE_STRING => StringField::class,
121
            AbstractField::TYPE_TEXT => StringField::class,
122
            AbstractField::TYPE_FLOAT => StringField::class,
123
            AbstractField::TYPE_INTEGER => StringField::class,
124
            AbstractField::TYPE_ARRAY => ArrayField::class,
125
            AbstractField::TYPE_ACTION => ActionField::class,
126
            AbstractField::TYPE_COLLECTION => CollectionField::class,
127
            AbstractField::TYPE_BOOLEAN => BooleanField::class,
128
            AbstractField::TYPE_MAPPED => MappedField::class,
129
            AbstractField::TYPE_ACTION_COLLECTION => ActionCollectionField::class,
130
            AbstractField::TYPE_LINK => LinkField::class,
131
            AbstractField::TYPE_DATE => DateField::class,
132
            AbstractField::TYPE_COUNT => CountField::class,
133
            AbstractField::TYPE_AUTO => AutoField::class,
134
        ];
135
136
        $resolver->setDefault('fields_mapping', $defaultMapping);
137
        $resolver->setAllowedTypes('fields_mapping', 'array');
138
        $resolver->setNormalizer('fields_mapping', function (Options $options, $value) use ($defaultMapping) {
139
            // Merge with default mapping to allow override
140
            $value = array_merge($defaultMapping, $value);
141
142
            return $value;
143
        });
144
145
        // Fields templates mapping
146
        $defaultMapping = [
147
            AbstractField::TYPE_LINK => 'LAGAdminBundle:Render:link.html.twig',
148
        ];
149
150
        $resolver->setDefault('fields_template_mapping', $defaultMapping);
151
        $resolver->setAllowedTypes('fields_template_mapping', 'array');
152
        $resolver->setNormalizer('fields_template_mapping', function (Options $options, $value) use ($defaultMapping) {
153
            // Merge with default mapping to allow override
154
            $value = array_merge($defaultMapping, $value);
155
156
            return $value;
157
        });
158
    }
159
160
}
161