Total Complexity | 50 |
Total Lines | 425 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 1 | Features | 0 |
Complex classes like ActionConfiguration often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ActionConfiguration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class ActionConfiguration extends Configuration |
||
18 | { |
||
19 | /** |
||
20 | * Related Action name. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $actionName; |
||
25 | |||
26 | /** |
||
27 | * @var AdminConfiguration |
||
28 | */ |
||
29 | protected $adminConfiguration; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $adminName; |
||
35 | |||
36 | /** |
||
37 | * ActionConfiguration constructor. |
||
38 | */ |
||
39 | public function __construct(string $actionName, string $adminName, AdminConfiguration $adminConfiguration) |
||
40 | { |
||
41 | parent::__construct(); |
||
42 | |||
43 | $this->actionName = $actionName; |
||
44 | $this->adminConfiguration = $adminConfiguration; |
||
45 | $this->adminName = $adminName; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Define allowed parameters and values for this configuration, using optionsResolver component. |
||
50 | */ |
||
51 | public function configureOptions(OptionsResolver $resolver) |
||
52 | { |
||
53 | $resolver |
||
54 | ->setDefaults([ |
||
55 | 'title' => null, |
||
56 | 'class' => Action::class, |
||
57 | 'fields' => [], |
||
58 | 'permissions' => [ |
||
59 | 'ROLE_ADMIN', |
||
60 | ], |
||
61 | 'export' => [], |
||
62 | 'order' => [], |
||
63 | 'icon' => null, |
||
64 | 'load_strategy' => LAGAdminBundle::LOAD_STRATEGY_NONE, |
||
65 | 'pager' => $this->adminConfiguration->get('pager'), |
||
66 | 'page_parameter' => $this->adminConfiguration->get('page_parameter'), |
||
67 | 'max_per_page' => 30, |
||
68 | 'criteria' => [], |
||
69 | 'filters' => [], |
||
70 | 'template' => $this->getDefaultTemplate(), |
||
71 | 'sortable' => false, |
||
72 | 'string_length' => $this->adminConfiguration->get('string_length'), |
||
73 | 'string_length_truncate' => $this->adminConfiguration->get('string_length_truncate'), |
||
74 | 'date_format' => $this->adminConfiguration->get('date_format'), |
||
75 | 'use_form' => true, |
||
76 | 'form' => null, |
||
77 | ]) |
||
78 | ->setAllowedTypes('title', [ |
||
79 | 'string', |
||
80 | 'null', |
||
81 | ]) |
||
82 | ->setAllowedTypes('class', 'string') |
||
83 | ->setAllowedTypes('fields', 'array') |
||
84 | ->setAllowedTypes('permissions', 'array') |
||
85 | ->setAllowedTypes('export', 'array') |
||
86 | ->setAllowedTypes('order', 'array') |
||
87 | ->setAllowedTypes('string_length', 'integer') |
||
88 | ->setAllowedTypes('string_length_truncate', 'string') |
||
89 | ->setNormalizer('fields', $this->getFieldsNormalizer()) |
||
90 | ->setNormalizer('order', $this->getOrderNormalizer()) |
||
91 | ->setNormalizer('load_strategy', $this->getLoadStrategyNormalizer()) |
||
92 | ->setNormalizer('criteria', $this->getCriteriaNormalizer()) |
||
93 | ->setNormalizer('filters', $this->getFiltersNormalizer()) |
||
94 | ->setNormalizer('form', $this->getFormNormalizer()) |
||
95 | ->setNormalizer('title', $this->getTitleNormalizer()) |
||
96 | ->setAllowedValues('load_strategy', [ |
||
97 | LAGAdminBundle::LOAD_STRATEGY_NONE, |
||
98 | LAGAdminBundle::LOAD_STRATEGY_UNIQUE, |
||
99 | LAGAdminBundle::LOAD_STRATEGY_MULTIPLE, |
||
100 | ]) |
||
101 | ->setAllowedValues('pager', [ |
||
102 | 'pagerfanta', |
||
103 | false, |
||
104 | null, |
||
105 | ]) |
||
106 | ; |
||
107 | |||
108 | $this->configureMenu($resolver); |
||
109 | $this->configureRepository($resolver); |
||
110 | $this->configureRouting($resolver); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Generate an admin route name using the pattern in the configuration. |
||
115 | * |
||
116 | * @throws Exception |
||
117 | */ |
||
118 | protected function generateRouteName(): string |
||
119 | { |
||
120 | if (!array_key_exists($this->actionName, $this->adminConfiguration->get('actions'))) { |
||
121 | throw new Exception(sprintf('Invalid action name %s for admin %s (available action are: %s)', $this->actionName, $this->adminName, implode(', ', array_keys($this->adminConfiguration->get('actions'))))); |
||
122 | } |
||
123 | |||
124 | return RoutingLoader::generateRouteName( |
||
125 | $this->adminName, |
||
126 | $this->actionName, |
||
127 | $this->adminConfiguration->get('routing_name_pattern') |
||
128 | ); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Return the field normalizer. It will transform null configuration into array to allow field type guessing |
||
133 | * working. |
||
134 | * |
||
135 | * @return Closure |
||
136 | */ |
||
137 | protected function getFieldsNormalizer() |
||
138 | { |
||
139 | return function (Options $options, $fields) { |
||
140 | $normalizedFields = []; |
||
141 | |||
142 | foreach ($fields as $name => $field) { |
||
143 | if (null === $field) { |
||
144 | $field = []; |
||
145 | } |
||
146 | |||
147 | $normalizedFields[$name] = $field; |
||
148 | } |
||
149 | |||
150 | return $normalizedFields; |
||
151 | }; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Return the order normalizer. It will check if the order value passed are valid. |
||
156 | * |
||
157 | * @return Closure |
||
158 | */ |
||
159 | protected function getOrderNormalizer() |
||
160 | { |
||
161 | return function (Options $options, $order) { |
||
162 | foreach ($order as $field => $sort) { |
||
163 | if (!is_string($sort) || !is_string($field) || !in_array(strtolower($sort), ['asc', 'desc'])) { |
||
164 | throw new Exception('Order value should be an array of string (["field" => $key]), got '.gettype($sort)); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | return $order; |
||
169 | }; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Return the load strategy normalizer. It will set the default strategy according to the action name, if no value |
||
174 | * is provided. |
||
175 | * |
||
176 | * @return Closure |
||
177 | */ |
||
178 | protected function getLoadStrategyNormalizer() |
||
179 | { |
||
180 | return function (Options $options, $value) { |
||
181 | if (!$value) { |
||
182 | if ('create' == $this->actionName) { |
||
183 | $value = LAGAdminBundle::LOAD_STRATEGY_NONE; |
||
184 | } elseif ('list' == $this->actionName) { |
||
185 | $value = LAGAdminBundle::LOAD_STRATEGY_MULTIPLE; |
||
186 | } else { |
||
187 | $value = LAGAdminBundle::LOAD_STRATEGY_UNIQUE; |
||
188 | } |
||
189 | } |
||
190 | |||
191 | return $value; |
||
192 | }; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Return the criteria normalizer. It will add the id parameters for the edit and delete actions if no value is |
||
197 | * provided. |
||
198 | * |
||
199 | * @return Closure |
||
200 | */ |
||
201 | protected function getCriteriaNormalizer() |
||
202 | { |
||
203 | return function (Options $options, $value) { |
||
204 | if (!$value) { |
||
205 | $idActions = [ |
||
206 | 'edit', |
||
207 | 'delete', |
||
208 | ]; |
||
209 | |||
210 | if (in_array($this->actionName, $idActions)) { |
||
211 | $value = [ |
||
212 | 'id', |
||
213 | ]; |
||
214 | } |
||
215 | } |
||
216 | |||
217 | return $value; |
||
218 | }; |
||
219 | } |
||
220 | |||
221 | public function getAdminName(): string |
||
222 | { |
||
223 | return $this->adminName; |
||
224 | } |
||
225 | |||
226 | public function getActionName(): string |
||
229 | } |
||
230 | |||
231 | public function getAdminConfiguration(): AdminConfiguration |
||
232 | { |
||
233 | return $this->adminConfiguration; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Return the filters normalizer. |
||
238 | */ |
||
239 | protected function getFiltersNormalizer(): Closure |
||
240 | { |
||
241 | return function (Options $options, $data) { |
||
242 | $normalizedData = []; |
||
243 | |||
244 | foreach ($data as $name => $field) { |
||
245 | if (is_string($field)) { |
||
246 | $field = [ |
||
247 | 'type' => $field, |
||
248 | 'options' => [], |
||
249 | ]; |
||
250 | } |
||
251 | $field['name'] = $name; |
||
252 | |||
253 | $resolver = new OptionsResolver(); |
||
254 | $filterConfiguration = new FilterConfiguration(); |
||
255 | $filterConfiguration->configureOptions($resolver); |
||
256 | $filterConfiguration->setParameters($resolver->resolve($field)); |
||
257 | |||
258 | $normalizedData[$name] = $filterConfiguration->getParameters(); |
||
259 | } |
||
260 | |||
261 | return $normalizedData; |
||
262 | }; |
||
263 | } |
||
264 | |||
265 | protected function getFormNormalizer(): Closure |
||
282 | }; |
||
283 | } |
||
284 | |||
285 | protected function getTitleNormalizer(): Closure |
||
286 | { |
||
287 | return function (Options $options, $value) { |
||
288 | // If the translation system is not used, return the provided value as is |
||
289 | if (!$this->adminConfiguration->isTranslationEnabled()) { |
||
290 | if (null === $value) { |
||
291 | return StringUtils::camelize($this->actionName); |
||
292 | } |
||
293 | |||
294 | return $value; |
||
295 | } |
||
296 | |||
297 | // If a value is defined, we should return the value |
||
298 | if (null !== $value) { |
||
299 | return $value; |
||
300 | } |
||
301 | // By default, the action title is action name using the configured translation pattern |
||
302 | $value = TranslationUtils::getTranslationKey( |
||
303 | $this->adminConfiguration->getTranslationPattern(), |
||
304 | $this->adminName, |
||
305 | $this->actionName |
||
306 | ); |
||
307 | |||
308 | return $value; |
||
309 | }; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Return the default route path according to the action name. |
||
314 | */ |
||
315 | protected function getDefaultRoutePath(): string |
||
316 | { |
||
317 | $pattern = $this |
||
318 | ->adminConfiguration |
||
319 | ->get('routing_url_pattern') |
||
320 | ; |
||
321 | $path = str_replace('{admin}', $this->adminName, $pattern); |
||
322 | $path = str_replace('{action}', $this->actionName, $path); |
||
323 | |||
324 | if (in_array($this->actionName, ['edit', 'delete'])) { |
||
325 | $path .= '/{id}'; |
||
326 | } |
||
327 | |||
328 | return $path; |
||
329 | } |
||
330 | |||
331 | protected function getDefaultTemplate(): ?string |
||
345 | } |
||
346 | |||
347 | protected function isActionInMapping(array $mapping): bool |
||
348 | { |
||
349 | return array_key_exists($this->actionName, $mapping); |
||
350 | } |
||
351 | |||
352 | protected function configureMenu(OptionsResolver $resolver): void |
||
353 | { |
||
354 | $resolver |
||
355 | ->setDefaults([ |
||
356 | 'add_return' => true, |
||
357 | 'menus' => [], |
||
358 | ]) |
||
359 | ->setAllowedTypes('add_return', 'boolean') |
||
360 | ->setNormalizer('menus', function (Options $options, $value) { |
||
361 | if (false === $value) { |
||
362 | return $value; |
||
363 | } |
||
364 | |||
365 | if (!is_array($value)) { |
||
366 | $value = []; |
||
367 | } |
||
368 | |||
369 | if (!key_exists('top', $value)) { |
||
370 | $value['top'] = []; |
||
371 | } |
||
372 | |||
373 | if (!key_exists('items', $value['top'])) { |
||
374 | $value['top']['items'] = []; |
||
375 | } |
||
376 | |||
377 | // Auto return button should be optional |
||
378 | if ($options->offsetGet('add_return')) { |
||
379 | $text = ucfirst('Return'); |
||
380 | |||
381 | if ($this->adminConfiguration->isTranslationEnabled()) { |
||
382 | $text = TranslationUtils::getTranslationKey( |
||
383 | $this->adminConfiguration->getTranslationPattern(), |
||
384 | $this->adminName, |
||
385 | 'return' |
||
386 | ); |
||
387 | } |
||
388 | |||
389 | // Use array_unshift() to put the items in first in menu |
||
390 | array_unshift($value['top']['items'], [ |
||
391 | 'admin' => $this->adminName, |
||
392 | 'action' => 'list', |
||
393 | // Do not use the translation trait as the action configuration is not configured yet, so |
||
394 | // translation parameters are not available |
||
395 | 'text' => $text, |
||
396 | 'icon' => 'arrow-left', |
||
397 | ]); |
||
398 | } |
||
399 | |||
400 | return $value; |
||
401 | }) |
||
402 | ; |
||
403 | } |
||
404 | |||
405 | protected function configureRepository(OptionsResolver $resolver): void |
||
410 | ]) |
||
411 | ; |
||
412 | } |
||
413 | |||
414 | protected function configureRouting(OptionsResolver $resolver): void |
||
415 | { |
||
416 | $resolver |
||
442 | }) |
||
443 | ; |
||
444 | } |
||
445 | } |
||
446 |