Standard   A
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 595
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 153
c 3
b 0
f 0
dl 0
loc 595
rs 9.1199
wmc 41

16 Methods

Rating   Name   Duplication   Size   Complexity  
A data() 0 5 1
A create() 0 23 3
A copy() 0 24 3
A batch() 0 26 4
A toArray() 0 17 2
A fromArray() 0 20 6
A render() 0 24 1
A save() 0 26 3
A getSubClient() 0 73 1
A delete() 0 41 4
A getDomains() 0 13 1
A getTypeItems() 0 6 1
A import() 0 21 5
A getSubClientNames() 0 35 1
A search() 0 48 2
A get() 0 24 3

How to fix   Complexity   

Complex Class

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
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Product;
12
13
sprintf( 'goods' ); // for translation
14
sprintf( 'product' ); // for translation
15
16
17
/**
18
 * Default implementation of product JQAdm client.
19
 *
20
 * @package Admin
21
 * @subpackage JQAdm
22
 */
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
68
	{
69
		$view->itemSubparts = $this->getSubClientNames();
70
		$view->itemTypes = $this->getTypeItems();
71
		return $view;
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
147
	{
148
		$view = $this->object()->data( $this->view() );
149
150
		try
151
		{
152
			$data = $view->param( 'item', [] );
153
154
			if( !isset( $view->item ) ) {
155
				$view->item = \Aimeos\MShop::create( $this->context(), 'index' )->create();
156
			}
157
158
			$data['product.siteid'] = $view->item->getSiteId();
159
160
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
161
			$view->itemBody = parent::create();
162
		}
163
		catch( \Exception $e )
164
		{
165
			$this->report( $e, 'create' );
166
		}
167
168
		return $this->render( $view );
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 );
0 ignored issues
show
Unused Code introduced by
The assignment to $item is dead and can be removed.
Loading history...
302
			$manager->commit();
303
304
			return $this->redirect( 'product', $view->param( 'next' ), $view->item->getId(), 'save' );
0 ignored issues
show
Bug introduced by
It seems like $view->item->getId() can also be of type Aimeos\Map; however, parameter $id of Aimeos\Admin\JQAdm\Base::redirect() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

304
			return $this->redirect( 'product', $view->param( 'next' ), /** @scrutinizer ignore-type */ $view->item->getId(), 'save' );
Loading history...
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' );
0 ignored issues
show
Bug introduced by
'product' of type string is incompatible with the type iterable expected by parameter $keys of Aimeos\Map::remove(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

328
			$domains = map( $this->getDomains() )->remove( /** @scrutinizer ignore-type */ 'product' );
Loading history...
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
569
	{
570
		$data = $item->toArray( true );
571
		$data['config'] = $this->flatten( $item->getConfig() );
572
573
		if( $copy === true )
574
		{
575
			$hash = substr( md5( microtime( true ) ), -5 );
576
			$data['product.code'] = $data['product.code'] . '_' . $hash;
577
			$data['product.label'] = $data['product.label'] . '_' . $hash;
578
			$data['product.siteid'] = $this->context()->locale()->getSiteId();
579
			$data['product.ctime'] = '';
580
			$data['product.url'] = '';
581
			$data['product.id'] = '';
582
		}
583
584
		return $data;
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
595
	{
596
		/** admin/jqadm/product/template-item
597
		 * Relative path to the HTML body template for the product item.
598
		 *
599
		 * The template file contains the HTML code and processing instructions
600
		 * to generate the result shown in the body of the frontend. The
601
		 * configuration string is the path to the template file relative
602
		 * to the templates directory (usually in templates/admin/jqadm).
603
		 *
604
		 * You can overwrite the template file configuration in extensions and
605
		 * provide alternative templates. These alternative templates should be
606
		 * named like the default one but with the string "default" replaced by
607
		 * an unique name. You may use the name of your project for this. If
608
		 * you've implemented an alternative client class as well, "default"
609
		 * should be replaced by the name of the new class.
610
		 *
611
		 * @param string Relative path to the template creating the HTML code
612
		 * @since 2016.04
613
		 */
614
		$tplconf = 'admin/jqadm/product/template-item';
615
		$default = 'product/item';
616
617
		return $view->render( $view->config( $tplconf, $default ) );
618
	}
619
}
620