Total Complexity | 51 |
Total Lines | 652 |
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() |
||
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, 'service' ); |
||
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( '==', 'service.id', $ids ) ); |
||
147 | $items = $manager->searchItems( $search, $this->getDomains() ); |
||
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', 'service', null, 'delete' ); |
||
164 | return; |
||
165 | } |
||
166 | catch( \Aimeos\MShop\Exception $e ) |
||
167 | { |
||
168 | $error = array( 'service-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( 'service-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() |
||
191 | { |
||
192 | $view = $this->getView(); |
||
193 | $context = $this->getContext(); |
||
194 | |||
195 | try |
||
196 | { |
||
197 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
198 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
199 | } |
||
200 | |||
201 | $manager = \Aimeos\MShop::create( $context, 'service' ); |
||
202 | |||
203 | $view->item = $manager->getItem( $id, $this->getDomains() ); |
||
204 | $view->itemData = $this->toArray( $view->item ); |
||
205 | $view->itemSubparts = $this->getSubClientNames(); |
||
206 | $view->itemDecorators = $this->getDecoratorNames(); |
||
207 | $view->itemProviders = $this->getProviderNames(); |
||
208 | $view->itemAttributes = $this->getConfigAttributes( $view->item ); |
||
209 | $view->itemTypes = $this->getTypeItems(); |
||
210 | $view->itemBody = ''; |
||
211 | |||
212 | foreach( $this->getSubClients() as $idx => $client ) |
||
213 | { |
||
214 | $view->tabindex = ++$idx + 1; |
||
215 | $view->itemBody .= $client->get(); |
||
216 | } |
||
217 | } |
||
218 | catch( \Aimeos\MShop\Exception $e ) |
||
219 | { |
||
220 | $error = array( 'service-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
221 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
222 | $this->logException( $e ); |
||
223 | } |
||
224 | catch( \Exception $e ) |
||
225 | { |
||
226 | $error = array( 'service-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
227 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
228 | $this->logException( $e ); |
||
229 | } |
||
230 | |||
231 | return $this->render( $view ); |
||
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, 'service' ); |
||
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' ), 'service', $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( 'service-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( 'service-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(), 'service' ); |
||
301 | $manager = \Aimeos\MShop::create( $context, 'service' ); |
||
302 | |||
303 | $search = $manager->createSearch(); |
||
304 | $search->setSortations( [$search->sort( '+', 'service.type' ), $search->sort( '+', 'service.position' )] ); |
||
305 | $search = $this->initCriteria( $search, $params ); |
||
306 | |||
307 | $view->items = $manager->searchItems( $search, $this->getDomains(), $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( 'service-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( 'service-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
327 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
328 | $this->logException( $e ); |
||
329 | } |
||
330 | |||
331 | /** admin/jqadm/service/template-list |
||
332 | * Relative path to the HTML body template for the service 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/service/template-list'; |
||
351 | $default = 'service/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/service/decorators/excludes |
||
367 | * Excludes decorators added by the "common" option from the service 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/service/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/service/decorators/global |
||
389 | * @see admin/jqadm/service/decorators/local |
||
390 | */ |
||
391 | |||
392 | /** admin/jqadm/service/decorators/global |
||
393 | * Adds a list of globally available decorators only to the service 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/service/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/service/decorators/excludes |
||
413 | * @see admin/jqadm/service/decorators/local |
||
414 | */ |
||
415 | |||
416 | /** admin/jqadm/service/decorators/local |
||
417 | * Adds a list of local decorators only to the service 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\Service\Decorator\*") around the JQAdm client. |
||
426 | * |
||
427 | * admin/jqadm/service/decorators/local = array( 'decorator2' ) |
||
428 | * |
||
429 | * This would add the decorator named "decorator2" defined by |
||
430 | * "\Aimeos\Admin\JQAdm\Service\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/service/decorators/excludes |
||
437 | * @see admin/jqadm/service/decorators/global |
||
438 | */ |
||
439 | return $this->createSubClient( 'service/' . $type, $name ); |
||
440 | } |
||
441 | |||
442 | |||
443 | /** |
||
444 | * Returns the backend configuration attributes of the provider and decorators |
||
445 | * |
||
446 | * @param \Aimeos\MShop\Service\Item\Iface $item Service item incl. provider/decorator property |
||
447 | * @return \Aimeos\MW\Common\Critera\Attribute\Iface[] List of configuration attributes |
||
448 | */ |
||
449 | public function getConfigAttributes( \Aimeos\MShop\Service\Item\Iface $item ) |
||
450 | { |
||
451 | $manager = \Aimeos\MShop::create( $this->getContext(), 'service' ); |
||
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 domain names whose items should be fetched too |
||
463 | * |
||
464 | * @return string[] List of domain names |
||
465 | */ |
||
466 | protected function getDomains() |
||
467 | { |
||
468 | /** admin/jqadm/service/domains |
||
469 | * List of domain items that should be fetched along with the service |
||
470 | * |
||
471 | * If you need to display additional content, you can configure your own |
||
472 | * list of domains (attribute, media, price, service, text, etc. are |
||
473 | * domains) whose items are fetched from the storage. |
||
474 | * |
||
475 | * @param array List of domain names |
||
476 | * @since 2017.10 |
||
477 | * @category Developer |
||
478 | */ |
||
479 | $domains = array( 'media', 'price', 'text' ); |
||
480 | |||
481 | return $this->getContext()->getConfig()->get( 'admin/jqadm/service/domains', $domains ); |
||
482 | } |
||
483 | |||
484 | |||
485 | /** |
||
486 | * Returns the names of the available service decorators |
||
487 | * |
||
488 | * @return string[] List of decorator class names |
||
489 | */ |
||
490 | protected function getDecoratorNames() |
||
491 | { |
||
492 | $ds = DIRECTORY_SEPARATOR; |
||
493 | return $this->getClassNames( 'MShop' . $ds . 'Service' . $ds . 'Provider' . $ds . 'Decorator' ); |
||
494 | } |
||
495 | |||
496 | |||
497 | /** |
||
498 | * Returns the names of the available service providers |
||
499 | * |
||
500 | * @return string[] List of provider class names |
||
501 | */ |
||
502 | protected function getProviderNames() |
||
503 | { |
||
504 | $ds = DIRECTORY_SEPARATOR; |
||
505 | return [ |
||
506 | 'delivery' => $this->getClassNames( 'MShop' . $ds . 'Service' . $ds . 'Provider' . $ds . 'Delivery' ), |
||
507 | 'payment' => $this->getClassNames( 'MShop' . $ds . 'Service' . $ds . 'Provider' . $ds . 'Payment' ), |
||
508 | ]; |
||
509 | } |
||
510 | |||
511 | |||
512 | /** |
||
513 | * Returns the list of sub-client names configured for the client. |
||
514 | * |
||
515 | * @return array List of JQAdm client names |
||
516 | */ |
||
517 | protected function getSubClientNames() |
||
518 | { |
||
519 | /** admin/jqadm/service/standard/subparts |
||
520 | * List of JQAdm sub-clients rendered within the service section |
||
521 | * |
||
522 | * The output of the frontend is composed of the code generated by the JQAdm |
||
523 | * clients. Each JQAdm client can consist of serveral (or none) sub-clients |
||
524 | * that are responsible for rendering certain sub-parts of the output. The |
||
525 | * sub-clients can contain JQAdm clients themselves and therefore a |
||
526 | * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
||
527 | * the output that is placed inside the container of its parent. |
||
528 | * |
||
529 | * At first, always the JQAdm code generated by the parent is printed, then |
||
530 | * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients |
||
531 | * determines the order of the output of these sub-clients inside the parent |
||
532 | * container. If the configured list of clients is |
||
533 | * |
||
534 | * array( "subclient1", "subclient2" ) |
||
535 | * |
||
536 | * you can easily change the order of the output by reordering the subparts: |
||
537 | * |
||
538 | * admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
539 | * |
||
540 | * You can also remove one or more parts if they shouldn't be rendered: |
||
541 | * |
||
542 | * admin/jqadm/<clients>/subparts = array( "subclient1" ) |
||
543 | * |
||
544 | * As the clients only generates structural JQAdm, the layout defined via CSS |
||
545 | * should support adding, removing or reordering content by a fluid like |
||
546 | * design. |
||
547 | * |
||
548 | * @param array List of sub-client names |
||
549 | * @since 2017.10 |
||
550 | * @category Developer |
||
551 | */ |
||
552 | return $this->getContext()->getConfig()->get( 'admin/jqadm/service/standard/subparts', [] ); |
||
553 | } |
||
554 | |||
555 | |||
556 | /** |
||
557 | * Returns the available service type items |
||
558 | * |
||
559 | * @return array List of item implementing \Aimeos\MShop\Common\Type\Iface |
||
560 | */ |
||
561 | protected function getTypeItems() |
||
569 | } |
||
570 | |||
571 | |||
572 | /** |
||
573 | * Creates new and updates existing items using the data array |
||
574 | * |
||
575 | * @param array $data Data array |
||
576 | * @return \Aimeos\MShop\Service\Item\Iface New service item object |
||
577 | */ |
||
578 | protected function fromArray( array $data ) |
||
579 | { |
||
609 | } |
||
610 | |||
611 | |||
612 | /** |
||
613 | * Constructs the data array for the view from the given item |
||
614 | * |
||
615 | * @param \Aimeos\MShop\Service\Item\Iface $item Service item object |
||
616 | * @return string[] Multi-dimensional associative list of item data |
||
617 | */ |
||
618 | protected function toArray( \Aimeos\MShop\Service\Item\Iface $item, $copy = false ) |
||
640 | } |
||
641 | |||
642 | |||
643 | /** |
||
644 | * Returns the rendered template including the view data |
||
645 | * |
||
646 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
647 | * @return string HTML output |
||
648 | */ |
||
649 | protected function render( \Aimeos\MW\View\Iface $view ) |
||
676 |