Total Complexity | 41 |
Total Lines | 595 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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 |
||
23 | class Standard |
||
24 | extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base |
||
25 | implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface |
||
26 | { |
||
27 | /** admin/jqadm/product/name |
||
28 | * Class name of the used product panel implementation |
||
29 | * |
||
30 | * Each default admin client can be replace by an alternative imlementation. |
||
31 | * To use this implementation, you have to set the last part of the class |
||
32 | * name as configuration value so the client factory knows which class it |
||
33 | * has to instantiate. |
||
34 | * |
||
35 | * For example, if the name of the default class is |
||
36 | * |
||
37 | * \Aimeos\Admin\JQAdm\Product\Standard |
||
38 | * |
||
39 | * and you want to replace it with your own version named |
||
40 | * |
||
41 | * \Aimeos\Admin\JQAdm\Product\Myfavorite |
||
42 | * |
||
43 | * then you have to set the this configuration option: |
||
44 | * |
||
45 | * admin/jqadm/product/name = Myfavorite |
||
46 | * |
||
47 | * The value is the last part of your own class name and it's case sensitive, |
||
48 | * so take care that the configuration value is exactly named like the last |
||
49 | * part of the class name. |
||
50 | * |
||
51 | * The allowed characters of the class name are A-Z, a-z and 0-9. No other |
||
52 | * characters are possible! You should always start the last part of the class |
||
53 | * name with an upper case character and continue only with lower case characters |
||
54 | * or numbers. Avoid chamel case names like "MyFavorite"! |
||
55 | * |
||
56 | * @param string Last part of the class name |
||
57 | * @since 2016.01 |
||
58 | */ |
||
59 | |||
60 | |||
61 | /** |
||
62 | * Adds the required data used in the template |
||
63 | * |
||
64 | * @param \Aimeos\Base\View\Iface $view View object |
||
65 | * @return \Aimeos\Base\View\Iface View object with assigned parameters |
||
66 | */ |
||
67 | public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface |
||
72 | } |
||
73 | |||
74 | |||
75 | /** |
||
76 | * Batch update of a resource |
||
77 | * |
||
78 | * @return string|null Output to display |
||
79 | */ |
||
80 | public function batch() : ?string |
||
81 | { |
||
82 | $view = $this->view(); |
||
83 | |||
84 | if( !empty( $ids = $view->param( 'id' ) ) ) |
||
85 | { |
||
86 | $manager = \Aimeos\MShop::create( $this->context(), 'index' ); |
||
87 | $filter = $manager->filter()->add( ['product.id' => $ids] )->slice( 0, count( $ids ) ); |
||
88 | $items = $manager->search( $filter, $this->getDomains() ); |
||
89 | |||
90 | $data = $view->param( 'item', [] ); |
||
91 | |||
92 | foreach( $items as $item ) { |
||
93 | $temp = $data; $item->fromArray( $temp, true ); |
||
94 | } |
||
95 | |||
96 | $view->items = $items; |
||
97 | |||
98 | foreach( $this->getSubClients() as $client ) { |
||
99 | $client->batch(); |
||
100 | } |
||
101 | |||
102 | $manager->save( $items ); |
||
103 | } |
||
104 | |||
105 | return $this->redirect( 'product', 'search', null, 'save' ); |
||
106 | } |
||
107 | |||
108 | |||
109 | /** |
||
110 | * Copies a resource |
||
111 | * |
||
112 | * @return string|null HTML output |
||
113 | */ |
||
114 | public function copy() : ?string |
||
115 | { |
||
116 | $view = $this->object()->data( $this->view() ); |
||
117 | |||
118 | try |
||
119 | { |
||
120 | if( ( $id = $view->param( 'id' ) ) === null ) |
||
121 | { |
||
122 | $msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' ); |
||
123 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) ); |
||
124 | } |
||
125 | |||
126 | $manager = \Aimeos\MShop::create( $this->context(), 'index' ); |
||
127 | $view->item = $manager->get( $id, $this->getDomains() ); |
||
128 | |||
129 | $view->itemData = $this->toArray( $view->item, true ); |
||
130 | $view->itemBody = parent::copy(); |
||
131 | } |
||
132 | catch( \Exception $e ) |
||
133 | { |
||
134 | $this->report( $e, 'copy' ); |
||
135 | } |
||
136 | |||
137 | return $this->render( $view ); |
||
138 | } |
||
139 | |||
140 | |||
141 | /** |
||
142 | * Creates a new resource |
||
143 | * |
||
144 | * @return string|null HTML output |
||
145 | */ |
||
146 | public function create() : ?string |
||
169 | } |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Deletes a resource |
||
174 | * |
||
175 | * @return string|null HTML output |
||
176 | */ |
||
177 | public function delete() : ?string |
||
178 | { |
||
179 | $tags = ['product']; |
||
180 | $view = $this->view(); |
||
181 | $context = $this->context(); |
||
182 | |||
183 | $manager = \Aimeos\MShop::create( $context, 'index' ); |
||
184 | $manager->begin(); |
||
185 | |||
186 | try |
||
187 | { |
||
188 | if( ( $ids = $view->param( 'id' ) ) === null ) |
||
189 | { |
||
190 | $msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' ); |
||
191 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) ); |
||
192 | } |
||
193 | |||
194 | $search = $manager->filter()->add( 'product.id', '==', $ids )->slice( 0, count( (array) $ids ) ); |
||
195 | $items = $manager->search( $search, $this->getDomains() ); |
||
196 | |||
197 | foreach( $items as $id => $item ) |
||
198 | { |
||
199 | $tags[] = 'product-' . $id; |
||
200 | $view->item = $item; |
||
201 | parent::delete(); |
||
202 | } |
||
203 | |||
204 | $manager->delete( $items ); |
||
205 | $manager->commit(); |
||
206 | |||
207 | $context->cache()->deleteByTags( $tags ); |
||
208 | |||
209 | return $this->redirect( 'product', 'search', null, 'delete' ); |
||
210 | } |
||
211 | catch( \Exception $e ) |
||
212 | { |
||
213 | $manager->rollback(); |
||
214 | $this->report( $e, 'delete' ); |
||
215 | } |
||
216 | |||
217 | return $this->search(); |
||
218 | } |
||
219 | |||
220 | |||
221 | /** |
||
222 | * Returns a single resource |
||
223 | * |
||
224 | * @return string|null HTML output |
||
225 | */ |
||
226 | public function get() : ?string |
||
227 | { |
||
228 | $view = $this->object()->data( $this->view() ); |
||
229 | |||
230 | try |
||
231 | { |
||
232 | if( ( $id = $view->param( 'id' ) ) === null ) |
||
233 | { |
||
234 | $msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' ); |
||
235 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) ); |
||
236 | } |
||
237 | |||
238 | $manager = \Aimeos\MShop::create( $this->context(), 'index' ); |
||
239 | |||
240 | $view->item = $manager->get( $id, $this->getDomains() ); |
||
241 | $view->itemData = $this->toArray( $view->item ); |
||
242 | $view->itemBody = parent::get(); |
||
243 | } |
||
244 | catch( \Exception $e ) |
||
245 | { |
||
246 | $this->report( $e, 'get' ); |
||
247 | } |
||
248 | |||
249 | return $this->render( $view ); |
||
250 | } |
||
251 | |||
252 | |||
253 | /** |
||
254 | * Imports a file |
||
255 | * |
||
256 | * @return string|null Output to display or redirect |
||
257 | */ |
||
258 | public function import() : ?string |
||
259 | { |
||
260 | $context = $this->context(); |
||
261 | $fs = $context->fs( 'fs-import' ); |
||
262 | $site = $context->locale()->getSiteItem()->getCode(); |
||
263 | $dir = $context->config()->get( 'controller/jobs/product/import/csv/location', 'product' ); |
||
264 | |||
265 | if( $fs instanceof \Aimeos\Base\Filesystem\DirIface && $fs->isDir( $dir . '/' . $site ) === false ) { |
||
266 | $fs->mkdir( $dir . '/' . $site ); |
||
267 | } |
||
268 | |||
269 | $uploads = (array) $this->view()->request()->getUploadedFiles(); |
||
270 | $files = $this->val( $uploads, 'import', [] ); |
||
271 | |||
272 | foreach( is_array( $files ) ? $files : [$files] as $idx => $file ) |
||
273 | { |
||
274 | $filename = date( 'YmdHis' ) . '_' . str_pad( $idx, 3, '0', STR_PAD_LEFT ) . '_' . substr( md5( microtime( true ) ), 0, 4 ) . '.csv'; |
||
275 | $fs->writes( $dir . '/' . $site . '/' . $filename, $file->getStream()->detach() ); |
||
276 | } |
||
277 | |||
278 | return $this->redirect( 'product', 'search', null, 'upload' ); |
||
279 | } |
||
280 | |||
281 | |||
282 | /** |
||
283 | * Saves the data |
||
284 | * |
||
285 | * @return string|null HTML output |
||
286 | */ |
||
287 | public function save() : ?string |
||
288 | { |
||
289 | $view = $this->view(); |
||
290 | $context = $this->context(); |
||
291 | |||
292 | $manager = \Aimeos\MShop::create( $context, 'index' ); |
||
293 | $manager->begin(); |
||
294 | |||
295 | try |
||
296 | { |
||
297 | $item = $this->fromArray( $view->param( 'item', [] ) ); |
||
298 | $view->item = $item->getId() ? $item : $manager->save( $item ); |
||
299 | $view->itemBody = parent::save(); |
||
300 | |||
301 | $item = $manager->save( clone $view->item ); |
||
|
|||
302 | $manager->commit(); |
||
303 | |||
304 | return $this->redirect( 'product', $view->param( 'next' ), $view->item->getId(), 'save' ); |
||
305 | } |
||
306 | catch( \Exception $e ) |
||
307 | { |
||
308 | $manager->rollback(); |
||
309 | $this->report( $e, 'save' ); |
||
310 | } |
||
311 | |||
312 | return $this->create(); |
||
313 | } |
||
314 | |||
315 | |||
316 | /** |
||
317 | * Returns a list of resource according to the conditions |
||
318 | * |
||
319 | * @return string|null HTML output |
||
320 | */ |
||
321 | public function search() : ?string |
||
322 | { |
||
323 | $view = $this->view(); |
||
324 | |||
325 | try |
||
326 | { |
||
327 | $total = 0; |
||
328 | $domains = map( $this->getDomains() )->remove( 'product' ); |
||
329 | $params = $this->storeFilter( $view->param(), 'product' ); |
||
330 | $manager = \Aimeos\MShop::create( $this->context(), 'index' ); |
||
331 | |||
332 | $search = $manager->filter()->order( 'product.id' ); |
||
333 | $search = $this->initCriteria( $search, $params ); |
||
334 | |||
335 | $view->items = $manager->search( $search, $domains->toArray(), $total ); |
||
336 | $view->filterAttributes = $manager->getSearchAttributes( true ); |
||
337 | $view->filterOperators = $search->getOperators(); |
||
338 | $view->itemTypes = $this->getTypeItems(); |
||
339 | $view->itemBody = parent::search(); |
||
340 | $view->total = $total; |
||
341 | } |
||
342 | catch( \Exception $e ) |
||
343 | { |
||
344 | $this->report( $e, 'search' ); |
||
345 | } |
||
346 | |||
347 | /** admin/jqadm/product/template-list |
||
348 | * Relative path to the HTML body template for the product list. |
||
349 | * |
||
350 | * The template file contains the HTML code and processing instructions |
||
351 | * to generate the result shown in the body of the frontend. The |
||
352 | * configuration string is the path to the template file relative |
||
353 | * to the templates directory (usually in templates/admin/jqadm). |
||
354 | * |
||
355 | * You can overwrite the template file configuration in extensions and |
||
356 | * provide alternative templates. These alternative templates should be |
||
357 | * named like the default one but with the string "default" replaced by |
||
358 | * an unique name. You may use the name of your project for this. If |
||
359 | * you've implemented an alternative client class as well, "default" |
||
360 | * should be replaced by the name of the new class. |
||
361 | * |
||
362 | * @param string Relative path to the template creating the HTML code |
||
363 | * @since 2016.04 |
||
364 | */ |
||
365 | $tplconf = 'admin/jqadm/product/template-list'; |
||
366 | $default = 'product/list'; |
||
367 | |||
368 | return $view->render( $view->config( $tplconf, $default ) ); |
||
369 | } |
||
370 | |||
371 | |||
372 | /** |
||
373 | * Returns the sub-client given by its name. |
||
374 | * |
||
375 | * @param string $type Name of the client type |
||
376 | * @param string|null $name Name of the sub-client (Default if null) |
||
377 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object |
||
378 | */ |
||
379 | public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Admin\JQAdm\Iface |
||
380 | { |
||
381 | /** admin/jqadm/product/decorators/excludes |
||
382 | * Excludes decorators added by the "common" option from the product 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 remove a decorator added via |
||
390 | * "client/jqadm/common/decorators/default" before they are wrapped |
||
391 | * around the JQAdm client. |
||
392 | * |
||
393 | * admin/jqadm/product/decorators/excludes = array( 'decorator1' ) |
||
394 | * |
||
395 | * This would remove the decorator named "decorator1" from the list of |
||
396 | * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via |
||
397 | * "client/jqadm/common/decorators/default" to the JQAdm client. |
||
398 | * |
||
399 | * @param array List of decorator names |
||
400 | * @since 2016.01 |
||
401 | * @see admin/jqadm/common/decorators/default |
||
402 | * @see admin/jqadm/product/decorators/global |
||
403 | * @see admin/jqadm/product/decorators/local |
||
404 | */ |
||
405 | |||
406 | /** admin/jqadm/product/decorators/global |
||
407 | * Adds a list of globally available decorators only to the product JQAdm client |
||
408 | * |
||
409 | * Decorators extend the functionality of a class by adding new aspects |
||
410 | * (e.g. log what is currently done), executing the methods of the underlying |
||
411 | * class only in certain conditions (e.g. only for logged in users) or |
||
412 | * modify what is returned to the caller. |
||
413 | * |
||
414 | * This option allows you to wrap global decorators |
||
415 | * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. |
||
416 | * |
||
417 | * admin/jqadm/product/decorators/global = array( 'decorator1' ) |
||
418 | * |
||
419 | * This would add the decorator named "decorator1" defined by |
||
420 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client. |
||
421 | * |
||
422 | * @param array List of decorator names |
||
423 | * @since 2016.01 |
||
424 | * @see admin/jqadm/common/decorators/default |
||
425 | * @see admin/jqadm/product/decorators/excludes |
||
426 | * @see admin/jqadm/product/decorators/local |
||
427 | */ |
||
428 | |||
429 | /** admin/jqadm/product/decorators/local |
||
430 | * Adds a list of local decorators only to the product JQAdm client |
||
431 | * |
||
432 | * Decorators extend the functionality of a class by adding new aspects |
||
433 | * (e.g. log what is currently done), executing the methods of the underlying |
||
434 | * class only in certain conditions (e.g. only for logged in users) or |
||
435 | * modify what is returned to the caller. |
||
436 | * |
||
437 | * This option allows you to wrap local decorators |
||
438 | * ("\Aimeos\Admin\JQAdm\Product\Decorator\*") around the JQAdm client. |
||
439 | * |
||
440 | * admin/jqadm/product/decorators/local = array( 'decorator2' ) |
||
441 | * |
||
442 | * This would add the decorator named "decorator2" defined by |
||
443 | * "\Aimeos\Admin\JQAdm\Product\Decorator\Decorator2" only to the JQAdm client. |
||
444 | * |
||
445 | * @param array List of decorator names |
||
446 | * @since 2016.01 |
||
447 | * @see admin/jqadm/common/decorators/default |
||
448 | * @see admin/jqadm/product/decorators/excludes |
||
449 | * @see admin/jqadm/product/decorators/global |
||
450 | */ |
||
451 | return $this->createSubClient( 'product/' . $type, $name ); |
||
452 | } |
||
453 | |||
454 | |||
455 | /** |
||
456 | * Returns the domain names whose items should be fetched too |
||
457 | * |
||
458 | * @return string[] List of domain names |
||
459 | */ |
||
460 | protected function getDomains() : array |
||
461 | { |
||
462 | /** admin/jqadm/product/domains |
||
463 | * List of domain items that should be fetched along with the product |
||
464 | * |
||
465 | * If you need to display additional content, you can configure your own |
||
466 | * list of domains (attribute, media, price, product, text, etc. are |
||
467 | * domains) whose items are fetched from the storage. |
||
468 | * |
||
469 | * @param array List of domain names |
||
470 | * @since 2016.01 |
||
471 | */ |
||
472 | return $this->context()->config()->get( 'admin/jqadm/product/domains', [] ); |
||
473 | } |
||
474 | |||
475 | |||
476 | /** |
||
477 | * Returns the list of sub-client names configured for the client. |
||
478 | * |
||
479 | * @return array List of JQAdm client names |
||
480 | */ |
||
481 | protected function getSubClientNames() : array |
||
482 | { |
||
483 | /** admin/jqadm/product/subparts |
||
484 | * List of JQAdm sub-clients rendered within the product section |
||
485 | * |
||
486 | * The output of the frontend is composed of the code generated by the JQAdm |
||
487 | * clients. Each JQAdm client can consist of serveral (or none) sub-clients |
||
488 | * that are responsible for rendering certain sub-parts of the output. The |
||
489 | * sub-clients can contain JQAdm clients themselves and therefore a |
||
490 | * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates |
||
491 | * the output that is placed inside the container of its parent. |
||
492 | * |
||
493 | * At first, always the JQAdm code generated by the parent is printed, then |
||
494 | * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients |
||
495 | * determines the order of the output of these sub-clients inside the parent |
||
496 | * container. If the configured list of clients is |
||
497 | * |
||
498 | * array( "subclient1", "subclient2" ) |
||
499 | * |
||
500 | * you can easily change the order of the output by reordering the subparts: |
||
501 | * |
||
502 | * admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" ) |
||
503 | * |
||
504 | * You can also remove one or more parts if they shouldn't be rendered: |
||
505 | * |
||
506 | * admin/jqadm/<clients>/subparts = array( "subclient1" ) |
||
507 | * |
||
508 | * As the clients only generates structural JQAdm, the layout defined via CSS |
||
509 | * should support adding, removing or reordering content by a fluid like |
||
510 | * design. |
||
511 | * |
||
512 | * @param array List of sub-client names |
||
513 | * @since 2016.01 |
||
514 | */ |
||
515 | return $this->context()->config()->get( 'admin/jqadm/product/subparts', [] ); |
||
516 | } |
||
517 | |||
518 | |||
519 | /** |
||
520 | * Returns the available product type items |
||
521 | * |
||
522 | * @return \Aimeos\Map List of item implementing \Aimeos\MShop\Common\Type\Iface |
||
523 | */ |
||
524 | protected function getTypeItems() : \Aimeos\Map |
||
525 | { |
||
526 | $typeManager = \Aimeos\MShop::create( $this->context(), 'product/type' ); |
||
527 | $search = $typeManager->filter( true )->order( 'product.type.code' )->slice( 0, 10000 ); |
||
528 | |||
529 | return $typeManager->search( $search ); |
||
530 | } |
||
531 | |||
532 | |||
533 | /** |
||
534 | * Creates new and updates existing items using the data array |
||
535 | * |
||
536 | * @param array $data Data array |
||
537 | * @return \Aimeos\MShop\Product\Item\Iface New product item object |
||
538 | */ |
||
539 | protected function fromArray( array $data ) : \Aimeos\MShop\Product\Item\Iface |
||
540 | { |
||
541 | $manager = \Aimeos\MShop::create( $this->context(), 'index' ); |
||
542 | |||
543 | if( isset( $data['product.id'] ) && $data['product.id'] != '' ) { |
||
544 | $item = $manager->get( $data['product.id'], $this->getDomains() ); |
||
545 | } else { |
||
546 | $item = $manager->create(); |
||
547 | } |
||
548 | |||
549 | $item->fromArray( $data, true )->setConfig( [] ); |
||
550 | |||
551 | foreach( (array) $this->val( $data, 'config', [] ) as $cfg ) |
||
552 | { |
||
553 | if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' && ( $val = trim( $cfg['val'] ?? '' ) ) !== '' ) { |
||
554 | $item->setConfigValue( $key, json_decode( $val, true ) ?? $val ); |
||
555 | } |
||
556 | } |
||
557 | |||
558 | return $item; |
||
559 | } |
||
560 | |||
561 | |||
562 | /** |
||
563 | * Constructs the data array for the view from the given item |
||
564 | * |
||
565 | * @param \Aimeos\MShop\Product\Item\Iface $item Product item object |
||
566 | * @return string[] Multi-dimensional associative list of item data |
||
567 | */ |
||
568 | protected function toArray( \Aimeos\MShop\Product\Item\Iface $item, bool $copy = false ) : array |
||
585 | } |
||
586 | |||
587 | |||
588 | /** |
||
589 | * Returns the rendered template including the view data |
||
590 | * |
||
591 | * @param \Aimeos\Base\View\Iface $view View object with data assigned |
||
592 | * @return string HTML output |
||
593 | */ |
||
594 | protected function render( \Aimeos\Base\View\Iface $view ) : string |
||
618 | } |
||
619 | } |
||
620 |