Total Complexity | 50 |
Total Lines | 589 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 1 |
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, 'customer' ); |
||
43 | $view->item = $manager->getItem( $id, $this->getDomains() ); |
||
44 | |||
45 | $view->itemData = $this->toArray( $view->item, true ); |
||
46 | $view->itemSubparts = $this->getSubClientNames(); |
||
47 | $view->itemGroups = $this->getGroupItems(); |
||
48 | $view->itemBody = ''; |
||
49 | |||
50 | foreach( $this->getSubClients() as $idx => $client ) |
||
51 | { |
||
52 | $view->tabindex = ++$idx + 1; |
||
53 | $view->itemBody .= $client->copy(); |
||
54 | } |
||
55 | } |
||
56 | catch( \Aimeos\MShop\Exception $e ) |
||
57 | { |
||
58 | $error = array( 'customer-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
59 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
60 | $this->logException( $e ); |
||
61 | } |
||
62 | catch( \Exception $e ) |
||
63 | { |
||
64 | $error = array( 'customer-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
65 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
66 | $this->logException( $e ); |
||
67 | } |
||
68 | |||
69 | return $this->render( $view ); |
||
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, 'customer' ); |
||
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( '==', 'customer.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', 'customer', null, 'delete' ); |
||
159 | return; |
||
160 | } |
||
161 | catch( \Aimeos\MShop\Exception $e ) |
||
162 | { |
||
163 | $error = array( 'customer-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( 'customer-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, 'customer' ); |
||
197 | |||
198 | $view->item = $manager->getItem( $id, $this->getDomains() ); |
||
199 | $view->itemSubparts = $this->getSubClientNames(); |
||
200 | $view->itemData = $this->toArray( $view->item ); |
||
201 | $view->itemGroups = $this->getGroupItems(); |
||
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( 'customer-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( 'customer-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, 'customer' ); |
||
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' ), 'customer', $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( 'customer-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( 'customer-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(), 'customer' ); |
||
293 | $manager = \Aimeos\MShop::create( $context, 'customer' ); |
||
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->total = $total; |
||
300 | $view->itemBody = ''; |
||
301 | |||
302 | foreach( $this->getSubClients() as $client ) { |
||
303 | $view->itemBody .= $client->search(); |
||
304 | } |
||
305 | } |
||
306 | catch( \Aimeos\MShop\Exception $e ) |
||
307 | { |
||
308 | $error = array( 'customer-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) ); |
||
309 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
310 | $this->logException( $e ); |
||
311 | } |
||
312 | catch( \Exception $e ) |
||
313 | { |
||
314 | $error = array( 'customer-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() ); |
||
315 | $view->errors = $view->get( 'errors', [] ) + $error; |
||
316 | $this->logException( $e ); |
||
317 | } |
||
318 | |||
319 | /** admin/jqadm/customer/template-list |
||
320 | * Relative path to the HTML body template for the customer list. |
||
321 | * |
||
322 | * The template file contains the HTML code and processing instructions |
||
323 | * to generate the result shown in the body of the frontend. The |
||
324 | * configuration string is the path to the template file relative |
||
325 | * to the templates directory (usually in admin/jqadm/templates). |
||
326 | * |
||
327 | * You can overwrite the template file configuration in extensions and |
||
328 | * provide alternative templates. These alternative templates should be |
||
329 | * named like the default one but with the string "default" replaced by |
||
330 | * an unique name. You may use the name of your project for this. If |
||
331 | * you've implemented an alternative client class as well, "default" |
||
332 | * should be replaced by the name of the new class. |
||
333 | * |
||
334 | * @param string Relative path to the template creating the HTML code |
||
335 | * @since 2016.04 |
||
336 | * @category Developer |
||
337 | */ |
||
338 | $tplconf = 'admin/jqadm/customer/template-list'; |
||
339 | $default = 'customer/list-standard'; |
||
340 | |||
341 | return $view->render( $view->config( $tplconf, $default ) ); |
||
342 | } |
||
343 | |||
344 | |||
345 | /** |
||
346 | * Returns the sub-client given by its name. |
||
347 | * |
||
348 | * @param string $type Name of the client type |
||
349 | * @param string|null $name Name of the sub-client (Default if null) |
||
350 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
351 | */ |
||
352 | public function getSubClient( $type, $name = null ) |
||
353 | { |
||
354 | /** admin/jqadm/customer/decorators/excludes |
||
355 | * Excludes decorators added by the "common" option from the customer JQAdm client |
||
356 | * |
||
357 | * Decorators extend the functionality of a class by adding new aspects |
||
358 | * (e.g. log what is currently done), executing the methods of the underlying |
||
359 | * class only in certain conditions (e.g. only for logged in users) or |
||
360 | * modify what is returned to the caller. |
||
361 | * |
||
362 | * This option allows you to remove a decorator added via |
||
363 | * "client/jqadm/common/decorators/default" before they are wrapped |
||
364 | * around the JQAdm client. |
||
365 | * |
||
366 | * admin/jqadm/customer/decorators/excludes = array( 'decorator1' ) |
||
367 | * |
||
368 | * This would remove the decorator named "decorator1" from the list of |
||
369 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
370 | * "client/jqadm/common/decorators/default" to the JQAdm client. |
||
371 | * |
||
372 | * @param array List of decorator names |
||
373 | * @since 2017.07 |
||
374 | * @category Developer |
||
375 | * @see admin/jqadm/common/decorators/default |
||
376 | * @see admin/jqadm/customer/decorators/global |
||
377 | * @see admin/jqadm/customer/decorators/local |
||
378 | */ |
||
379 | |||
380 | /** admin/jqadm/customer/decorators/global |
||
381 | * Adds a list of globally available decorators only to the customer JQAdm client |
||
382 | * |
||
383 | * Decorators extend the functionality of a class by adding new aspects |
||
384 | * (e.g. log what is currently done), executing the methods of the underlying |
||
385 | * class only in certain conditions (e.g. only for logged in users) or |
||
386 | * modify what is returned to the caller. |
||
387 | * |
||
388 | * This option allows you to wrap global decorators |
||
389 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
390 | * |
||
391 | * admin/jqadm/customer/decorators/global = array( 'decorator1' ) |
||
392 | * |
||
393 | * This would add the decorator named "decorator1" defined by |
||
394 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
395 | * |
||
396 | * @param array List of decorator names |
||
397 | * @since 2017.07 |
||
398 | * @category Developer |
||
399 | * @see admin/jqadm/common/decorators/default |
||
400 | * @see admin/jqadm/customer/decorators/excludes |
||
401 | * @see admin/jqadm/customer/decorators/local |
||
402 | */ |
||
403 | |||
404 | /** admin/jqadm/customer/decorators/local |
||
405 | * Adds a list of local decorators only to the customer JQAdm client |
||
406 | * |
||
407 | * Decorators extend the functionality of a class by adding new aspects |
||
408 | * (e.g. log what is currently done), executing the methods of the underlying |
||
409 | * class only in certain conditions (e.g. only for logged in users) or |
||
410 | * modify what is returned to the caller. |
||
411 | * |
||
412 | * This option allows you to wrap local decorators |
||
413 | * ("\Aimeos\Admin\JQAdm\Customer\Decorator\*") around the JQAdm client. |
||
414 | * |
||
415 | * admin/jqadm/customer/decorators/local = array( 'decorator2' ) |
||
416 | * |
||
417 | * This would add the decorator named "decorator2" defined by |
||
418 | * "\Aimeos\Admin\JQAdm\Customer\Decorator\Decorator2" only to the JQAdm client. |
||
419 | * |
||
420 | * @param array List of decorator names |
||
421 | * @since 2017.07 |
||
422 | * @category Developer |
||
423 | * @see admin/jqadm/common/decorators/default |
||
424 | * @see admin/jqadm/customer/decorators/excludes |
||
425 | * @see admin/jqadm/customer/decorators/global |
||
426 | */ |
||
427 | return $this->createSubClient( 'customer/' . $type, $name ); |
||
428 | } |
||
429 | |||
430 | |||
431 | /** |
||
432 | * Returns the domain names whose items should be fetched too |
||
433 | * |
||
434 | * @return string[] List of domain names |
||
435 | */ |
||
436 | protected function getDomains() |
||
437 | { |
||
438 | /** admin/jqadm/customer/domains |
||
439 | * List of domain items that should be fetched along with the customer |
||
440 | * |
||
441 | * If you need to display additional content, you can configure your own |
||
442 | * list of domains (attribute, media, price, customer, text, etc. are |
||
443 | * domains) whose items are fetched from the storage. |
||
444 | * |
||
445 | * @param array List of domain names |
||
446 | * @since 2017.07 |
||
447 | * @category Developer |
||
448 | */ |
||
449 | $domains = ['customer/address', 'customer/group', 'customer/property']; |
||
450 | |||
451 | return $this->getContext()->getConfig()->get( 'admin/jqadm/customer/domains', $domains ); |
||
452 | } |
||
453 | |||
454 | |||
455 | /** |
||
456 | * Returns the available group items |
||
457 | * |
||
458 | * @return \Aimeos\MShop\Customer\Item\Group\Iface[] Associative list of group IDs as keys and group items as values |
||
459 | */ |
||
460 | protected function getGroupItems() |
||
461 | { |
||
462 | $list = []; |
||
463 | $isSuper = $this->getView()->access( ['super'] ); |
||
464 | $isAdmin = $this->getView()->access( ['admin'] ); |
||
465 | |||
466 | $manager = \Aimeos\MShop::create( $this->getContext(), 'customer/group' ); |
||
467 | $search = $manager->createSearch(); |
||
468 | $search->setSortations( [$search->sort( '+', 'customer.group.label' )] ); |
||
469 | |||
470 | foreach( $manager->searchItems( $search ) as $groupId => $groupItem ) |
||
471 | { |
||
472 | if( !$isSuper && in_array( $groupItem->getCode(), ['super'] ) ) { |
||
|
|||
473 | continue; |
||
474 | } |
||
475 | |||
476 | if( !$isSuper && !$isAdmin && in_array( $groupItem->getCode(), ['super', 'admin', 'editor'] ) ) { |
||
477 | continue; |
||
478 | } |
||
479 | |||
480 | $list[$groupId] = $groupItem; |
||
481 | } |
||
482 | |||
483 | return $list; |
||
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() |
||
493 | { |
||
494 | /** admin/jqadm/customer/standard/subparts |
||
495 | * List of JQAdm sub-clients rendered within the customer section |
||
496 | * |
||
497 | * The output of the frontend is composed of the code generated by the JQAdm |
||
498 | * clients. Each JQAdm client can consist of serveral (or none) sub-clients |
||
499 | * that are responsible for rendering certain sub-parts of the output. The |
||
500 | * sub-clients can contain JQAdm clients themselves and therefore a |
||
501 | * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
||
502 | * the output that is placed inside the container of its parent. |
||
503 | * |
||
504 | * At first, always the JQAdm code generated by the parent is printed, then |
||
505 | * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients |
||
506 | * determines the order of the output of these sub-clients inside the parent |
||
507 | * container. If the configured list of clients is |
||
508 | * |
||
509 | * array( "subclient1", "subclient2" ) |
||
510 | * |
||
511 | * you can easily change the order of the output by reordering the subparts: |
||
512 | * |
||
513 | * admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
514 | * |
||
515 | * You can also remove one or more parts if they shouldn't be rendered: |
||
516 | * |
||
517 | * admin/jqadm/<clients>/subparts = array( "subclient1" ) |
||
518 | * |
||
519 | * As the clients only generates structural JQAdm, the layout defined via CSS |
||
520 | * should support adding, removing or reordering content by a fluid like |
||
521 | * design. |
||
522 | * |
||
523 | * @param array List of sub-client names |
||
524 | * @since 2017.07 |
||
525 | * @category Developer |
||
526 | */ |
||
527 | return $this->getContext()->getConfig()->get( 'admin/jqadm/customer/standard/subparts', [] ); |
||
528 | } |
||
529 | |||
530 | |||
531 | |||
532 | /** |
||
533 | * Creates new and updates existing items using the data array |
||
534 | * |
||
535 | * @param array $data Data array |
||
536 | * @return \Aimeos\MShop\Customer\Item\Iface New customer item object |
||
537 | */ |
||
538 | protected function fromArray( array $data ) |
||
539 | { |
||
540 | $manager = \Aimeos\MShop::create( $this->getContext(), 'customer' ); |
||
541 | |||
542 | if( isset( $data['customer.id'] ) && $data['customer.id'] != '' ) { |
||
543 | $item = $manager->getItem( $data['customer.id'], $this->getDomains() ); |
||
544 | } else { |
||
545 | $item = $manager->createItem(); |
||
546 | } |
||
547 | |||
548 | $addr = $item->getPaymentAddress(); |
||
549 | $label = ( $addr->getFirstname() ? $addr->getFirstname() . ' ' : '' ) . $addr->getLastname(); |
||
550 | $label .= ( $addr->getCompany() ? '(' . $addr->getCompany() . ')' : '' ); |
||
551 | |||
552 | return $item->fromArray( $data, true ) |
||
553 | ->setGroups( array_intersect( array_keys( $this->getGroupItems() ), $item->getGroups() ) ) |
||
554 | ->setCode( $item->getCode() ?: $addr->getEmail() ) |
||
555 | ->setLabel( $label ); |
||
556 | } |
||
557 | |||
558 | |||
559 | /** |
||
560 | * Constructs the data array for the view from the given item |
||
561 | * |
||
562 | * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object |
||
563 | * @return string[] Multi-dimensional associative list of item data |
||
564 | */ |
||
565 | protected function toArray( \Aimeos\MShop\Customer\Item\Iface $item, $copy = false ) |
||
577 | } |
||
578 | |||
579 | |||
580 | /** |
||
581 | * Returns the rendered template including the view data |
||
582 | * |
||
583 | * @param \Aimeos\MW\View\Iface $view View object with data assigned |
||
584 | * @return string HTML output |
||
585 | */ |
||
586 | protected function render( \Aimeos\MW\View\Iface $view ) |
||
611 | } |
||
612 | } |
||
613 |