Completed
Pull Request — dev (#25)
by Arnaud
16:09
created

ActionConfiguration   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 230
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 94.12%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 16
c 4
b 1
f 2
lcom 1
cbo 8
dl 0
loc 230
ccs 64
cts 68
cp 0.9412
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
D configureOptions() 0 192 15
1
<?php
2
3
namespace LAG\AdminBundle\Action\Configuration;
4
5
use LAG\AdminBundle\Admin\AdminInterface;
6
use LAG\AdminBundle\Admin\Behaviors\TranslationKeyTrait;
7
use LAG\AdminBundle\Configuration\Configuration;
8
use LAG\AdminBundle\Menu\Configuration\MenuConfiguration;
9
use Symfony\Component\DependencyInjection\Container;
10
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
11
use Symfony\Component\OptionsResolver\Options;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
14
class ActionConfiguration extends Configuration
15
{
16
    use TranslationKeyTrait;
17
18
    /**
19
     * Related Action name.
20
     *
21
     * @var string
22
     */
23
    protected $actionName;
24
25
    /**
26
     * Related Admin (optional)
27
     *
28
     * @var AdminInterface
29
     */
30
    protected $admin = null;
31
32
    /**
33
     * ActionConfiguration constructor.
34 8
     *
35
     * @param $actionName
36 8
     * @param AdminInterface $admin
37
     */
38 8
    public function __construct($actionName, AdminInterface $admin)
39 8
    {
40 8
        parent::__construct();
41
42
        $this->actionName = $actionName;
43
        $this->admin = $admin;
44
    }
45
46
    /**
47 7
     * Define allowed parameters and values for this configuration, using optionsResolver component.
48
     *
49
     * @param OptionsResolver $resolver
50
     */
51 7
    public function configureOptions(OptionsResolver $resolver)
52 7
    {
53
        // action title, default to action's name
54
        $resolver
55
            ->setDefault('title', Container::camelize($this->actionName))
56 7
            ->setAllowedTypes('title', 'string');
57
58 7
        // displayed fields for this action
59 7
        $resolver
60
            ->setDefault('fields', [
61 7
                'id' => []
62
            ])
63 7
            ->setAllowedTypes('fields', 'array')
64
            ->setNormalizer('fields', function(Options $options, $fields) {
65 7
                $normalizedFields = [];
66
67
                foreach ($fields as $name => $field) {
68
69 7
                    if ($field === null) {
70
                        $field = [];
71
                    }
72 7
73 7
                    $normalizedFields[$name] = $field;
74
                }
75
76
                return $normalizedFields;
77
            })
78 7
        ;
79
80 7
        // action permissions. By default, only admin are allowed
81
        $resolver
82
            ->setDefault('permissions', [
83
                'ROLE_ADMIN'
84 7
            ]);
85 7
86
        // by default, all exports type are allowed
87
        $resolver
88
            ->setDefault('export', [
89
                'json',
90
                'html',
91
                'csv',
92
                'xls'
93 7
            ]);
94 7
95
        // entity will be retrived with this order. It should be an array of field/order mapping
96
        $resolver
97
            ->setDefault('order', [])
98 7
            ->setAllowedTypes('order', 'array');
99 7
100
        // the action route should be a string
101 7
        $resolver
102
            ->setDefault('route', '')
103 7
            ->setAllowedTypes('route', 'string')
104
            ->setNormalizer('route', function (Options $options, $value) {
105
                if (!$value) {
106
                    // if no route was provided, it should be linked to an Admin
107
                    if (!$this->admin) {
108
                        throw new InvalidOptionsException('No route was provided for action : ' . $this->actionName);
109 7
                    }
110 7
111
                    // generate default route from admin
112
                    return $this
113 1
                        ->admin
114 7
                        ->generateRouteName($this->actionName);
115
                }
116
117
                return $value;
118 7
            });
119 7
120
        // action parameters should be an array
121
        $resolver
122
            ->setDefault('route_parameters', [])
123 7
            ->setAllowedTypes('route_parameters', 'array');
124 7
125
        // font awesome icons
126
        $resolver
127
            ->setDefault('icon', '')
128 7
            ->setAllowedTypes('icon', 'string');
129 7
130 7
        // load strategy : determine which method should be called in the data provider
131 7
        $resolver
132 7
            ->setDefault('load_strategy', null)
133
            ->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_NONE)
134
            ->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_UNIQUE)
135 7
            ->addAllowedValues('load_strategy', AdminInterface::LOAD_STRATEGY_MULTIPLE)
136 4
            ->addAllowedValues('load_strategy', null)
137 1
            ->setNormalizer('load_strategy', function (Options $options, $value) {
138 4
139
                if (!$value) {
140
                    if ($this->actionName == 'create') {
141 4
                        $value = AdminInterface::LOAD_STRATEGY_NONE;
142
                    } else if ($this->actionName == 'list') {
143
                        $value = AdminInterface::LOAD_STRATEGY_MULTIPLE;
144
                    } else {
145 7
                        $value = AdminInterface::LOAD_STRATEGY_UNIQUE;
146 7
                    }
147
                }
148
149
                return $value;
150 7
            });
151 7
152 7
        // pagination configuration
153
        $resolver
154
            ->setDefault('pager', 'pagerfanta')
155
            ->addAllowedValues('pager', 'pagerfanta')
156
            ->addAllowedValues('pager', false)
157 7
        ;
158
159
        // criteria used to find entity in the data provider
160 7
        $resolver
161
            ->setDefault('criteria', [])
162 7
            ->setNormalizer('criteria', function (Options $options, $value) {
163
164
                if (!$value) {
165
                    $idActions = [
166 7
                        'edit',
167
                        'delete'
168
                    ];
169 1
170
                    if (in_array($this->actionName, $idActions)) {
171
                        $value = [
172
                            'id'
173 7
                        ];
174 7
                    }
175
                }
176
177
                return $value;
178 7
            })
179
        ;
180
181
        // filters
182 7
        $resolver->setDefault('filters', []);
183 7
184
        // menus
185 7
        $resolver
186
            ->setDefault('menus', [])
187
            ->setNormalizer('menus', function (Options $options, $menus) {
188
                // set default to an array
189 7
                if ($menus === false) {
190 7
                    $menus = [];
191
                }
192 7
193
                return $menus;
194
            })
195
        ;
196
197
        // batch actions
198
        $resolver
199
            // by default, the batch actions is desactivated
200
            ->setDefault('batch', null)
201
            ->setNormalizer('batch', function(Options $options, $batch) {
202
203
                // if batch is desactivated, no more checks should be done
204
                if ($batch === false) {
205
                    return $batch;
206
                }
207
                // for list actions, we add a default configuration
208
                if ($batch === null) {
209
                    // delete action should be allowed in order to be place in batch actions
210
                    $allowedActions = array_keys($this
211
                        ->admin
212
                        ->getConfiguration()
213
                        ->getParameter('actions'));
214
215
                    if ($this->actionName == 'list' && in_array('delete', $allowedActions)) {
216
                        $pattern = $this
217
                            ->admin
218
                            ->getConfiguration()
219
                            ->getParameter('translation_pattern')['pattern'];
220
221
                        $batch = [
222
                            'items' => [
223
                                'delete' => [
224
                                    'admin' => $this->admin->getName(),
225
                                    'action' => 'delete',
226
                                    'text' => $this->getTranslationKey($pattern, 'delete', $this->admin->getName())
227
                                ]
228
                            ]
229
                        ];
230
                    } else {
231
                        return $batch;
232
                    }
233
                }
234
                $resolver = new OptionsResolver();
235
                $configuration = new MenuConfiguration();
236
                $configuration->configureOptions($resolver);
237
                $batch = $resolver->resolve($batch);
238
239
                return $batch;
240
            })
241
        ;
242
    }
243
}
244