Passed
Push — master ( afdf4c...3dd999 )
by Aimeos
08:34
created

Standard::fromArray()   F

Complexity

Conditions 16
Paths 514

Size

Total Lines 64
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 32
c 1
b 0
f 0
nc 514
nop 2
dl 0
loc 64
rs 2.075

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2021
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Catalog\Product;
12
13
sprintf( 'product' ); // for translation
14
15
16
/**
17
 * Default implementation of catalog product JQAdm client.
18
 *
19
 * @package Admin
20
 * @subpackage JQAdm
21
 */
22
class Standard
23
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
24
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
25
{
26
	/** admin/jqadm/catalog/product/name
27
	 * Name of the product subpart used by the JQAdm catalog implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Catalog\Product\Myname".
30
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
31
	 *
32
	 * @param string Last part of the JQAdm class name
33
	 * @since 2017.07
34
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Adds the required data used in the template
40
	 *
41
	 * @param \Aimeos\MW\View\Iface $view View object
42
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
43
	 */
44
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
45
	{
46
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists/type' );
47
48
		$search = $manager->filter( true )->slice( 0, 10000 );
49
		$search->setConditions( $search->compare( '==', 'catalog.lists.type.domain', 'product' ) );
50
		$search->setSortations( [$search->sort( '+', 'catalog.lists.type.position' )] );
51
52
		$view->productListTypes = $manager->search( $search );
53
54
		return $view;
55
	}
56
57
58
	/**
59
	 * Copies a resource
60
	 *
61
	 * @return string|null HTML output
62
	 */
63
	public function copy() : ?string
64
	{
65
		$view = $this->getObject()->addData( $this->getView() );
66
		$view->productBody = parent::copy();
67
68
		return $this->render( $view );
69
	}
70
71
72
	/**
73
	 * Creates a new resource
74
	 *
75
	 * @return string|null HTML output
76
	 */
77
	public function create() : ?string
78
	{
79
		$view = $this->getObject()->addData( $this->getView() );
80
		$view->productBody = parent::create();
81
82
		return $this->render( $view );
83
	}
84
85
86
	/**
87
	 * Returns a single resource
88
	 *
89
	 * @return string|null HTML output
90
	 */
91
	public function get() : ?string
92
	{
93
		$view = $this->getObject()->addData( $this->getView() );
94
95
		$total = 0;
96
		$params = $this->storeFilter( $view->param( 'cp', [] ), 'catalogproduct' );
97
		$listItems = $this->getListItems( $view->item, $params, $total );
98
99
		$view->productItems = $this->getProductItems( $listItems );
100
		$view->productData = $this->toArray( $listItems );
101
		$view->productBody = parent::get();
102
		$view->productTotal = $total;
103
104
		return $this->render( $view );
105
	}
106
107
108
	/**
109
	 * Saves the data
110
	 *
111
	 * @return string|null HTML output
112
	 */
113
	public function save() : ?string
114
	{
115
		$view = $this->getView();
116
117
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists' );
118
		$manager->begin();
119
120
		try
121
		{
122
			$this->storeFilter( $view->param( 'cp', [] ), 'catalogproduct' );
123
			$this->fromArray( $view->item, $view->param( 'product', [] ) );
124
			$view->productBody = parent::save();
125
126
			$manager->commit();
127
		}
128
		catch( \Exception $e )
129
		{
130
			$manager->rollback();
131
			throw $e;
132
		}
133
134
		return null;
135
	}
136
137
138
	/**
139
	 * Returns the sub-client given by its name.
140
	 *
141
	 * @param string $type Name of the client type
142
	 * @param string|null $name Name of the sub-client (Default if null)
143
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
144
	 */
145
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
146
	{
147
		/** admin/jqadm/catalog/product/decorators/excludes
148
		 * Excludes decorators added by the "common" option from the catalog JQAdm client
149
		 *
150
		 * Decorators extend the functionality of a class by adding new aspects
151
		 * (e.g. log what is currently done), executing the methods of the underlying
152
		 * class only in certain conditions (e.g. only for logged in users) or
153
		 * modify what is returned to the caller.
154
		 *
155
		 * This option allows you to remove a decorator added via
156
		 * "admin/jqadm/common/decorators/default" before they are wrapped
157
		 * around the JQAdm client.
158
		 *
159
		 *  admin/jqadm/catalog/product/decorators/excludes = array( 'decorator1' )
160
		 *
161
		 * This would remove the decorator named "decorator1" from the list of
162
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
163
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
164
		 *
165
		 * @param array List of decorator names
166
		 * @since 2017.07
167
		 * @category Developer
168
		 * @see admin/jqadm/common/decorators/default
169
		 * @see admin/jqadm/catalog/product/decorators/global
170
		 * @see admin/jqadm/catalog/product/decorators/local
171
		 */
172
173
		/** admin/jqadm/catalog/product/decorators/global
174
		 * Adds a list of globally available decorators only to the catalog JQAdm client
175
		 *
176
		 * Decorators extend the functionality of a class by adding new aspects
177
		 * (e.g. log what is currently done), executing the methods of the underlying
178
		 * class only in certain conditions (e.g. only for logged in users) or
179
		 * modify what is returned to the caller.
180
		 *
181
		 * This option allows you to wrap global decorators
182
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
183
		 *
184
		 *  admin/jqadm/catalog/product/decorators/global = array( 'decorator1' )
185
		 *
186
		 * This would add the decorator named "decorator1" defined by
187
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
188
		 *
189
		 * @param array List of decorator names
190
		 * @since 2017.07
191
		 * @category Developer
192
		 * @see admin/jqadm/common/decorators/default
193
		 * @see admin/jqadm/catalog/product/decorators/excludes
194
		 * @see admin/jqadm/catalog/product/decorators/local
195
		 */
196
197
		/** admin/jqadm/catalog/product/decorators/local
198
		 * Adds a list of local decorators only to the catalog JQAdm client
199
		 *
200
		 * Decorators extend the functionality of a class by adding new aspects
201
		 * (e.g. log what is currently done), executing the methods of the underlying
202
		 * class only in certain conditions (e.g. only for logged in users) or
203
		 * modify what is returned to the caller.
204
		 *
205
		 * This option allows you to wrap local decorators
206
		 * ("\Aimeos\Admin\JQAdm\Catalog\Decorator\*") around the JQAdm client.
207
		 *
208
		 *  admin/jqadm/catalog/product/decorators/local = array( 'decorator2' )
209
		 *
210
		 * This would add the decorator named "decorator2" defined by
211
		 * "\Aimeos\Admin\JQAdm\Catalog\Decorator\Decorator2" only to the JQAdm client.
212
		 *
213
		 * @param array List of decorator names
214
		 * @since 2017.07
215
		 * @category Developer
216
		 * @see admin/jqadm/common/decorators/default
217
		 * @see admin/jqadm/catalog/product/decorators/excludes
218
		 * @see admin/jqadm/catalog/product/decorators/global
219
		 */
220
		return $this->createSubClient( 'catalog/product/' . $type, $name );
221
	}
222
223
224
	/**
225
	 * Returns the catalog list items referencing the products
226
	 *
227
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object
228
	 * @param array $params Associative list of GET/POST parameters
229
	 * @param integer $total Value/result parameter that will contain the item total afterwards
230
	 * @return \Aimeos\Map Catalog list items implementing \Aimeos\MShop\Common\Item\List\Iface  referencing the products
231
	 */
232
	protected function getListItems( \Aimeos\MShop\Catalog\Item\Iface $item, array $params = [], &$total = null ) : \Aimeos\Map
233
	{
234
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists' );
235
236
		$search = $manager->filter();
237
		$search->setSortations( [
238
			$search->sort( '+', 'catalog.lists.position' ),
239
			$search->sort( '+', 'catalog.lists.refid' )
240
		] );
241
242
		$search = $this->initCriteria( $search, $params, 'catalogproduct' );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Admin\JQAdm\Base::initCriteria() has too many arguments starting with 'catalogproduct'. ( Ignorable by Annotation )

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

242
		/** @scrutinizer ignore-call */ 
243
  $search = $this->initCriteria( $search, $params, 'catalogproduct' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
243
		$expr = [
244
			$search->getConditions(),
245
			$search->compare( '==', 'catalog.lists.parentid', $item->getId() ),
246
			$search->compare( '==', 'catalog.lists.domain', 'product' ),
247
		];
248
		$search->setConditions( $search->and( $expr ) );
249
250
		return $manager->search( $search, [], $total );
251
	}
252
253
254
	/**
255
	 * Returns the product items referenced by the given list items
256
	 *
257
	 * @param \Aimeos\Map $listItems Catalog list items implementing \Aimeos\MShop\Common\Item\List\Iface and referencing the products
258
	 * @return \Aimeos\Map List of product IDs as keys and items implementing \Aimeos\MShop\Product\Item\Iface
259
	 */
260
	protected function getProductItems( \Aimeos\Map $listItems ) : \Aimeos\Map
261
	{
262
		$list = $listItems->getRefId()->toArray();
263
		$manager = \Aimeos\MShop::create( $this->getContext(), 'product' );
264
265
		$search = $manager->filter()->slice( 0, count( $list ) );
266
		$search->setConditions( $search->compare( '==', 'product.id', $list ) );
267
268
		return $manager->search( $search );
269
	}
270
271
272
	/**
273
	 * Returns the list of sub-client names configured for the client.
274
	 *
275
	 * @return array List of JQAdm client names
276
	 */
277
	protected function getSubClientNames() : array
278
	{
279
		/** admin/jqadm/catalog/product/subparts
280
		 * List of JQAdm sub-clients rendered within the catalog product section
281
		 *
282
		 * The output of the frontend is composed of the code generated by the JQAdm
283
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
284
		 * that are responsible for rendering certain sub-parts of the output. The
285
		 * sub-clients can contain JQAdm clients themselves and therefore a
286
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
287
		 * the output that is placed inside the container of its parent.
288
		 *
289
		 * At first, always the JQAdm code generated by the parent is printed, then
290
		 * the JQAdm code of its sub-clients. The product of the JQAdm sub-clients
291
		 * determines the product of the output of these sub-clients inside the parent
292
		 * container. If the configured list of clients is
293
		 *
294
		 *  array( "subclient1", "subclient2" )
295
		 *
296
		 * you can easily change the product of the output by reproducting the subparts:
297
		 *
298
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
299
		 *
300
		 * You can also remove one or more parts if they shouldn't be rendered:
301
		 *
302
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
303
		 *
304
		 * As the clients only generates structural JQAdm, the layout defined via CSS
305
		 * should support adding, removing or reproducting content by a fluid like
306
		 * design.
307
		 *
308
		 * @param array List of sub-client names
309
		 * @since 2017.07
310
		 * @category Developer
311
		 */
312
		return $this->getContext()->getConfig()->get( 'admin/jqadm/catalog/product/subparts', [] );
313
	}
314
315
316
	/**
317
	 * Creates new and updates existing items using the data array
318
	 *
319
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object without referenced domain items
320
	 * @param array $data Data array
321
	 * @return \Aimeos\MShop\Catalog\Item\Iface Modified catalog item
322
	 */
323
	protected function fromArray( \Aimeos\MShop\Catalog\Item\Iface $item, array $data ) : \Aimeos\MShop\Catalog\Item\Iface
324
	{
325
		$listManager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists' );
326
		$listItem = $listManager->create()->setParentId( $item->getId() )->setDomain( 'product' );
327
		$listIds = $this->getValue( $data, 'catalog.lists.id', [] );
328
329
		if( !empty( $listIds ) ) {
330
			$listItems = $listManager->search( $listManager->filter()->add( 'catalog.lists.id', '==', $listIds ) );
331
		} else {
332
			$listItems = map();
333
		}
334
335
		foreach( (array) $listIds as $idx => $listid )
336
		{
337
			$litem = $listItems->get( $listid ) ?: clone $listItem;
338
339
			if( isset( $data['catalog.lists.type'][$idx] ) ) {
340
				$litem->setType( $this->getValue( $data, 'catalog.lists.type/' . $idx ) );
341
			}
342
343
			if( isset( $data['catalog.lists.refid'][$idx] ) ) {
344
				$litem->setRefId( $this->getValue( $data, 'catalog.lists.refid/' . $idx ) );
345
			}
346
347
			if( isset( $data['catalog.lists.status'][$idx] ) ) {
348
				$litem->setStatus( (int) $this->getValue( $data, 'catalog.lists.status/' . $idx ) );
349
			}
350
351
			if( isset( $data['catalog.lists.position'][$idx] ) ) {
352
				$litem->setPosition( (int) $this->getValue( $data, 'catalog.lists.position/' . $idx ) );
353
			}
354
355
			if( isset( $data['catalog.lists.datestart'][$idx] ) ) {
356
				$litem->setDateStart( $this->getValue( $data, 'catalog.lists.datestart/' . $idx ) );
357
			}
358
359
			if( isset( $data['catalog.lists.dateend'][$idx] ) ) {
360
				$litem->setDateEnd( $this->getValue( $data, 'catalog.lists.dateend/' . $idx ) );
361
			}
362
363
			if( isset( $data['catalog.lists.config'][$idx] )
364
				&& ( $conf = json_decode( $this->getValue( $data, 'catalog.lists.config/' . $idx ), true ) ) !== null
0 ignored issues
show
Bug introduced by
It seems like $this->getValue($data, '....lists.config/' . $idx) can also be of type null; however, parameter $json of json_decode() does only seem to accept 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

364
				&& ( $conf = json_decode( /** @scrutinizer ignore-type */ $this->getValue( $data, 'catalog.lists.config/' . $idx ), true ) ) !== null
Loading history...
365
			) {
366
				$litem->setConfig( $conf );
367
			}
368
369
			if( isset( $data['config'][$idx]['key'] ) )
370
			{
371
				$conf = [];
372
373
				foreach( (array) $data['config'][$idx]['key'] as $pos => $key )
374
				{
375
					if( trim( $key ) !== '' && isset( $data['config'][$idx]['val'][$pos] ) ) {
376
						$conf[$key] = $data['config'][$idx]['val'][$pos];
377
					}
378
				}
379
380
				$litem->setConfig( $conf );
381
			}
382
383
			$listManager->save( $litem, false );
384
		}
385
386
		return $item;
387
	}
388
389
390
	/**
391
	 * Constructs the data array for the view from the given item
392
	 *
393
	 * @param \Aimeos\Map $listItems Catalog list items implementing \Aimeos\MShop\Common\Item\Lists\Iface and referencing the products
394
	 * @return string[] Multi-dimensional associative list of item data
395
	 */
396
	protected function toArray( \Aimeos\Map $listItems ) : array
397
	{
398
		$data = [];
399
400
		foreach( $listItems as $listItem )
401
		{
402
			foreach( $listItem->toArray( true ) as $key => $value ) {
403
				$data[$key][] = $value;
404
			}
405
		}
406
407
		return $data;
408
	}
409
410
411
	/**
412
	 * Returns the rendered template including the view data
413
	 *
414
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
415
	 * @return string HTML output
416
	 */
417
	protected function render( \Aimeos\MW\View\Iface $view ) : string
418
	{
419
		/** admin/jqadm/catalog/product/template-item
420
		 * Relative path to the HTML body template of the product subpart for catalogs.
421
		 *
422
		 * The template file contains the HTML code and processing instructions
423
		 * to generate the result shown in the body of the frontend. The
424
		 * configuration string is the path to the template file relative
425
		 * to the templates directory (usually in admin/jqadm/templates).
426
		 *
427
		 * You can overwrite the template file configuration in extensions and
428
		 * provide alternative templates. These alternative templates should be
429
		 * named like the default one but with the string "default" replaced by
430
		 * an unique name. You may use the name of your project for this. If
431
		 * you've implemented an alternative client class as well, "default"
432
		 * should be replaced by the name of the new class.
433
		 *
434
		 * @param string Relative path to the template creating the HTML code
435
		 * @since 2016.04
436
		 * @category Developer
437
		 */
438
		$tplconf = 'admin/jqadm/catalog/product/template-item';
439
		$default = 'catalog/item-product-standard';
440
441
		return $view->render( $view->config( $tplconf, $default ) );
442
	}
443
}
444