Completed
Branch master (01610a)
by Arnaud
06:29
created

ActionConfiguration::configureOptions()   D

Complexity

Conditions 15
Paths 1

Size

Total Lines 201
Code Lines 116

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 84
CRAP Score 17.4683

Importance

Changes 5
Bugs 1 Features 2
Metric Value
c 5
b 1
f 2
dl 0
loc 201
ccs 84
cts 108
cp 0.7778
rs 4.9121
cc 15
eloc 116
nc 1
nop 1
crap 17.4683

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