Passed
Push — master ( d343ad...3614c7 )
by Aimeos
03:04
created

Standard   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 606
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 157
dl 0
loc 606
rs 6.96
c 0
b 0
f 0
wmc 53

15 Methods

Rating   Name   Duplication   Size   Complexity  
A data() 0 13 2
A toArray() 0 16 4
A render() 0 25 1
A create() 0 24 3
A getDomains() 0 14 1
A copy() 0 25 3
B fromArray() 0 28 9
B delete() 0 54 6
A getSubClientNames() 0 36 1
A batch() 0 3 1
A getSubClient() 0 76 1
C getGroupItems() 0 33 13
A get() 0 25 3
A search() 0 45 2
A save() 0 25 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), 2017-2022
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Customer;
12
13
sprintf( 'users' ); // for translation
14
sprintf( 'customer' ); // for translation
15
16
17
/**
18
 * Default implementation of customer 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/customer/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\Customer\Standard
38
	 *
39
	 * and you want to replace it with your own version named
40
	 *
41
	 *  \Aimeos\Admin\JQAdm\Customer\Myfavorite
42
	 *
43
	 * then you have to set the this configuration option:
44
	 *
45
	 *  admin/jqadm/customer/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
	 * @category Developer
59
	 */
60
61
62
	/**
63
	 * Adds the required data used in the template
64
	 *
65
	 * @param \Aimeos\Base\View\Iface $view View object
66
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
67
	 */
68
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
69
	{
70
		$codes = [];
71
72
		foreach( $this->context()->config()->get( 'common/countries', [] ) as $code ) {
73
			$codes[$code] = $view->translate( 'country', $code );
74
		}
75
76
		asort( $codes );
77
78
		$view->itemSubparts = $this->getSubClientNames();
79
		$view->countries = $codes;
80
		return $view;
81
	}
82
83
84
	/**
85
	 * Batch update of a resource
86
	 *
87
	 * @return string|null Output to display
88
	 */
89
	public function batch() : ?string
90
	{
91
		return $this->batchBase( 'customer' );
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->batchBase('customer') 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...
92
	}
93
94
95
	/**
96
	 * Copies a resource
97
	 *
98
	 * @return string|null HTML output
99
	 */
100
	public function copy() : ?string
101
	{
102
		$view = $this->object()->data( $this->view() );
103
104
		try
105
		{
106
			if( ( $id = $view->param( 'id' ) ) === null )
107
			{
108
				$msg = $this->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( $this->context(), 'customer' );
113
			$view->item = $manager->get( $id, $this->getDomains() );
114
115
			$view->itemGroups = $this->getGroupItems( $view->item );
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(), 'customer' )->create();
143
			}
144
145
			$data['customer.siteid'] = $view->item->getSiteId();
146
147
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
148
			$view->itemGroups = $this->getGroupItems( $view->item );
149
			$view->itemBody = parent::create();
150
		}
151
		catch( \Exception $e )
152
		{
153
			$this->report( $e, 'create' );
154
		}
155
156
		return $this->render( $view );
157
	}
158
159
160
	/**
161
	 * Deletes a resource
162
	 *
163
	 * @return string|null HTML output
164
	 */
165
	public function delete() : ?string
166
	{
167
		$view = $this->view();
168
		$context = $this->context();
169
170
		$manager = \Aimeos\MShop::create( $context, 'customer' );
171
		$manager->begin();
172
173
		try
174
		{
175
			if( ( $ids = $view->param( 'id' ) ) === null )
176
			{
177
				$msg = $context->translate( 'admin', 'Required parameter "%1$s" is missing' );
178
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
179
			}
180
181
			if( !$view->access( ['super', 'admin'] ) )
182
			{
183
				$msg = $context->translate( 'admin', 'Only super users and administrators can delete items' );
184
				throw new \Aimeos\Admin\JQAdm\Exception( $msg );
185
			}
186
187
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
188
			$search->add( $search->and( [
189
				$search->compare( '==', 'customer.id', $ids ),
190
				$search->compare( '!=', 'customer.siteid', '' )
191
			] ) );
192
193
			$items = $manager->search( $search, $this->getDomains() );
194
195
			foreach( $items as $item )
196
			{
197
				$view->item = $item;
198
				parent::delete();
199
			}
200
201
			$manager->delete( $items->toArray() );
202
			$manager->commit();
203
204
			if( $items->count() !== count( (array) $ids ) )
205
			{
206
				$msg = $context->translate( 'admin', 'Not all items could be deleted' );
207
				throw new \Aimeos\Admin\JQAdm\Exception( $msg );
208
			}
209
210
			return $this->redirect( 'customer', 'search', null, 'delete' );
211
		}
212
		catch( \Exception $e )
213
		{
214
			$manager->rollback();
215
			$this->report( $e, 'delete' );
216
		}
217
218
		return $this->search();
219
	}
220
221
222
	/**
223
	 * Returns a single resource
224
	 *
225
	 * @return string|null HTML output
226
	 */
227
	public function get() : ?string
228
	{
229
		$view = $this->object()->data( $this->view() );
230
231
		try
232
		{
233
			if( ( $id = $view->param( 'id' ) ) === null )
234
			{
235
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
236
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
237
			}
238
239
			$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
240
241
			$view->item = $manager->get( $id, $this->getDomains() );
242
			$view->itemGroups = $this->getGroupItems( $view->item );
243
			$view->itemData = $this->toArray( $view->item );
244
			$view->itemBody = parent::get();
245
		}
246
		catch( \Exception $e )
247
		{
248
			$this->report( $e, 'get' );
249
		}
250
251
		return $this->render( $view );
252
	}
253
254
255
	/**
256
	 * Saves the data
257
	 *
258
	 * @return string|null HTML output
259
	 */
260
	public function save() : ?string
261
	{
262
		$view = $this->view();
263
264
		$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
265
		$manager->begin();
266
267
		try
268
		{
269
			$item = $this->fromArray( $view->param( 'item', [] ) );
270
			$view->item = $item->getId() ? $item : $manager->save( $item );
271
			$view->itemBody = parent::save();
272
273
			$manager->save( clone $view->item );
274
			$manager->commit();
275
276
			return $this->redirect( 'customer', $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

276
			return $this->redirect( 'customer', $view->param( 'next' ), /** @scrutinizer ignore-type */ $view->item->getId(), 'save' );
Loading history...
277
		}
278
		catch( \Exception $e )
279
		{
280
			$manager->rollback();
281
			$this->report( $e, 'save' );
282
		}
283
284
		return $this->create();
285
	}
286
287
288
	/**
289
	 * Returns a list of resource according to the conditions
290
	 *
291
	 * @return string|null HTML output
292
	 */
293
	public function search() : ?string
294
	{
295
		$view = $this->view();
296
297
		try
298
		{
299
			$total = 0;
300
			$params = $this->storeFilter( $view->param(), 'customer' );
301
			$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
302
			$search = $this->initCriteria( $manager->filter(), $params );
303
304
			$view->items = $manager->search( $search, $this->getDomains(), $total );
305
			$view->filterAttributes = $manager->getSearchAttributes( true );
306
			$view->filterOperators = $search->getOperators();
307
			$view->itemBody = parent::search();
308
			$view->total = $total;
309
		}
310
		catch( \Exception $e )
311
		{
312
			$this->report( $e, 'search' );
313
		}
314
315
		/** admin/jqadm/customer/template-list
316
		 * Relative path to the HTML body template for the customer list.
317
		 *
318
		 * The template file contains the HTML code and processing instructions
319
		 * to generate the result shown in the body of the frontend. The
320
		 * configuration string is the path to the template file relative
321
		 * to the templates directory (usually in admin/jqadm/templates).
322
		 *
323
		 * You can overwrite the template file configuration in extensions and
324
		 * provide alternative templates. These alternative templates should be
325
		 * named like the default one but with the string "default" replaced by
326
		 * an unique name. You may use the name of your project for this. If
327
		 * you've implemented an alternative client class as well, "default"
328
		 * should be replaced by the name of the new class.
329
		 *
330
		 * @param string Relative path to the template creating the HTML code
331
		 * @since 2016.04
332
		 * @category Developer
333
		 */
334
		$tplconf = 'admin/jqadm/customer/template-list';
335
		$default = 'customer/list';
336
337
		return $view->render( $view->config( $tplconf, $default ) );
338
	}
339
340
341
	/**
342
	 * Returns the sub-client given by its name.
343
	 *
344
	 * @param string $type Name of the client type
345
	 * @param string|null $name Name of the sub-client (Default if null)
346
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
347
	 */
348
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
349
	{
350
		/** admin/jqadm/customer/decorators/excludes
351
		 * Excludes decorators added by the "common" option from the customer JQAdm client
352
		 *
353
		 * Decorators extend the functionality of a class by adding new aspects
354
		 * (e.g. log what is currently done), executing the methods of the underlying
355
		 * class only in certain conditions (e.g. only for logged in users) or
356
		 * modify what is returned to the caller.
357
		 *
358
		 * This option allows you to remove a decorator added via
359
		 * "client/jqadm/common/decorators/default" before they are wrapped
360
		 * around the JQAdm client.
361
		 *
362
		 *  admin/jqadm/customer/decorators/excludes = array( 'decorator1' )
363
		 *
364
		 * This would remove the decorator named "decorator1" from the list of
365
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
366
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
367
		 *
368
		 * @param array List of decorator names
369
		 * @since 2017.07
370
		 * @category Developer
371
		 * @see admin/jqadm/common/decorators/default
372
		 * @see admin/jqadm/customer/decorators/global
373
		 * @see admin/jqadm/customer/decorators/local
374
		 */
375
376
		/** admin/jqadm/customer/decorators/global
377
		 * Adds a list of globally available decorators only to the customer JQAdm client
378
		 *
379
		 * Decorators extend the functionality of a class by adding new aspects
380
		 * (e.g. log what is currently done), executing the methods of the underlying
381
		 * class only in certain conditions (e.g. only for logged in users) or
382
		 * modify what is returned to the caller.
383
		 *
384
		 * This option allows you to wrap global decorators
385
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
386
		 *
387
		 *  admin/jqadm/customer/decorators/global = array( 'decorator1' )
388
		 *
389
		 * This would add the decorator named "decorator1" defined by
390
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
391
		 *
392
		 * @param array List of decorator names
393
		 * @since 2017.07
394
		 * @category Developer
395
		 * @see admin/jqadm/common/decorators/default
396
		 * @see admin/jqadm/customer/decorators/excludes
397
		 * @see admin/jqadm/customer/decorators/local
398
		 */
399
400
		/** admin/jqadm/customer/decorators/local
401
		 * Adds a list of local decorators only to the customer JQAdm client
402
		 *
403
		 * Decorators extend the functionality of a class by adding new aspects
404
		 * (e.g. log what is currently done), executing the methods of the underlying
405
		 * class only in certain conditions (e.g. only for logged in users) or
406
		 * modify what is returned to the caller.
407
		 *
408
		 * This option allows you to wrap local decorators
409
		 * ("\Aimeos\Admin\JQAdm\Customer\Decorator\*") around the JQAdm client.
410
		 *
411
		 *  admin/jqadm/customer/decorators/local = array( 'decorator2' )
412
		 *
413
		 * This would add the decorator named "decorator2" defined by
414
		 * "\Aimeos\Admin\JQAdm\Customer\Decorator\Decorator2" only to the JQAdm client.
415
		 *
416
		 * @param array List of decorator names
417
		 * @since 2017.07
418
		 * @category Developer
419
		 * @see admin/jqadm/common/decorators/default
420
		 * @see admin/jqadm/customer/decorators/excludes
421
		 * @see admin/jqadm/customer/decorators/global
422
		 */
423
		return $this->createSubClient( 'customer/' . $type, $name );
424
	}
425
426
427
	/**
428
	 * Returns the domain names whose items should be fetched too
429
	 *
430
	 * @return string[] List of domain names
431
	 */
432
	protected function getDomains() : array
433
	{
434
		/** admin/jqadm/customer/domains
435
		 * List of domain items that should be fetched along with the customer
436
		 *
437
		 * If you need to display additional content, you can configure your own
438
		 * list of domains (attribute, media, price, customer, text, etc. are
439
		 * domains) whose items are fetched from the storage.
440
		 *
441
		 * @param array List of domain names
442
		 * @since 2017.07
443
		 * @category Developer
444
		 */
445
		return $this->context()->config()->get( 'admin/jqadm/customer/domains', [] );
446
	}
447
448
449
	/**
450
	 * Returns the available group items
451
	 *
452
	 * @param \Aimeos\MShop\Customer\Item\Iface|null $item Customer item that should be updated
453
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface[] Associative list of group IDs as keys and group items as values
454
	 */
455
	protected function getGroupItems( \Aimeos\MShop\Customer\Item\Iface $item = null ) : array
456
	{
457
		$list = [];
458
		$view = $this->view();
459
		$context = $this->context();
460
461
		$isSuper = $view->access( ['super'] );
462
		$isAdmin = $view->access( ['admin'] );
463
		$isEditor = $view->access( ['editor'] );
464
465
		$manager = \Aimeos\MShop::create( $context, 'customer/group' );
466
		$search = $manager->filter( true )->slice( 0, 10000 )->order( 'customer.group.label' );
467
468
		foreach( $manager->search( $search ) as $groupId => $groupItem )
469
		{
470
			if( !$isSuper && $groupItem->getCode() === 'super' ) {
471
				continue;
472
			}
473
474
			if( !$isSuper && !$isAdmin && $groupItem->getCode() === 'admin' ) {
475
				continue;
476
			}
477
478
			if( !$isSuper && !$isAdmin && $groupItem->getCode() === 'editor'
479
				&& ( !$isEditor || $item === null || (string) $context->user() !== (string) $item->getId() )
480
			) {
481
				continue;
482
			}
483
484
			$list[$groupId] = $groupItem;
485
		}
486
487
		return $list;
488
	}
489
490
491
	/**
492
	 * Returns the list of sub-client names configured for the client.
493
	 *
494
	 * @return array List of JQAdm client names
495
	 */
496
	protected function getSubClientNames() : array
497
	{
498
		/** admin/jqadm/customer/subparts
499
		 * List of JQAdm sub-clients rendered within the customer section
500
		 *
501
		 * The output of the frontend is composed of the code generated by the JQAdm
502
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
503
		 * that are responsible for rendering certain sub-parts of the output. The
504
		 * sub-clients can contain JQAdm clients themselves and therefore a
505
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
506
		 * the output that is placed inside the container of its parent.
507
		 *
508
		 * At first, always the JQAdm code generated by the parent is printed, then
509
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
510
		 * determines the order of the output of these sub-clients inside the parent
511
		 * container. If the configured list of clients is
512
		 *
513
		 *  array( "subclient1", "subclient2" )
514
		 *
515
		 * you can easily change the order of the output by reordering the subparts:
516
		 *
517
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
518
		 *
519
		 * You can also remove one or more parts if they shouldn't be rendered:
520
		 *
521
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
522
		 *
523
		 * As the clients only generates structural JQAdm, the layout defined via CSS
524
		 * should support adding, removing or reordering content by a fluid like
525
		 * design.
526
		 *
527
		 * @param array List of sub-client names
528
		 * @since 2017.07
529
		 * @category Developer
530
		 */
531
		return $this->context()->config()->get( 'admin/jqadm/customer/subparts', [] );
532
	}
533
534
535
536
	/**
537
	 * Creates new and updates existing items using the data array
538
	 *
539
	 * @param array $data Data array
540
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item object
541
	 */
542
	protected function fromArray( array $data ) : \Aimeos\MShop\Customer\Item\Iface
543
	{
544
		$context = $this->context();
545
		$manager = \Aimeos\MShop::create( $context, 'customer' );
546
547
		if( isset( $data['customer.id'] ) && $data['customer.id'] != '' ) {
548
			$item = $manager->get( $data['customer.id'], $this->getDomains() );
549
		} else {
550
			$item = $manager->create();
551
		}
552
553
		$addr = $item->getPaymentAddress();
554
		$label = ( $addr->getFirstname() ? $addr->getFirstname() . ' ' : '' ) . $addr->getLastname();
555
		$label .= ( $addr->getCompany() ? ' (' . $addr->getCompany() . ')' : '' );
556
557
		$groupIds = $this->val( $data, 'customer.groups', [] );
558
		$gids = array_keys( $this->getGroupItems( $item ) );
559
560
		$item->setLabel( $label )->setStatus( $data['customer.status'] ?? 0 )
561
			->setGroups( array_intersect( $gids, $groupIds ) );
562
563
		if( $this->view()->access( ['super'] ) || $item->getId() === $context->user() )
564
		{
565
			!isset( $data['customer.password'] ) ?: $item->setPassword( $data['customer.password'] );
566
			!isset( $data['customer.code'] ) ?: $item->setCode( $data['customer.code'] );
567
		}
568
569
		return $item->fromArray( $data );
570
	}
571
572
573
	/**
574
	 * Constructs the data array for the view from the given item
575
	 *
576
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
577
	 * @return string[] Multi-dimensional associative list of item data
578
	 */
579
	protected function toArray( \Aimeos\MShop\Customer\Item\Iface $item, bool $copy = false ) : array
580
	{
581
		$data = $item->toArray( true );
582
583
		if( $this->view()->access( ['super'] ) || $item->getId() === $this->context()->user() ) {
584
			$data['.modify'] = true;
585
		}
586
587
		if( $copy === true )
588
		{
589
			$data['customer.siteid'] = $this->context()->locale()->getSiteId();
590
			$data['customer.code'] = '';
591
			$data['customer.id'] = '';
592
		}
593
594
		return $data;
595
	}
596
597
598
	/**
599
	 * Returns the rendered template including the view data
600
	 *
601
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
602
	 * @return string HTML output
603
	 */
604
	protected function render( \Aimeos\Base\View\Iface $view ) : string
605
	{
606
		/** admin/jqadm/customer/template-item
607
		 * Relative path to the HTML body template for the customer item.
608
		 *
609
		 * The template file contains the HTML code and processing instructions
610
		 * to generate the result shown in the body of the frontend. The
611
		 * configuration string is the path to the template file relative
612
		 * to the templates directory (usually in admin/jqadm/templates).
613
		 *
614
		 * You can overwrite the template file configuration in extensions and
615
		 * provide alternative templates. These alternative templates should be
616
		 * named like the default one but with the string "default" replaced by
617
		 * an unique name. You may use the name of your project for this. If
618
		 * you've implemented an alternative client class as well, "default"
619
		 * should be replaced by the name of the new class.
620
		 *
621
		 * @param string Relative path to the template creating the HTML code
622
		 * @since 2016.04
623
		 * @category Developer
624
		 */
625
		$tplconf = 'admin/jqadm/customer/template-item';
626
		$default = 'customer/item';
627
628
		return $view->render( $view->config( $tplconf, $default ) );
629
	}
630
}
631