Passed
Push — master ( 140d1a...4bf2ae )
by Aimeos
18:27 queued 14:35
created

Standard::fromArray()   F

Complexity

Conditions 16
Paths 513

Size

Total Lines 70
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 35
c 1
b 0
f 0
nc 513
nop 2
dl 0
loc 70
rs 2.0762

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), 2023
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Attribute\Product;
12
13
sprintf( 'product' ); // for translation
14
15
16
/**
17
 * Default implementation of attribute 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/attribute/product/name
27
	 * Name of the product subpart used by the JQAdm attribute implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Attribute\Address\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 2023.10
34
	 */
35
36
37
	/**
38
	 * Adds the required data used in the template
39
	 *
40
	 * @param \Aimeos\Base\View\Iface $view View object
41
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
42
	 */
43
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
44
	{
45
		$manager = \Aimeos\MShop::create( $this->context(), 'attribute/lists/type' );
46
47
		$search = $manager->filter( true )->slice( 0, 10000 );
48
		$search->setConditions( $search->compare( '==', 'attribute.lists.type.domain', 'product' ) );
49
		$search->setSortations( [$search->sort( '+', 'attribute.lists.type.position' )] );
50
51
		$view->productListTypes = $manager->search( $search );
52
53
		return $view;
54
	}
55
56
57
	/**
58
	 * Copies a resource
59
	 *
60
	 * @return string|null HTML output
61
	 */
62
	public function copy() : ?string
63
	{
64
		$view = $this->object()->data( $this->view() );
65
		$view->productBody = parent::copy();
66
67
		return $this->render( $view );
68
	}
69
70
71
	/**
72
	 * Creates a new resource
73
	 *
74
	 * @return string|null HTML output
75
	 */
76
	public function create() : ?string
77
	{
78
		$view = $this->object()->data( $this->view() );
79
		$view->productBody = parent::create();
80
81
		return $this->render( $view );
82
	}
83
84
85
	/**
86
	 * Returns a single resource
87
	 *
88
	 * @return string|null HTML output
89
	 */
90
	public function get() : ?string
91
	{
92
		$view = $this->object()->data( $this->view() );
93
94
		$total = 0;
95
		$params = $this->storeFilter( $view->param( 'up', [] ), 'attributeproduct' );
96
		$listItems = $this->getListItems( $view->item, $params, $total );
97
98
		$view->productItems = $this->getProductItems( $listItems );
99
		$view->productData = $this->toArray( $listItems );
100
		$view->productBody = parent::search();
101
		$view->productTotal = $total;
102
103
		return $this->render( $view );
104
	}
105
106
107
	/**
108
	 * Saves the data
109
	 *
110
	 * @return string|null HTML output
111
	 */
112
	public function save() : ?string
113
	{
114
		$view = $this->view();
115
116
		$manager = \Aimeos\MShop::create( $this->context(), 'attribute/lists' );
117
		$manager->begin();
118
119
		try
120
		{
121
			$this->storeFilter( $view->param( 'up', [] ), 'attributeproduct' );
122
			$this->fromArray( $view->item, $view->param( 'product', [] ) );
123
			$view->productBody = parent::save();
124
125
			$manager->commit();
126
		}
127
		catch( \Exception $e )
128
		{
129
			$manager->rollback();
130
			throw $e;
131
		}
132
133
		return null;
134
	}
135
136
137
	/**
138
	 * Returns the sub-client given by its name.
139
	 *
140
	 * @param string $type Name of the client type
141
	 * @param string|null $name Name of the sub-client (Default if null)
142
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
143
	 */
144
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
145
	{
146
		/** admin/jqadm/attribute/product/decorators/excludes
147
		 * Excludes decorators added by the "common" option from the attribute JQAdm client
148
		 *
149
		 * Decorators extend the functionality of a class by adding new aspects
150
		 * (e.g. log what is currently done), executing the methods of the underlying
151
		 * class only in certain conditions (e.g. only for logged in users) or
152
		 * modify what is returned to the caller.
153
		 *
154
		 * This option allows you to remove a decorator added via
155
		 * "admin/jqadm/common/decorators/default" before they are wrapped
156
		 * around the JQAdm client.
157
		 *
158
		 *  admin/jqadm/attribute/product/decorators/excludes = array( 'decorator1' )
159
		 *
160
		 * This would remove the decorator named "decorator1" from the list of
161
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
162
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
163
		 *
164
		 * @param array List of decorator names
165
		 * @since 2023.10
166
		 * @see admin/jqadm/common/decorators/default
167
		 * @see admin/jqadm/attribute/product/decorators/global
168
		 * @see admin/jqadm/attribute/product/decorators/local
169
		 */
170
171
		/** admin/jqadm/attribute/product/decorators/global
172
		 * Adds a list of globally available decorators only to the attribute JQAdm client
173
		 *
174
		 * Decorators extend the functionality of a class by adding new aspects
175
		 * (e.g. log what is currently done), executing the methods of the underlying
176
		 * class only in certain conditions (e.g. only for logged in users) or
177
		 * modify what is returned to the caller.
178
		 *
179
		 * This option allows you to wrap global decorators
180
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
181
		 *
182
		 *  admin/jqadm/attribute/product/decorators/global = array( 'decorator1' )
183
		 *
184
		 * This would add the decorator named "decorator1" defined by
185
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
186
		 *
187
		 * @param array List of decorator names
188
		 * @since 2023.10
189
		 * @see admin/jqadm/common/decorators/default
190
		 * @see admin/jqadm/attribute/product/decorators/excludes
191
		 * @see admin/jqadm/attribute/product/decorators/local
192
		 */
193
194
		/** admin/jqadm/attribute/product/decorators/local
195
		 * Adds a list of local decorators only to the attribute JQAdm client
196
		 *
197
		 * Decorators extend the functionality of a class by adding new aspects
198
		 * (e.g. log what is currently done), executing the methods of the underlying
199
		 * class only in certain conditions (e.g. only for logged in users) or
200
		 * modify what is returned to the caller.
201
		 *
202
		 * This option allows you to wrap local decorators
203
		 * ("\Aimeos\Admin\JQAdm\Attribute\Decorator\*") around the JQAdm client.
204
		 *
205
		 *  admin/jqadm/attribute/product/decorators/local = array( 'decorator2' )
206
		 *
207
		 * This would add the decorator named "decorator2" defined by
208
		 * "\Aimeos\Admin\JQAdm\Attribute\Decorator\Decorator2" only to the JQAdm client.
209
		 *
210
		 * @param array List of decorator names
211
		 * @since 2023.10
212
		 * @see admin/jqadm/common/decorators/default
213
		 * @see admin/jqadm/attribute/product/decorators/excludes
214
		 * @see admin/jqadm/attribute/product/decorators/global
215
		 */
216
		return $this->createSubClient( 'attribute/product/' . $type, $name );
217
	}
218
219
220
	/**
221
	 * Returns the attribute list items referencing the products
222
	 *
223
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object
224
	 * @param array $params Associative list of GET/POST parameters
225
	 * @param integer $total Value/result parameter that will contain the item total afterwards
226
	 * @return \Aimeos\Map Attribute list items implementing \Aimeos\MShop\Common\Item\List\Iface referencing the products
227
	 */
228
	protected function getListItems( \Aimeos\MShop\Attribute\Item\Iface $item, array $params = [], &$total = null ) : \Aimeos\Map
229
	{
230
		$manager = \Aimeos\MShop::create( $this->context(), 'attribute/lists' );
231
232
		$search = $manager->filter();
233
		$search->setSortations( [$search->sort( '-', 'attribute.lists.ctime' )] );
234
235
		$search = $this->initCriteria( $search, $params );
236
		$expr = [
237
			$search->getConditions(),
238
			$search->compare( '==', 'attribute.lists.parentid', $item->getId() ),
239
			$search->compare( '==', 'attribute.lists.domain', 'product' ),
240
		];
241
		$search->setConditions( $search->and( $expr ) );
242
243
		return $manager->search( $search, [], $total );
244
	}
245
246
247
	/**
248
	 * Returns the product items referenced by the given list items
249
	 *
250
	 * @param \Aimeos\Map $listItems Attribute list items implementing \Aimeos\MShop\Common\Item\List\Iface and referencing the products
251
	 * @return \Aimeos\Map List of product IDs as keys and items implementing \Aimeos\MShop\Product\Item\Iface
252
	 */
253
	protected function getProductItems( \Aimeos\Map $listItems ) : \Aimeos\Map
254
	{
255
		$list = $listItems->getRefId()->toArray();
256
		$manager = \Aimeos\MShop::create( $this->context(), 'product' );
257
258
		$search = $manager->filter()->slice( 0, count( $list ) );
259
		$search->setConditions( $search->compare( '==', 'product.id', $list ) );
260
261
		return $manager->search( $search );
262
	}
263
264
265
	/**
266
	 * Returns the list of sub-client names configured for the client.
267
	 *
268
	 * @return array List of JQAdm client names
269
	 */
270
	protected function getSubClientNames() : array
271
	{
272
		/** admin/jqadm/attribute/product/subparts
273
		 * List of JQAdm sub-clients rendered within the attribute product section
274
		 *
275
		 * The output of the frontend is composed of the code generated by the JQAdm
276
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
277
		 * that are responsible for rendering certain sub-parts of the output. The
278
		 * sub-clients can contain JQAdm clients themselves and therefore a
279
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
280
		 * the output that is placed inside the container of its parent.
281
		 *
282
		 * At first, always the JQAdm code generated by the parent is printed, then
283
		 * the JQAdm code of its sub-clients. The product of the JQAdm sub-clients
284
		 * determines the product of the output of these sub-clients inside the parent
285
		 * container. If the configured list of clients is
286
		 *
287
		 *  array( "subclient1", "subclient2" )
288
		 *
289
		 * you can easily change the product of the output by reproducting the subparts:
290
		 *
291
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
292
		 *
293
		 * You can also remove one or more parts if they shouldn't be rendered:
294
		 *
295
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
296
		 *
297
		 * As the clients only generates structural JQAdm, the layout defined via CSS
298
		 * should support adding, removing or reproducting content by a fluid like
299
		 * design.
300
		 *
301
		 * @param array List of sub-client names
302
		 * @since 2023.10
303
		 */
304
		return $this->context()->config()->get( 'admin/jqadm/attribute/product/subparts', [] );
305
	}
306
307
308
	/**
309
	 * Creates new and updates existing items using the data array
310
	 *
311
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object without referenced domain items
312
	 * @param array $data Data array
313
	 * @return \Aimeos\MShop\Attribute\Item\Iface Modified coupon item
314
	 */
315
	protected function fromArray( \Aimeos\MShop\Attribute\Item\Iface $item, array $data ) : \Aimeos\MShop\Attribute\Item\Iface
316
	{
317
		$listIds = $this->val( $data, 'attribute.lists.id', [] );
318
		$listManager = \Aimeos\MShop::create( $this->context(), 'attribute/lists' );
319
320
		$search = $listManager->filter()->slice( 0, count( $listIds ) );
321
		$search->setConditions( $search->compare( '==', 'attribute.lists.id', $listIds ) );
322
323
		$listItems = $listManager->search( $search );
324
325
326
		foreach( (array) $listIds as $idx => $listid )
327
		{
328
			if( isset( $listItems[$listid] ) ) {
329
				$litem = $listItems[$listid];
330
			} else {
331
				$litem = $listManager->create();
332
			}
333
334
			$litem->setParentId( $item->getId() );
335
			$litem->setDomain( 'product' );
336
337
			if( isset( $data['attribute.lists.refid'][$idx] ) ) {
338
				$litem->setRefId( $this->val( $data, 'attribute.lists.refid/' . $idx ) );
339
			}
340
341
			if( isset( $data['attribute.lists.status'][$idx] ) ) {
342
				$litem->setStatus( (int) $this->val( $data, 'attribute.lists.status/' . $idx ) );
343
			}
344
345
			if( isset( $data['attribute.lists.type'][$idx] ) ) {
346
				$litem->setType( $this->val( $data, 'attribute.lists.type/' . $idx ) );
347
			}
348
349
			if( isset( $data['attribute.lists.position'][$idx] ) ) {
350
				$litem->setPosition( (int) $this->val( $data, 'attribute.lists.position/' . $idx ) );
351
			}
352
353
			if( isset( $data['attribute.lists.datestart'][$idx] ) ) {
354
				$litem->setDateStart( $this->val( $data, 'attribute.lists.datestart/' . $idx ) );
355
			}
356
357
			if( isset( $data['attribute.lists.dateend'][$idx] ) ) {
358
				$litem->setDateEnd( $this->val( $data, 'attribute.lists.dateend/' . $idx ) );
359
			}
360
361
			if( isset( $data['attribute.lists.config'][$idx] )
362
				&& ( $conf = json_decode( $this->val( $data, 'attribute.lists.config/' . $idx, '' ), true ) ) !== null
363
			) {
364
				$litem->setConfig( $conf );
365
			}
366
367
			if( $litem->getId() === null && isset( $data['config'][$idx]['key'] ) )
368
			{
369
				$conf = [];
370
371
				foreach( (array) $data['config'][$idx]['key'] as $pos => $key )
372
				{
373
					if( trim( $key ) !== '' && isset( $data['config'][$idx]['val'][$pos] ) ) {
374
						$conf[$key] = $data['config'][$idx]['val'][$pos];
375
					}
376
				}
377
378
				$litem->setConfig( $conf );
379
			}
380
381
			$listManager->save( $litem, false );
382
		}
383
384
		return $item;
385
	}
386
387
388
	/**
389
	 * Constructs the data array for the view from the given item
390
	 *
391
	 * @param \Aimeos\Map $listItems Attribute list items implementing \Aimeos\MShop\Common\Item\Lists\Iface and referencing the products
392
	 * @return string[] Multi-dimensional associative list of item data
393
	 */
394
	protected function toArray( \Aimeos\Map $listItems ) : array
395
	{
396
		$data = [];
397
398
		foreach( $listItems as $listItem )
399
		{
400
			foreach( $listItem->toArray( true ) as $key => $value ) {
401
				$data[$key][] = $value;
402
			}
403
		}
404
405
		return $data;
406
	}
407
408
409
	/**
410
	 * Returns the rendered template including the view data
411
	 *
412
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
413
	 * @return string HTML output
414
	 */
415
	protected function render( \Aimeos\Base\View\Iface $view ) : string
416
	{
417
		/** admin/jqadm/attribute/product/template-item
418
		 * Relative path to the HTML body template of the product subpart for attributes.
419
		 *
420
		 * The template file contains the HTML code and processing instructions
421
		 * to generate the result shown in the body of the frontend. The
422
		 * configuration string is the path to the template file relative
423
		 * to the templates directory (usually in templates/admin/jqadm).
424
		 *
425
		 * You can overwrite the template file configuration in extensions and
426
		 * provide alternative templates. These alternative templates should be
427
		 * named like the default one but with the string "default" replaced by
428
		 * an unique name. You may use the name of your project for this. If
429
		 * you've implemented an alternative client class as well, "default"
430
		 * should be replaced by the name of the new class.
431
		 *
432
		 * @param string Relative path to the template creating the HTML code
433
		 * @since 2023.10
434
		 */
435
		$tplconf = 'admin/jqadm/attribute/product/template-item';
436
		$default = 'attribute/item-product';
437
438
		return $view->render( $view->config( $tplconf, $default ) );
439
	}
440
}
441