Passed
Push — master ( bacc6c...74bd46 )
by Aimeos
03:09
created

Standard::toArray()   F

Complexity

Conditions 15
Paths 864

Size

Total Lines 70
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 32
c 1
b 0
f 0
nc 864
nop 2
dl 0
loc 70
rs 1.9388

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-2023
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Order;
12
13
sprintf( 'sales' ); // for translation
14
sprintf( 'order' ); // for translation
15
16
17
/**
18
 * Default implementation of order 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/order/name
28
	 * Class name of the used account favorite client 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\Order\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Order\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/order/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
		$codes = [];
70
71
		foreach( $this->context()->config()->get( 'common/countries', [] ) as $code ) {
72
			$codes[$code] = $view->translate( 'country', $code );
73
		}
74
75
		asort( $codes );
76
77
		$view->itemSubparts = $this->getSubClientNames();
78
		$view->countries = $codes;
79
		return $view;
80
	}
81
82
83
	/**
84
	 * Batch update of a resource
85
	 *
86
	 * @return string|null Output to display
87
	 */
88
	public function batch() : ?string
89
	{
90
		return $this->batchBase( 'order' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('order') targeting Aimeos\Admin\JQAdm\Base::batchBase() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
91
	}
92
93
94
	/**
95
	 * Copies a resource
96
	 *
97
	 * @return string|null HTML output
98
	 */
99
	public function copy() : ?string
100
	{
101
		$context = $this->context();
102
		$view = $this->object()->data( $this->view() );
103
104
		try
105
		{
106
			if( ( $id = $view->param( 'id' ) ) === null )
107
			{
108
				$msg = $context->translate( 'admin', 'Required parameter "%1$s" is missing' );
109
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
110
			}
111
112
			$manager = \Aimeos\MShop::create( $context, 'order' );
113
			$refs = $context->config()->get( 'mshop/order/manager/subdomains', [] );
114
115
			$view->item = $manager->get( $id, $refs );
116
			$view->itemData = $this->toArray( $view->item, true );
117
			$view->itemBody = parent::copy();
118
		}
119
		catch( \Exception $e )
120
		{
121
			$this->report( $e, 'copy' );
122
		}
123
124
		return $this->render( $view );
125
	}
126
127
128
	/**
129
	 * Creates a new resource
130
	 *
131
	 * @return string|null HTML output
132
	 */
133
	public function create() : ?string
134
	{
135
		$view = $this->object()->data( $this->view() );
136
137
		try
138
		{
139
			$data = $view->param( 'item', [] );
140
141
			if( !isset( $view->item ) ) {
142
				$view->item = \Aimeos\MShop::create( $this->context(), 'order' )->create();
143
			}
144
145
			$data['order.siteid'] = $view->item->getSiteId();
146
147
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
148
			$view->itemBody = parent::create();
149
		}
150
		catch( \Exception $e )
151
		{
152
			$this->report( $e, 'create' );
153
		}
154
155
		return $this->render( $view );
156
	}
157
158
159
	/**
160
	 * Exports a resource
161
	 *
162
	 * @return string Admin output to display
163
	 */
164
	public function export() : ?string
165
	{
166
		$view = $this->view();
167
		$context = $this->context();
168
169
		try
170
		{
171
			$params = $this->storeFilter( $view->param(), 'order' );
172
			$msg = ['sitecode' => $context->locale()->getSiteItem()->getCode()];
173
174
			if( isset( $params['filter'] ) ) {
175
				$msg['filter'] = $this->getCriteriaConditions( (array) $params['filter'] );
176
			}
177
178
			if( isset( $params['sort'] ) ) {
179
				$msg['sort'] = (array) $params['sort'];
180
			}
181
182
			$queue = $view->param( 'queue', 'order-export' );
183
			$mq = $context->queue( 'mq-admin', $queue );
184
			$mq->add( json_encode( $msg ) );
185
186
			$msg = $context->translate( 'admin', 'Your export will be available in a few minutes for download' );
187
			$view->info = $view->get( 'info', [] ) + ['order-item' => $msg];
188
		}
189
		catch( \Exception $e )
190
		{
191
			$this->report( $e, 'export' );
192
		}
193
194
		return $this->search();
195
	}
196
197
198
	/**
199
	 * Returns a single resource
200
	 *
201
	 * @return string|null HTML output
202
	 */
203
	public function get() : ?string
204
	{
205
		$context = $this->context();
206
		$view = $this->object()->data( $this->view() );
207
208
		try
209
		{
210
			if( ( $id = $view->param( 'id' ) ) === null )
211
			{
212
				$msg = $context->translate( 'admin', 'Required parameter "%1$s" is missing' );
213
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
214
			}
215
216
			$manager = \Aimeos\MShop::create( $context, 'order' );
217
			$refs = $context->config()->get( 'mshop/order/manager/subdomains', [] );
218
219
			$view->item = $manager->get( $id, $refs );
220
			$view->itemData = $this->toArray( $view->item );
221
			$view->itemBody = parent::get();
222
		}
223
		catch( \Exception $e )
224
		{
225
			$this->report( $e, 'get' );
226
		}
227
228
		return $this->render( $view );
229
	}
230
231
232
	/**
233
	 * Saves the data
234
	 *
235
	 * @return string|null HTML output
236
	 */
237
	public function save() : ?string
238
	{
239
		$view = $this->view();
240
241
		$manager = \Aimeos\MShop::create( $this->context(), 'order' );
242
		$manager->begin();
243
244
		try
245
		{
246
			$item = $this->fromArray( $view->param( 'item', [] ) );
247
			$view->item = $item->getId() ? $item : $manager->save( clone $item );
248
			$view->itemBody = parent::save();
249
250
			$manager->save( clone $view->item );
251
			$manager->commit();
252
253
			return $this->redirect( 'order', $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

253
			return $this->redirect( 'order', $view->param( 'next' ), /** @scrutinizer ignore-type */ $view->item->getId(), 'save' );
Loading history...
254
		}
255
		catch( \Exception $e )
256
		{
257
			$manager->rollback();
258
			$this->report( $e, 'save' );
259
		}
260
261
		return $this->create();
262
	}
263
264
265
	/**
266
	 * Returns a list of resource according to the conditions
267
	 *
268
	 * @return string|null HTML output
269
	 */
270
	public function search() : ?string
271
	{
272
		$view = $this->view();
273
274
		try
275
		{
276
			$total = 0;
277
			$context = $this->context();
278
			$refs = $context->config()->get( 'mshop/order/manager/subdomains', [] );
279
280
			$manager = \Aimeos\MShop::create( $context, 'order' );
281
			$params = $this->storeFilter( $view->param(), 'order' );
282
283
			$search = $manager->filter( false, true )->order( '-order.id' );
284
			$search = $this->initCriteria( $search, $params );
285
286
			$view->items = $manager->search( $search, $refs, $total );
287
			$view->filterAttributes = $manager->getSearchAttributes( true );
288
			$view->filterOperators = $search->getOperators();
289
			$view->itemBody = parent::search();
290
			$view->total = $total;
291
		}
292
		catch( \Exception $e )
293
		{
294
			$this->report( $e, 'search' );
295
		}
296
297
		/** admin/jqadm/order/template-list
298
		 * Relative path to the HTML body template for the order list.
299
		 *
300
		 * The template file contains the HTML code and processing instructions
301
		 * to generate the result shown in the body of the frontend. The
302
		 * configuration string is the path to the template file relative
303
		 * to the templates directory (usually in templates/admin/jqadm).
304
		 *
305
		 * You can overwrite the template file configuration in extensions and
306
		 * provide alternative templates. These alternative templates should be
307
		 * named like the default one but with the string "default" replaced by
308
		 * an unique name. You may use the name of your project for this. If
309
		 * you've implemented an alternative client class as well, "default"
310
		 * should be replaced by the name of the new class.
311
		 *
312
		 * @param string Relative path to the template creating the HTML code
313
		 * @since 2016.04
314
		 */
315
		$tplconf = 'admin/jqadm/order/template-list';
316
		$default = 'order/list';
317
318
		return $view->render( $view->config( $tplconf, $default ) );
319
	}
320
321
322
	/**
323
	 * Returns the sub-client given by its name.
324
	 *
325
	 * @param string $type Name of the client type
326
	 * @param string|null $name Name of the sub-client (Default if null)
327
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
328
	 */
329
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
330
	{
331
		/** admin/jqadm/order/decorators/excludes
332
		 * Excludes decorators added by the "common" option from the order JQAdm client
333
		 *
334
		 * Decorators extend the functionality of a class by adding new aspects
335
		 * (e.g. log what is currently done), executing the methods of the underlying
336
		 * class only in certain conditions (e.g. only for logged in users) or
337
		 * modify what is returned to the caller.
338
		 *
339
		 * This option allows you to remove a decorator added via
340
		 * "client/jqadm/common/decorators/default" before they are wrapped
341
		 * around the JQAdm client.
342
		 *
343
		 *  admin/jqadm/order/decorators/excludes = array( 'decorator1' )
344
		 *
345
		 * This would remove the decorator named "decorator1" from the list of
346
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
347
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
348
		 *
349
		 * @param array List of decorator names
350
		 * @since 2016.01
351
		 * @see admin/jqadm/common/decorators/default
352
		 * @see admin/jqadm/order/decorators/global
353
		 * @see admin/jqadm/order/decorators/local
354
		 */
355
356
		/** admin/jqadm/order/decorators/global
357
		 * Adds a list of globally available decorators only to the order JQAdm client
358
		 *
359
		 * Decorators extend the functionality of a class by adding new aspects
360
		 * (e.g. log what is currently done), executing the methods of the underlying
361
		 * class only in certain conditions (e.g. only for logged in users) or
362
		 * modify what is returned to the caller.
363
		 *
364
		 * This option allows you to wrap global decorators
365
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
366
		 *
367
		 *  admin/jqadm/order/decorators/global = array( 'decorator1' )
368
		 *
369
		 * This would add the decorator named "decorator1" defined by
370
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
371
		 *
372
		 * @param array List of decorator names
373
		 * @since 2016.01
374
		 * @see admin/jqadm/common/decorators/default
375
		 * @see admin/jqadm/order/decorators/excludes
376
		 * @see admin/jqadm/order/decorators/local
377
		 */
378
379
		/** admin/jqadm/order/decorators/local
380
		 * Adds a list of local decorators only to the order JQAdm client
381
		 *
382
		 * Decorators extend the functionality of a class by adding new aspects
383
		 * (e.g. log what is currently done), executing the methods of the underlying
384
		 * class only in certain conditions (e.g. only for logged in users) or
385
		 * modify what is returned to the caller.
386
		 *
387
		 * This option allows you to wrap local decorators
388
		 * ("\Aimeos\Admin\JQAdm\Order\Decorator\*") around the JQAdm client.
389
		 *
390
		 *  admin/jqadm/order/decorators/local = array( 'decorator2' )
391
		 *
392
		 * This would add the decorator named "decorator2" defined by
393
		 * "\Aimeos\Admin\JQAdm\Order\Decorator\Decorator2" only to the JQAdm client.
394
		 *
395
		 * @param array List of decorator names
396
		 * @since 2016.01
397
		 * @see admin/jqadm/common/decorators/default
398
		 * @see admin/jqadm/order/decorators/excludes
399
		 * @see admin/jqadm/order/decorators/global
400
		 */
401
		return $this->createSubClient( 'order/' . $type, $name );
402
	}
403
404
405
	/**
406
	 * Returns the list of sub-client names configured for the client.
407
	 *
408
	 * @return array List of JQAdm client names
409
	 */
410
	protected function getSubClientNames() : array
411
	{
412
		/** admin/jqadm/order/subparts
413
		 * List of JQAdm sub-clients rendered within the order section
414
		 *
415
		 * The output of the frontend is composed of the code generated by the JQAdm
416
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
417
		 * that are responsible for rendering certain sub-parts of the output. The
418
		 * sub-clients can contain JQAdm clients themselves and therefore a
419
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
420
		 * the output that is placed inside the container of its parent.
421
		 *
422
		 * At first, always the JQAdm code generated by the parent is printed, then
423
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
424
		 * determines the order of the output of these sub-clients inside the parent
425
		 * container. If the configured list of clients is
426
		 *
427
		 *  array( "subclient1", "subclient2" )
428
		 *
429
		 * you can easily change the order of the output by reordering the subparts:
430
		 *
431
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
432
		 *
433
		 * You can also remove one or more parts if they shouldn't be rendered:
434
		 *
435
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
436
		 *
437
		 * As the clients only generates structural JQAdm, the layout defined via CSS
438
		 * should support adding, removing or reordering content by a fluid like
439
		 * design.
440
		 *
441
		 * @param array List of sub-client names
442
		 * @since 2016.01
443
		 */
444
		return $this->context()->config()->get( 'admin/jqadm/order/subparts', [] );
445
	}
446
447
448
	/**
449
	 * Creates new and updates existing items using the data array
450
	 *
451
	 * @param array $data Data array
452
	 * @return \Aimeos\MShop\Order\Item\Iface New order item object
453
	 */
454
	protected function fromArray( array $data ) : \Aimeos\MShop\Order\Item\Iface
455
	{
456
		$context = $this->context();
457
458
		$manager = \Aimeos\MShop::create( $context, 'order' );
459
		$attrManager = \Aimeos\MShop::create( $context, 'order/service/attribute' );
460
461
		if( isset( $data['order.id'] ) ) {
462
			$refs = $context->config()->get( 'mshop/order/manager/subdomains', [] );
463
			$basket = $manager->get( $data['order.id'], $refs )->off();
464
		} else {
465
			$basket = $manager->create()->off();
466
		}
467
468
		$basket->fromArray( $data, true );
469
		$allowed = array_flip( [
470
			'order.product.statusdelivery',
471
			'order.product.statuspayment',
472
			'order.product.qtyopen',
473
			'order.product.timeframe',
474
			'order.product.notes',
475
		] );
476
477
		foreach( $basket->getProducts() as $pos => $product )
478
		{
479
			$list = array_intersect_key( $data['product'][$pos], $allowed );
480
			$product->fromArray( $list );
481
		}
482
483
		foreach( $basket->getAddresses() as $type => $addresses )
484
		{
485
			foreach( $addresses as $pos => $address )
486
			{
487
				if( isset( $data['address'][$type][$pos] ) ) {
488
					$list = (array) $data['address'][$type][$pos];
489
					$basket->addAddress( $address->fromArray( $list, true ), $type, $pos );
490
				} else {
491
					$basket->deleteAddress( $type, $pos );
492
				}
493
			}
494
		}
495
496
		foreach( $basket->getServices() as $type => $services )
497
		{
498
			foreach( $services as $index => $service )
499
			{
500
				$list = [];
501
				$attrItems = $service->getAttributeItems();
502
503
				if( isset( $data['service'][$type][$service->getServiceId()] ) )
504
				{
505
					foreach( (array) $data['service'][$type][$service->getServiceId()] as $key => $pair )
506
					{
507
						foreach( $pair as $pos => $value ) {
508
							$list[$pos][$key] = $value;
509
						}
510
					}
511
512
					foreach( $list as $array )
513
					{
514
						if( isset( $attrItems[$array['order.service.attribute.id']] ) )
515
						{
516
							$attrItem = $attrItems[$array['order.service.attribute.id']];
517
							unset( $attrItems[$array['order.service.attribute.id']] );
518
						}
519
						else
520
						{
521
							$attrItem = $attrManager->create();
522
						}
523
524
						$attrItem->fromArray( $array, true );
525
						$attrItem->setParentId( $service->getId() );
526
527
						$item = $attrManager->save( $attrItem );
0 ignored issues
show
Unused Code introduced by
The assignment to $item is dead and can be removed.
Loading history...
528
					}
529
				}
530
531
				$attrManager->delete( $attrItems->toArray() );
532
			}
533
		}
534
535
		return $basket;
536
	}
537
538
539
	/**
540
	 * Constructs the data array for the view from the given item
541
	 *
542
	 * @param \Aimeos\MShop\Order\Item\Iface $item Order item object
543
	 * @return string[] Multi-dimensional associative list of item data
544
	 */
545
	protected function toArray( \Aimeos\MShop\Order\Item\Iface $item, bool $copy = false ) : array
546
	{
547
		$siteId = $this->context()->locale()->getSiteId();
548
		$data = $item->toArray( true );
549
550
		if( $item->getCustomerId() != '' )
551
		{
552
			try {
553
				$data += \Aimeos\MShop::create( $this->context(), 'customer' )->get( $item->getCustomerId() )->toArray();
554
			} catch( \Exception $e ) {};
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
555
		}
556
557
558
		if( $copy === true )
559
		{
560
			$data['order.siteid'] = $siteId;
561
			$data['order.id'] = '';
562
		}
563
564
		foreach( $item->getAddresses() as $type => $addresses )
565
		{
566
			foreach( $addresses as $pos => $addrItem )
567
			{
568
				$data['address'][$type][$pos] = $addrItem->toArray( true );
569
570
				if( $copy === true )
571
				{
572
					$data['address'][$type][$pos]['order.address.siteid'] = $siteId;
573
					$data['address'][$type][$pos]['order.address.id'] = '';
574
				}
575
			}
576
		}
577
578
		foreach( $item->getProducts() as $pos => $services )
579
		{
580
			foreach( $item->getProducts() as $pos => $productItem )
0 ignored issues
show
Comprehensibility Bug introduced by
$pos is overwriting a variable from outer foreach loop.
Loading history...
581
			{
582
				$data['product'][$pos] = $productItem->toArray( true );
583
584
				foreach( $productItem->getAttributeItems() as $attrItem ) {
585
					$data['product'][$pos]['attributes'][] = $attrItem->toArray( true );
586
				}
587
588
				if( $copy === true )
589
				{
590
					$data['product'][$pos]['order.product.siteid'] = $siteId;
591
					$data['product'][$pos]['order.product.id'] = '';
592
				}
593
			}
594
		}
595
596
		foreach( $item->getServices() as $type => $services )
597
		{
598
			foreach( $services as $pos => $serviceItem )
599
			{
600
				$data['service'][$type][$pos] = $serviceItem->toArray( true );
601
602
				foreach( $serviceItem->getAttributeItems() as $attrItem ) {
603
					$data['service'][$type][$pos]['attributes'][] = $attrItem->toArray( true );
604
				}
605
606
				if( $copy === true )
607
				{
608
					$data['service'][$type][$pos]['order.service.siteid'] = $siteId;
609
					$data['service'][$type][$pos]['order.service.id'] = '';
610
				}
611
			}
612
		}
613
614
		return $data;
615
	}
616
617
618
	/**
619
	 * Returns the rendered template including the view data
620
	 *
621
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
622
	 * @return string HTML output
623
	 */
624
	protected function render( \Aimeos\Base\View\Iface $view ) : string
625
	{
626
		/** admin/jqadm/order/template-item
627
		 * Relative path to the HTML body template for the order item.
628
		 *
629
		 * The template file contains the HTML code and processing instructions
630
		 * to generate the result shown in the body of the frontend. The
631
		 * configuration string is the path to the template file relative
632
		 * to the templates directory (usually in templates/admin/jqadm).
633
		 *
634
		 * You can overwrite the template file configuration in extensions and
635
		 * provide alternative templates. These alternative templates should be
636
		 * named like the default one but with the string "default" replaced by
637
		 * an unique name. You may use the name of your project for this. If
638
		 * you've implemented an alternative client class as well, "default"
639
		 * should be replaced by the name of the new class.
640
		 *
641
		 * @param string Relative path to the template creating the HTML code
642
		 * @since 2016.04
643
		 */
644
		$tplconf = 'admin/jqadm/order/template-item';
645
		$default = 'order/item';
646
647
		return $view->render( $view->config( $tplconf, $default ) );
648
	}
649
}
650