Completed
Push — master ( d8083a...61164d )
by Arnaud
13s queued 11s
created

ApplicationConfiguration::setFieldsOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 27
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 40
ccs 25
cts 25
cp 1
crap 1
rs 9.488
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 4
    public function configureOptions(OptionsResolver $resolver)
36
    {
37
        $resolver
38 4
            ->setDefaults([
39
                // enable or disable extra configuration listener
40 4
                '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
                'resources_path' => __DIR__.'/../../../../config/admin/resources',
73
            ])
74 4
            ->setAllowedTypes('enable_extra_configuration', 'boolean')
75 4
            ->setAllowedTypes('enable_security', 'boolean')
76 4
            ->setAllowedTypes('enable_menus', 'boolean')
77 4
            ->setAllowedTypes('title', 'string')
78 4
            ->setAllowedTypes('description', 'string')
79 4
            ->setAllowedTypes('locale', 'string')
80 4
            ->setAllowedTypes('base_template', 'string')
81 4
            ->setAllowedTypes('block_template', 'string')
82 4
            ->setAllowedTypes('bootstrap', 'boolean')
83 4
            ->setAllowedTypes('date_format', 'string')
84 4
            ->setAllowedTypes('string_length', 'integer')
85 4
            ->setAllowedTypes('string_length_truncate', 'string')
86 4
            ->setAllowedTypes('max_per_page', 'integer')
87 4
            ->setAllowedTypes('admin_class', 'string')
88 4
            ->setAllowedTypes('routing_name_pattern', 'string')
89 4
            ->setAllowedTypes('routing_url_pattern', 'string')
90 4
            ->setAllowedTypes('page_parameter', 'string')
91
            ->setNormalizer('routing_name_pattern', function (Options $options, $value) {
92 4
                if (false === strstr($value, '{admin}')) {
93
                    throw new InvalidOptionsException('Admin routing configuration pattern name should contains the {admin} placeholder');
94
                }
95 4
                if (false === strstr($value, '{action}')) {
96
                    throw new InvalidOptionsException('Admin routing configuration pattern name should contains the {action} placeholder');
97
                }
98
99 4
                return $value;
100 4
            })
101
            ->setNormalizer('routing_url_pattern', function (Options $options, $value) {
102 4
                if (false === strstr($value, '{admin}')) {
103
                    throw new InvalidOptionsException('Admin routing configuration url pattern should contains {admin} placeholder');
104
                }
105
106 4
                if (false === strstr($value, '{action}')) {
107
                    throw new InvalidOptionsException('Admin routing configuration url pattern should contains the {action} placeholder');
108
                }
109
110 4
                return $value;
111 4
            })
112
        ;
113
114 4
        $this->setFieldsOptions($resolver);
115 4
        $this->configureTranslation($resolver);
116 4
    }
117
118 4
    protected function setFieldsOptions(OptionsResolver $resolver)
119
    {
120
        $defaultMapping = [
121 4
            AbstractField::TYPE_STRING => StringField::class,
122 4
            AbstractField::TYPE_TEXT => StringField::class,
123 4
            AbstractField::TYPE_FLOAT => StringField::class,
124 4
            AbstractField::TYPE_INTEGER => StringField::class,
125 4
            AbstractField::TYPE_ARRAY => ArrayField::class,
126 4
            AbstractField::TYPE_ACTION => ActionField::class,
127 4
            AbstractField::TYPE_COLLECTION => CollectionField::class,
128 4
            AbstractField::TYPE_BOOLEAN => BooleanField::class,
129 4
            AbstractField::TYPE_MAPPED => MappedField::class,
130 4
            AbstractField::TYPE_ACTION_COLLECTION => ActionCollectionField::class,
131 4
            AbstractField::TYPE_LINK => LinkField::class,
132 4
            AbstractField::TYPE_DATE => DateField::class,
133 4
            AbstractField::TYPE_COUNT => CountField::class,
134 4
            AbstractField::TYPE_AUTO => AutoField::class,
135
        ];
136
137 4
        $resolver->setDefault('fields_mapping', $defaultMapping);
138 4
        $resolver->setAllowedTypes('fields_mapping', 'array');
139
        $resolver->setNormalizer('fields_mapping', function (Options $options, $value) use ($defaultMapping) {
140
            // Merge with default mapping to allow override
141 4
            $value = array_merge($defaultMapping, $value);
142
143 4
            return $value;
144 4
        });
145
146
        // Fields templates mapping
147
        $defaultMapping = [
148 4
            AbstractField::TYPE_LINK => 'LAGAdminBundle:Render:link.html.twig',
149
        ];
150
151 4
        $resolver->setDefault('fields_template_mapping', $defaultMapping);
152 4
        $resolver->setAllowedTypes('fields_template_mapping', 'array');
153
        $resolver->setNormalizer('fields_template_mapping', function (Options $options, $value) use ($defaultMapping) {
154
            // Merge with default mapping to allow override
155 4
            $value = array_merge($defaultMapping, $value);
156
157 4
            return $value;
158 4
        });
159 4
    }
160
}
161