Total Complexity | 50 |
Total Lines | 626 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Standard 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 Standard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Standard |
||
23 | extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base |
||
24 | implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface |
||
25 | { |
||
26 | /** |
||
27 | * Copies a resource |
||
28 | * |
||
29 | * @return string HTML output |
||
30 | */ |
||
31 | public function copy() |
||
32 | { |
||
33 | $view = $this->getView(); |
||
34 | $context = $this->getContext(); |
||
35 | |||
36 | try |
||
37 | { |
||
38 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
39 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
40 | } |
||
41 | |||
42 | $manager = \Aimeos\MShop::create( $context, 'plugin' ); |
||
43 | |||
44 | $view->item = $manager->getItem( $id ); |
||
45 | $view->itemData = $this->toArray( $view->item, true ); |
||
46 | $view->itemSubparts = $this->getSubClientNames(); |
||
47 | $view->itemProviders = $this->getProviderNames(); |
||
48 | $view->itemDecorators = $this->getDecoratorNames(); |
||
49 | $view->itemAttributes = $this->getConfigAttributes( $view->item ); |
||
50 | $view->itemTypes = $this->getTypeItems(); |
||
51 | $view->itemBody = ''; |
||
52 | |||
53 | foreach( $this->getSubClients() as $idx => $client ) |
||
54 | { |
||
55 | $view->tabindex = ++$idx + 1; |
||
56 | $view->itemBody .= $client->copy(); |
||
57 | } |
||
58 | } |
||
59 | catch( \Aimeos\MShop\Exception $e ) |
||
60 | { |
||
61 | $error = array( 'plugin-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
62 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
63 | $this->logException( $e ); |
||
64 | } |
||
65 | catch( \Exception $e ) |
||
66 | { |
||
67 | $error = array( 'plugin-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
68 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
69 | $this->logException( $e ); |
||
70 | } |
||
71 | |||
72 | return $this->render( $view ); |
||
73 | } |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Creates a new resource |
||
78 | * |
||
79 | * @return string HTML output |
||
80 | */ |
||
81 | public function create() |
||
123 | } |
||
124 | |||
125 | |||
126 | /** |
||
127 | * Deletes a resource |
||
128 | * |
||
129 | * @return string|null HTML output |
||
130 | */ |
||
131 | public function delete() |
||
132 | { |
||
133 | $view = $this->getView(); |
||
134 | $context = $this->getContext(); |
||
135 | |||
136 | $manager = \Aimeos\MShop::create( $context, 'plugin' ); |
||
137 | $manager->begin(); |
||
138 | |||
139 | try |
||
140 | { |
||
141 | if( ( $ids = $view->param( 'id' ) ) === null ) { |
||
142 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
143 | } |
||
144 | |||
145 | $search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) ); |
||
146 | $search->setConditions( $search->compare( '==', 'plugin.id', $ids ) ); |
||
147 | $items = $manager->searchItems( $search ); |
||
148 | |||
149 | foreach( $items as $item ) |
||
150 | { |
||
151 | $view->item = $item; |
||
152 | |||
153 | foreach( $this->getSubClients() as $client ) { |
||
154 | $client->delete(); |
||
155 | } |
||
156 | |||
157 | $manager->saveItem( $view->item ); |
||
158 | } |
||
159 | |||
160 | $manager->deleteItems( array_keys( $items ) ); |
||
161 | $manager->commit(); |
||
162 | |||
163 | $this->nextAction( $view, 'search', 'plugin', null, 'delete' ); |
||
164 | return; |
||
165 | } |
||
166 | catch( \Aimeos\MShop\Exception $e ) |
||
167 | { |
||
168 | $error = array( 'plugin-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
169 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
170 | $this->logException( $e ); |
||
171 | } |
||
172 | catch( \Exception $e ) |
||
173 | { |
||
174 | $error = array( 'plugin-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
175 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
176 | $this->logException( $e ); |
||
177 | } |
||
178 | |||
179 | $manager->rollback(); |
||
180 | |||
181 | return $this->search(); |
||
182 | } |
||
183 | |||
184 | |||
185 | /** |
||
186 | * Returns a single resource |
||
187 | * |
||
188 | * @return string HTML output |
||
189 | */ |
||
190 | public function get() |
||
232 | } |
||
233 | |||
234 | |||
235 | /** |
||
236 | * Saves the data |
||
237 | * |
||
238 | * @return string HTML output |
||
239 | */ |
||
240 | public function save() |
||
241 | { |
||
242 | $view = $this->getView(); |
||
243 | $context = $this->getContext(); |
||
244 | |||
245 | $manager = \Aimeos\MShop::create( $context, 'plugin' ); |
||
246 | $manager->begin(); |
||
247 | |||
248 | try |
||
249 | { |
||
250 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
251 | $view->item = $item->getId() ? $item : $manager->saveItem( $item ); |
||
252 | $view->itemBody = ''; |
||
253 | |||
254 | foreach( $this->getSubClients() as $client ) { |
||
255 | $view->itemBody .= $client->save(); |
||
256 | } |
||
257 | |||
258 | $manager->saveItem( clone $view->item ); |
||
259 | $manager->commit(); |
||
260 | |||
261 | $this->nextAction( $view, $view->param( 'next' ), 'plugin', $view->item->getId(), 'save' ); |
||
262 | return; |
||
263 | } |
||
264 | catch( \Aimeos\Admin\JQAdm\Exception $e ) |
||
265 | { |
||
266 | // fall through to create |
||
267 | } |
||
268 | catch( \Aimeos\MShop\Exception $e ) |
||
269 | { |
||
270 | $error = array( 'plugin-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
271 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
272 | $this->logException( $e ); |
||
273 | } |
||
274 | catch( \Exception $e ) |
||
275 | { |
||
276 | $error = array( 'plugin-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
277 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
278 | $this->logException( $e ); |
||
279 | } |
||
280 | |||
281 | $manager->rollback(); |
||
282 | |||
283 | return $this->create(); |
||
284 | } |
||
285 | |||
286 | |||
287 | /** |
||
288 | * Returns a list of resource according to the conditions |
||
289 | * |
||
290 | * @return string HTML output |
||
291 | */ |
||
292 | public function search() |
||
293 | { |
||
294 | $view = $this->getView(); |
||
295 | $context = $this->getContext(); |
||
296 | |||
297 | try |
||
298 | { |
||
299 | $total = 0; |
||
300 | $params = $this->storeSearchParams( $view->param(), 'plugin' ); |
||
301 | $manager = \Aimeos\MShop::create( $context, 'plugin' ); |
||
302 | |||
303 | $search = $manager->createSearch(); |
||
304 | $search->setSortations( [$search->sort( '+', 'plugin.type' ), $search->sort( '+', 'plugin.position' )] ); |
||
305 | $search = $this->initCriteria( $search, $params ); |
||
306 | |||
307 | $view->items = $manager->searchItems( $search, [], $total ); |
||
308 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
309 | $view->filterOperators = $search->getOperators(); |
||
310 | $view->itemTypes = $this->getTypeItems(); |
||
311 | $view->total = $total; |
||
312 | $view->itemBody = ''; |
||
313 | |||
314 | foreach( $this->getSubClients() as $client ) { |
||
315 | $view->itemBody .= $client->search(); |
||
316 | } |
||
317 | } |
||
318 | catch( \Aimeos\MShop\Exception $e ) |
||
319 | { |
||
320 | $error = array( 'plugin-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
321 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
322 | $this->logException( $e ); |
||
323 | } |
||
324 | catch( \Exception $e ) |
||
325 | { |
||
326 | $error = array( 'plugin-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
327 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
328 | $this->logException( $e ); |
||
329 | } |
||
330 | |||
331 | /** admin/jqadm/plugin/template-list |
||
332 | * Relative path to the HTML body template for the plugin list. |
||
333 | * |
||
334 | * The template file contains the HTML code and processing instructions |
||
335 | * to generate the result shown in the body of the frontend. The |
||
336 | * configuration string is the path to the template file relative |
||
337 | * to the templates directory (usually in admin/jqadm/templates). |
||
338 | * |
||
339 | * You can overwrite the template file configuration in extensions and |
||
340 | * provide alternative templates. These alternative templates should be |
||
341 | * named like the default one but with the string "default" replaced by |
||
342 | * an unique name. You may use the name of your project for this. If |
||
343 | * you've implemented an alternative client class as well, "default" |
||
344 | * should be replaced by the name of the new class. |
||
345 | * |
||
346 | * @param string Relative path to the template creating the HTML code |
||
347 | * @since 2016.04 |
||
348 | * @category Developer |
||
349 | */ |
||
350 | $tplconf = 'admin/jqadm/plugin/template-list'; |
||
351 | $default = 'plugin/list-standard'; |
||
352 | |||
353 | return $view->render( $view->config( $tplconf, $default ) ); |
||
354 | } |
||
355 | |||
356 | |||
357 | /** |
||
358 | * Returns the sub-client given by its name. |
||
359 | * |
||
360 | * @param string $type Name of the client type |
||
361 | * @param string|null $name Name of the sub-client (Default if null) |
||
362 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
363 | */ |
||
364 | public function getSubClient( $type, $name = null ) |
||
365 | { |
||
366 | /** admin/jqadm/plugin/decorators/excludes |
||
367 | * Excludes decorators added by the "common" option from the plugin JQAdm client |
||
368 | * |
||
369 | * Decorators extend the functionality of a class by adding new aspects |
||
370 | * (e.g. log what is currently done), executing the methods of the underlying |
||
371 | * class only in certain conditions (e.g. only for logged in users) or |
||
372 | * modify what is returned to the caller. |
||
373 | * |
||
374 | * This option allows you to remove a decorator added via |
||
375 | * "client/jqadm/common/decorators/default" before they are wrapped |
||
376 | * around the JQAdm client. |
||
377 | * |
||
378 | * admin/jqadm/plugin/decorators/excludes = array( 'decorator1' ) |
||
379 | * |
||
380 | * This would remove the decorator named "decorator1" from the list of |
||
381 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
382 | * "client/jqadm/common/decorators/default" to the JQAdm client. |
||
383 | * |
||
384 | * @param array List of decorator names |
||
385 | * @since 2017.10 |
||
386 | * @category Developer |
||
387 | * @see admin/jqadm/common/decorators/default |
||
388 | * @see admin/jqadm/plugin/decorators/global |
||
389 | * @see admin/jqadm/plugin/decorators/local |
||
390 | */ |
||
391 | |||
392 | /** admin/jqadm/plugin/decorators/global |
||
393 | * Adds a list of globally available decorators only to the plugin JQAdm client |
||
394 | * |
||
395 | * Decorators extend the functionality of a class by adding new aspects |
||
396 | * (e.g. log what is currently done), executing the methods of the underlying |
||
397 | * class only in certain conditions (e.g. only for logged in users) or |
||
398 | * modify what is returned to the caller. |
||
399 | * |
||
400 | * This option allows you to wrap global decorators |
||
401 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
402 | * |
||
403 | * admin/jqadm/plugin/decorators/global = array( 'decorator1' ) |
||
404 | * |
||
405 | * This would add the decorator named "decorator1" defined by |
||
406 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
407 | * |
||
408 | * @param array List of decorator names |
||
409 | * @since 2017.10 |
||
410 | * @category Developer |
||
411 | * @see admin/jqadm/common/decorators/default |
||
412 | * @see admin/jqadm/plugin/decorators/excludes |
||
413 | * @see admin/jqadm/plugin/decorators/local |
||
414 | */ |
||
415 | |||
416 | /** admin/jqadm/plugin/decorators/local |
||
417 | * Adds a list of local decorators only to the plugin JQAdm client |
||
418 | * |
||
419 | * Decorators extend the functionality of a class by adding new aspects |
||
420 | * (e.g. log what is currently done), executing the methods of the underlying |
||
421 | * class only in certain conditions (e.g. only for logged in users) or |
||
422 | * modify what is returned to the caller. |
||
423 | * |
||
424 | * This option allows you to wrap local decorators |
||
425 | * ("\Aimeos\Admin\JQAdm\Plugin\Decorator\*") around the JQAdm client. |
||
426 | * |
||
427 | * admin/jqadm/plugin/decorators/local = array( 'decorator2' ) |
||
428 | * |
||
429 | * This would add the decorator named "decorator2" defined by |
||
430 | * "\Aimeos\Admin\JQAdm\Plugin\Decorator\Decorator2" only to the JQAdm client. |
||
431 | * |
||
432 | * @param array List of decorator names |
||
433 | * @since 2017.10 |
||
434 | * @category Developer |
||
435 | * @see admin/jqadm/common/decorators/default |
||
436 | * @see admin/jqadm/plugin/decorators/excludes |
||
437 | * @see admin/jqadm/plugin/decorators/global |
||
438 | */ |
||
439 | return $this->createSubClient( 'plugin/' . $type, $name ); |
||
440 | } |
||
441 | |||
442 | |||
443 | /** |
||
444 | * Returns the backend configuration attributes of the provider and decorators |
||
445 | * |
||
446 | * @param \Aimeos\MShop\Plugin\Item\Iface $item Plugin item incl. provider/decorator property |
||
447 | * @return \Aimeos\MW\Common\Critera\Attribute\Iface[] List of configuration attributes |
||
448 | */ |
||
449 | public function getConfigAttributes( \Aimeos\MShop\Plugin\Item\Iface $item ) |
||
450 | { |
||
451 | $manager = \Aimeos\MShop::create( $this->getContext(), 'plugin' ); |
||
452 | |||
453 | try { |
||
454 | return $manager->getProvider( $item, $item->getType() )->getConfigBE(); |
||
455 | } catch( \Aimeos\MShop\Exception $e ) { |
||
456 | return []; |
||
457 | } |
||
458 | } |
||
459 | |||
460 | |||
461 | /** |
||
462 | * Returns the names of the available plugin decorators |
||
463 | * |
||
464 | * @return string[] List of decorator class names |
||
465 | */ |
||
466 | protected function getDecoratorNames() |
||
467 | { |
||
468 | $ds = DIRECTORY_SEPARATOR; |
||
469 | return $this->getClassNames( 'MShop' . $ds . 'Plugin' . $ds . 'Provider' . $ds . 'Decorator' ); |
||
470 | } |
||
471 | |||
472 | |||
473 | /** |
||
474 | * Returns the names of the available plugin providers |
||
475 | * |
||
476 | * @return string[] List of provider class names |
||
477 | */ |
||
478 | protected function getProviderNames() |
||
479 | { |
||
480 | $ds = DIRECTORY_SEPARATOR; |
||
481 | return [ |
||
482 | 'order' => $this->getClassNames( 'MShop' . $ds . 'Plugin' . $ds . 'Provider' . $ds . 'Order' ) |
||
483 | ]; |
||
484 | } |
||
485 | |||
486 | |||
487 | /** |
||
488 | * Returns the list of sub-client names configured for the client. |
||
489 | * |
||
490 | * @return array List of JQAdm client names |
||
491 | */ |
||
492 | protected function getSubClientNames() |
||
528 | } |
||
529 | |||
530 | |||
531 | /** |
||
532 | * Returns the available plugin type items |
||
533 | * |
||
534 | * @return array List of item implementing \Aimeos\MShop\Common\Type\Iface |
||
535 | */ |
||
536 | protected function getTypeItems() |
||
537 | { |
||
538 | $typeManager = \Aimeos\MShop::create( $this->getContext(), 'plugin/type' ); |
||
539 | |||
540 | $search = $typeManager->createSearch( true )->setSlice( 0, 10000 ); |
||
541 | $search->setSortations( [$search->sort( '+', 'plugin.type.position' )] ); |
||
542 | |||
543 | return $this->map( $typeManager->searchItems( $search ) ); |
||
544 | } |
||
545 | |||
546 | |||
547 | /** |
||
548 | * Creates new and updates existing items using the data array |
||
549 | * |
||
550 | * @param array $data Data array |
||
551 | * @return \Aimeos\MShop\Plugin\Item\Iface New plugin item object |
||
552 | */ |
||
553 | protected function fromArray( array $data ) |
||
554 | { |
||
555 | $conf = []; |
||
556 | |||
557 | if( isset( $data['config']['key'] ) ) |
||
558 | { |
||
559 | foreach( (array) $data['config']['key'] as $idx => $key ) |
||
560 | { |
||
561 | if( trim( $key ) !== '' && isset( $data['config']['val'][$idx] ) ) |
||
562 | { |
||
563 | if( ( $val = json_decode( $data['config']['val'][$idx] ) ) === null ) { |
||
564 | $conf[$key] = $data['config']['val'][$idx]; |
||
565 | } else { |
||
566 | $conf[$key] = $val; |
||
567 | } |
||
568 | } |
||
569 | } |
||
570 | } |
||
571 | |||
572 | $manager = \Aimeos\MShop::create( $this->getContext(), 'plugin' ); |
||
573 | |||
574 | if( isset( $data['plugin.id'] ) && $data['plugin.id'] != '' ) { |
||
575 | $item = $manager->getItem( $data['plugin.id'] ); |
||
576 | } else { |
||
577 | $item = $manager->createItem(); |
||
578 | } |
||
579 | |||
580 | $item->fromArray( $data, true ); |
||
581 | $item->setConfig( $conf ); |
||
582 | |||
583 | return $item; |
||
584 | } |
||
585 | |||
586 | |||
587 | /** |
||
588 | * Constructs the data array for the view from the given item |
||
589 | * |
||
590 | * @param \Aimeos\MShop\Plugin\Item\Iface $item Plugin item object |
||
591 | * @return string[] Multi-dimensional associative list of item data |
||
592 | */ |
||
593 | protected function toArray( \Aimeos\MShop\Plugin\Item\Iface $item, $copy = false ) |
||
614 | } |
||
615 | |||
616 | |||
617 | /** |
||
618 | * Returns the rendered template including the view data |
||
619 | * |
||
620 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
621 | * @return string HTML output |
||
622 | */ |
||
623 | protected function render( \Aimeos\MW\View\Iface $view ) |
||
648 | } |
||
649 | } |
||
650 |