Total Complexity | 41 |
Total Lines | 568 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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() |
||
70 | } |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Creates a new resource |
||
75 | * |
||
76 | * @return string HTML output |
||
77 | */ |
||
78 | public function create() |
||
118 | } |
||
119 | |||
120 | |||
121 | /** |
||
122 | * Deletes a resource |
||
123 | * |
||
124 | * @return string|null HTML output |
||
125 | */ |
||
126 | public function delete() |
||
127 | { |
||
128 | $view = $this->getView(); |
||
129 | $context = $this->getContext(); |
||
130 | |||
131 | $manager = \Aimeos\MShop::create( $context, 'attribute' ); |
||
132 | $manager->begin(); |
||
133 | |||
134 | try |
||
135 | { |
||
136 | if( ( $ids = $view->param( 'id' ) ) === null ) { |
||
137 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
138 | } |
||
139 | |||
140 | $search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) ); |
||
141 | $search->setConditions( $search->compare( '==', 'attribute.id', $ids ) ); |
||
142 | $items = $manager->searchItems( $search, $this->getDomains() ); |
||
143 | |||
144 | foreach( $items as $item ) |
||
145 | { |
||
146 | $view->item = $item; |
||
147 | |||
148 | foreach( $this->getSubClients() as $client ) { |
||
149 | $client->delete(); |
||
150 | } |
||
151 | |||
152 | $manager->saveItem( $view->item ); |
||
153 | } |
||
154 | |||
155 | $manager->deleteItems( array_keys( $items ) ); |
||
156 | $manager->commit(); |
||
157 | |||
158 | $this->nextAction( $view, 'search', 'attribute', null, 'delete' ); |
||
159 | return; |
||
160 | } |
||
161 | catch( \Aimeos\MShop\Exception $e ) |
||
162 | { |
||
163 | $error = array( 'attribute-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
164 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
165 | $this->logException( $e ); |
||
166 | } |
||
167 | catch( \Exception $e ) |
||
168 | { |
||
169 | $error = array( 'attribute-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
170 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
171 | $this->logException( $e ); |
||
172 | } |
||
173 | |||
174 | $manager->rollback(); |
||
175 | |||
176 | return $this->search(); |
||
177 | } |
||
178 | |||
179 | |||
180 | /** |
||
181 | * Returns a single resource |
||
182 | * |
||
183 | * @return string HTML output |
||
184 | */ |
||
185 | public function get() |
||
186 | { |
||
187 | $view = $this->getView(); |
||
188 | $context = $this->getContext(); |
||
189 | |||
190 | try |
||
191 | { |
||
192 | if( ( $id = $view->param( 'id' ) ) === null ) { |
||
193 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) ); |
||
194 | } |
||
195 | |||
196 | $manager = \Aimeos\MShop::create( $context, 'attribute' ); |
||
197 | |||
198 | $view->item = $manager->getItem( $id, $this->getDomains() ); |
||
199 | $view->itemSubparts = $this->getSubClientNames(); |
||
200 | $view->itemData = $this->toArray( $view->item ); |
||
201 | $view->itemTypes = $this->getTypeItems(); |
||
202 | $view->itemBody = ''; |
||
203 | |||
204 | foreach( $this->getSubClients() as $idx => $client ) |
||
205 | { |
||
206 | $view->tabindex = ++$idx + 1; |
||
207 | $view->itemBody .= $client->get(); |
||
208 | } |
||
209 | } |
||
210 | catch( \Aimeos\MShop\Exception $e ) |
||
211 | { |
||
212 | $error = array( 'attribute-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
213 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
214 | $this->logException( $e ); |
||
215 | } |
||
216 | catch( \Exception $e ) |
||
217 | { |
||
218 | $error = array( 'attribute-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
219 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
220 | $this->logException( $e ); |
||
221 | } |
||
222 | |||
223 | return $this->render( $view ); |
||
224 | } |
||
225 | |||
226 | |||
227 | /** |
||
228 | * Saves the data |
||
229 | * |
||
230 | * @return string HTML output |
||
231 | */ |
||
232 | public function save() |
||
233 | { |
||
234 | $view = $this->getView(); |
||
235 | $context = $this->getContext(); |
||
236 | |||
237 | $manager = \Aimeos\MShop::create( $context, 'attribute' ); |
||
238 | $manager->begin(); |
||
239 | |||
240 | try |
||
241 | { |
||
242 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
243 | $view->item = $item->getId() ? $item : $manager->saveItem( $item ); |
||
244 | $view->itemBody = ''; |
||
245 | |||
246 | foreach( $this->getSubClients() as $client ) { |
||
247 | $view->itemBody .= $client->save(); |
||
248 | } |
||
249 | |||
250 | $manager->saveItem( clone $view->item ); |
||
251 | $manager->commit(); |
||
252 | |||
253 | $this->nextAction( $view, $view->param( 'next' ), 'attribute', $view->item->getId(), 'save' ); |
||
254 | return; |
||
255 | } |
||
256 | catch( \Aimeos\Admin\JQAdm\Exception $e ) |
||
257 | { |
||
258 | // fall through to create |
||
259 | } |
||
260 | catch( \Aimeos\MShop\Exception $e ) |
||
261 | { |
||
262 | $error = array( 'attribute-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
263 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
264 | $this->logException( $e ); |
||
265 | } |
||
266 | catch( \Exception $e ) |
||
267 | { |
||
268 | $error = array( 'attribute-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
269 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
270 | $this->logException( $e ); |
||
271 | } |
||
272 | |||
273 | $manager->rollback(); |
||
274 | |||
275 | return $this->create(); |
||
276 | } |
||
277 | |||
278 | |||
279 | /** |
||
280 | * Returns a list of resource according to the conditions |
||
281 | * |
||
282 | * @return string HTML output |
||
283 | */ |
||
284 | public function search() |
||
285 | { |
||
286 | $view = $this->getView(); |
||
287 | $context = $this->getContext(); |
||
288 | |||
289 | try |
||
290 | { |
||
291 | $total = 0; |
||
292 | $params = $this->storeSearchParams( $view->param(), 'attribute' ); |
||
293 | $manager = \Aimeos\MShop::create( $context, 'attribute' ); |
||
294 | $search = $this->initCriteria( $manager->createSearch(), $params ); |
||
295 | |||
296 | $view->items = $manager->searchItems( $search, $this->getDomains(), $total ); |
||
297 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
298 | $view->filterOperators = $search->getOperators(); |
||
299 | $view->itemTypes = $this->getTypeItems(); |
||
300 | $view->total = $total; |
||
301 | $view->itemBody = ''; |
||
302 | |||
303 | foreach( $this->getSubClients() as $client ) { |
||
304 | $view->itemBody .= $client->search(); |
||
305 | } |
||
306 | } |
||
307 | catch( \Aimeos\MShop\Exception $e ) |
||
308 | { |
||
309 | $error = array( 'attribute-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
310 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
311 | $this->logException( $e ); |
||
312 | } |
||
313 | catch( \Exception $e ) |
||
314 | { |
||
315 | $error = array( 'attribute-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
316 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
317 | $this->logException( $e ); |
||
318 | } |
||
319 | |||
320 | /** admin/jqadm/attribute/template-list |
||
321 | * Relative path to the HTML body template for the attribute list. |
||
322 | * |
||
323 | * The template file contains the HTML code and processing instructions |
||
324 | * to generate the result shown in the body of the frontend. The |
||
325 | * configuration string is the path to the template file relative |
||
326 | * to the templates directory (usually in admin/jqadm/templates). |
||
327 | * |
||
328 | * You can overwrite the template file configuration in extensions and |
||
329 | * provide alternative templates. These alternative templates should be |
||
330 | * named like the default one but with the string "default" replaced by |
||
331 | * an unique name. You may use the name of your project for this. If |
||
332 | * you've implemented an alternative client class as well, "default" |
||
333 | * should be replaced by the name of the new class. |
||
334 | * |
||
335 | * @param string Relative path to the template creating the HTML code |
||
336 | * @since 2017.07 |
||
337 | * @category Developer |
||
338 | */ |
||
339 | $tplconf = 'admin/jqadm/attribute/template-list'; |
||
340 | $default = 'attribute/list-standard'; |
||
341 | |||
342 | return $view->render( $view->config( $tplconf, $default ) ); |
||
343 | } |
||
344 | |||
345 | |||
346 | /** |
||
347 | * Returns the sub-client given by its name. |
||
348 | * |
||
349 | * @param string $type Name of the client type |
||
350 | * @param string|null $name Name of the sub-client (Default if null) |
||
351 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
352 | */ |
||
353 | public function getSubClient( $type, $name = null ) |
||
354 | { |
||
355 | /** admin/jqadm/attribute/decorators/excludes |
||
356 | * Excludes decorators added by the "common" option from the attribute JQAdm client |
||
357 | * |
||
358 | * Decorators extend the functionality of a class by adding new aspects |
||
359 | * (e.g. log what is currently done), executing the methods of the underlying |
||
360 | * class only in certain conditions (e.g. only for logged in users) or |
||
361 | * modify what is returned to the caller. |
||
362 | * |
||
363 | * This option allows you to remove a decorator added via |
||
364 | * "client/jqadm/common/decorators/default" before they are wrapped |
||
365 | * around the JQAdm client. |
||
366 | * |
||
367 | * admin/jqadm/attribute/decorators/excludes = array( 'decorator1' ) |
||
368 | * |
||
369 | * This would remove the decorator named "decorator1" from the list of |
||
370 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
371 | * "client/jqadm/common/decorators/default" to the JQAdm client. |
||
372 | * |
||
373 | * @param array List of decorator names |
||
374 | * @since 2017.07 |
||
375 | * @category Developer |
||
376 | * @see admin/jqadm/common/decorators/default |
||
377 | * @see admin/jqadm/attribute/decorators/global |
||
378 | * @see admin/jqadm/attribute/decorators/local |
||
379 | */ |
||
380 | |||
381 | /** admin/jqadm/attribute/decorators/global |
||
382 | * Adds a list of globally available decorators only to the attribute JQAdm client |
||
383 | * |
||
384 | * Decorators extend the functionality of a class by adding new aspects |
||
385 | * (e.g. log what is currently done), executing the methods of the underlying |
||
386 | * class only in certain conditions (e.g. only for logged in users) or |
||
387 | * modify what is returned to the caller. |
||
388 | * |
||
389 | * This option allows you to wrap global decorators |
||
390 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
391 | * |
||
392 | * admin/jqadm/attribute/decorators/global = array( 'decorator1' ) |
||
393 | * |
||
394 | * This would add the decorator named "decorator1" defined by |
||
395 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
396 | * |
||
397 | * @param array List of decorator names |
||
398 | * @since 2017.07 |
||
399 | * @category Developer |
||
400 | * @see admin/jqadm/common/decorators/default |
||
401 | * @see admin/jqadm/attribute/decorators/excludes |
||
402 | * @see admin/jqadm/attribute/decorators/local |
||
403 | */ |
||
404 | |||
405 | /** admin/jqadm/attribute/decorators/local |
||
406 | * Adds a list of local decorators only to the attribute JQAdm client |
||
407 | * |
||
408 | * Decorators extend the functionality of a class by adding new aspects |
||
409 | * (e.g. log what is currently done), executing the methods of the underlying |
||
410 | * class only in certain conditions (e.g. only for logged in users) or |
||
411 | * modify what is returned to the caller. |
||
412 | * |
||
413 | * This option allows you to wrap local decorators |
||
414 | * ("\Aimeos\Admin\JQAdm\Attribute\Decorator\*") around the JQAdm client. |
||
415 | * |
||
416 | * admin/jqadm/attribute/decorators/local = array( 'decorator2' ) |
||
417 | * |
||
418 | * This would add the decorator named "decorator2" defined by |
||
419 | * "\Aimeos\Admin\JQAdm\Attribute\Decorator\Decorator2" only to the JQAdm client. |
||
420 | * |
||
421 | * @param array List of decorator names |
||
422 | * @since 2017.07 |
||
423 | * @category Developer |
||
424 | * @see admin/jqadm/common/decorators/default |
||
425 | * @see admin/jqadm/attribute/decorators/excludes |
||
426 | * @see admin/jqadm/attribute/decorators/global |
||
427 | */ |
||
428 | return $this->createSubClient( 'attribute/' . $type, $name ); |
||
429 | } |
||
430 | |||
431 | |||
432 | /** |
||
433 | * Returns the domain names whose items should be fetched too |
||
434 | * |
||
435 | * @return string[] List of domain names |
||
436 | */ |
||
437 | protected function getDomains() |
||
438 | { |
||
439 | /** admin/jqadm/attribute/domains |
||
440 | * List of domain items that should be fetched along with the attribute |
||
441 | * |
||
442 | * If you need to display additional content, you can configure your own |
||
443 | * list of domains (attribute, media, price, attribute, text, etc. are |
||
444 | * domains) whose items are fetched from the storage. |
||
445 | * |
||
446 | * @param array List of domain names |
||
447 | * @since 2017.07 |
||
448 | * @category Developer |
||
449 | */ |
||
450 | $domains = array( 'attribute/property', 'media', 'price', 'text' ); |
||
451 | |||
452 | return $this->getContext()->getConfig()->get( 'admin/jqadm/attribute/domains', $domains ); |
||
453 | } |
||
454 | |||
455 | |||
456 | /** |
||
457 | * Returns the list of sub-client names configured for the client. |
||
458 | * |
||
459 | * @return array List of JQAdm client names |
||
460 | */ |
||
461 | protected function getSubClientNames() |
||
462 | { |
||
463 | /** admin/jqadm/attribute/standard/subparts |
||
464 | * List of JQAdm sub-clients rendered within the attribute section |
||
465 | * |
||
466 | * The output of the frontend is composed of the code generated by the JQAdm |
||
467 | * clients. Each JQAdm client can consist of serveral (or none) sub-clients |
||
468 | * that are responsible for rendering certain sub-parts of the output. The |
||
469 | * sub-clients can contain JQAdm clients themselves and therefore a |
||
470 | * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
||
471 | * the output that is placed inside the container of its parent. |
||
472 | * |
||
473 | * At first, always the JQAdm code generated by the parent is printed, then |
||
474 | * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients |
||
475 | * determines the order of the output of these sub-clients inside the parent |
||
476 | * container. If the configured list of clients is |
||
477 | * |
||
478 | * array( "subclient1", "subclient2" ) |
||
479 | * |
||
480 | * you can easily change the order of the output by reordering the subparts: |
||
481 | * |
||
482 | * admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
483 | * |
||
484 | * You can also remove one or more parts if they shouldn't be rendered: |
||
485 | * |
||
486 | * admin/jqadm/<clients>/subparts = array( "subclient1" ) |
||
487 | * |
||
488 | * As the clients only generates structural JQAdm, the layout defined via CSS |
||
489 | * should support adding, removing or reordering content by a fluid like |
||
490 | * design. |
||
491 | * |
||
492 | * @param array List of sub-client names |
||
493 | * @since 2017.07 |
||
494 | * @category Developer |
||
495 | */ |
||
496 | return $this->getContext()->getConfig()->get( 'admin/jqadm/attribute/standard/subparts', [] ); |
||
497 | } |
||
498 | |||
499 | |||
500 | /** |
||
501 | * Returns the available attribute type items |
||
502 | * |
||
503 | * @return array List of item implementing \Aimeos\MShop\Common\Type\Iface |
||
504 | */ |
||
505 | protected function getTypeItems() |
||
506 | { |
||
507 | $typeManager = \Aimeos\MShop::create( $this->getContext(), 'attribute/type' ); |
||
508 | |||
509 | $search = $typeManager->createSearch( true )->setSlice( 0, 10000 ); |
||
510 | $search->setSortations( [$search->sort( '+', 'attribute.type.position' )] ); |
||
511 | |||
512 | return $this->map( $typeManager->searchItems( $search ) ); |
||
513 | } |
||
514 | |||
515 | |||
516 | /** |
||
517 | * Creates new and updates existing items using the data array |
||
518 | * |
||
519 | * @param array $data Data array |
||
520 | * @return \Aimeos\MShop\Attribute\Item\Iface New attribute item object |
||
521 | */ |
||
522 | protected function fromArray( array $data ) |
||
523 | { |
||
524 | $manager = \Aimeos\MShop::create( $this->getContext(), 'attribute' ); |
||
525 | |||
526 | if( isset( $data['attribute.id'] ) && $data['attribute.id'] != '' ) { |
||
527 | $item = $manager->getItem( $data['attribute.id'], $this->getDomains() ); |
||
528 | } else { |
||
529 | $item = $manager->createItem(); |
||
530 | } |
||
531 | |||
532 | $item->fromArray( $data, true ); |
||
533 | |||
534 | return $item; |
||
535 | } |
||
536 | |||
537 | |||
538 | /** |
||
539 | * Constructs the data array for the view from the given item |
||
540 | * |
||
541 | * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object |
||
542 | * @return string[] Multi-dimensional associative list of item data |
||
543 | */ |
||
544 | protected function toArray( \Aimeos\MShop\Attribute\Item\Iface $item, $copy = false ) |
||
545 | { |
||
546 | $data = $item->toArray( true ); |
||
547 | |||
548 | if( $copy === true ) |
||
549 | { |
||
550 | $data['attribute.siteid'] = $this->getContext()->getLocale()->getSiteId(); |
||
551 | $data['attribute.code'] = $data['attribute.code'] . '_copy'; |
||
552 | $data['attribute.id'] = ''; |
||
553 | } |
||
554 | |||
555 | return $data; |
||
556 | } |
||
557 | |||
558 | |||
559 | /** |
||
560 | * Returns the rendered template including the view data |
||
561 | * |
||
562 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
563 | * @return string HTML output |
||
564 | */ |
||
565 | protected function render( \Aimeos\MW\View\Iface $view ) |
||
590 | } |
||
591 | } |
||
592 |