Completed
Push — dev ( 08fefa...c47c46 )
by Arnaud
03:12
created

ActionConfiguration::configureOptions()   D

Complexity

Conditions 15
Paths 1

Size

Total Lines 202
Code Lines 116

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 83
CRAP Score 17.7914

Importance

Changes 0
Metric Value
dl 0
loc 202
ccs 83
cts 108
cp 0.7685
rs 4.9121
c 0
b 0
f 0
cc 15
eloc 116
nc 1
nop 1
crap 17.7914

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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