Standard   C
last analyzed

Complexity

Total Complexity 54

Size/Duplication

Total Lines 599
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 160
c 5
b 0
f 0
dl 0
loc 599
rs 6.4799
wmc 54

14 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 23 3
A getDomains() 0 13 1
A getSubClientNames() 0 35 1
A getSubClient() 0 73 1
A get() 0 24 3
A copy() 0 24 3
A data() 0 18 2
B delete() 0 54 6
B batch() 0 31 9
A search() 0 44 2
A save() 0 25 3
C fromArray() 0 36 13
A toArray() 0 21 6
A render() 0 24 1

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-2025
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 customer 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\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
	 */
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
		$manager = \Aimeos\MShop::create( $this->context(), 'group' );
78
		$filter = $manager->filter()->slice( 0, 1000 );
79
80
		$view->itemSubparts = $this->getSubClientNames();
81
		$view->itemGroups = $manager->search( $filter );
82
		$view->countries = $codes;
83
84
		return $view;
85
	}
86
87
88
	/**
89
	 * Batch update of a resource
90
	 *
91
	 * @return string|null Output to display
92
	 */
93
	public function batch() : ?string
94
	{
95
		$view = $this->view();
96
97
		if( !empty( $ids = $view->param( 'id' ) ) )
98
		{
99
			$context = $this->context();
100
			$manager = \Aimeos\MShop::create( $context, 'customer' );
101
			$filter = $manager->filter()->add( ['customer.id' => $ids] )->slice( 0, count( $ids ) );
102
			$items = $manager->search( $filter );
103
104
			$data = $view->param( 'item', [] );
105
106
			foreach( $items as $item )
107
			{
108
				if( $view->access( ['super', 'admin'] ) || $item->getId() === $context->user() )
109
				{
110
					!isset( $data['customer.password'] ) ?: $item->setPassword( $data['customer.password'] );
111
					!isset( $data['groups'] ) ?: $item->setGroups( array_filter( (array) $data['groups'] ) );
112
				}
113
114
				!isset( $data['customer.dateverified'] ) ?: $item->setDateVerified( $data['customer.dateverified'] );
115
				!isset( $data['customer.status'] ) ?: $item->setStatus( $data['customer.status'] );
116
117
				$temp = $data; $item->fromArray( $temp );
118
			}
119
120
			$manager->save( $items );
121
		}
122
123
		return $this->redirect( 'customer', 'search', null, 'save' );
124
	}
125
126
127
	/**
128
	 * Copies a resource
129
	 *
130
	 * @return string|null HTML output
131
	 */
132
	public function copy() : ?string
133
	{
134
		$view = $this->object()->data( $this->view() );
135
136
		try
137
		{
138
			if( ( $id = $view->param( 'id' ) ) === null )
139
			{
140
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
141
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
142
			}
143
144
			$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
145
			$view->item = $manager->get( $id, $this->getDomains() );
146
147
			$view->itemData = $this->toArray( $view->item, true );
148
			$view->itemBody = parent::copy();
149
		}
150
		catch( \Exception $e )
151
		{
152
			$this->report( $e, 'copy' );
153
		}
154
155
		return $this->render( $view );
156
	}
157
158
159
	/**
160
	 * Creates a new resource
161
	 *
162
	 * @return string|null HTML output
163
	 */
164
	public function create() : ?string
165
	{
166
		$view = $this->object()->data( $this->view() );
167
168
		try
169
		{
170
			$data = $view->param( 'item', [] );
171
172
			if( !isset( $view->item ) ) {
173
				$view->item = \Aimeos\MShop::create( $this->context(), 'customer' )->create();
174
			}
175
176
			$data['customer.siteid'] = $view->item->getSiteId();
177
178
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
179
			$view->itemBody = parent::create();
180
		}
181
		catch( \Exception $e )
182
		{
183
			$this->report( $e, 'create' );
184
		}
185
186
		return $this->render( $view );
187
	}
188
189
190
	/**
191
	 * Deletes a resource
192
	 *
193
	 * @return string|null HTML output
194
	 */
195
	public function delete() : ?string
196
	{
197
		$view = $this->view();
198
		$context = $this->context();
199
200
		$manager = \Aimeos\MShop::create( $context, 'customer' );
201
		$manager->begin();
202
203
		try
204
		{
205
			if( ( $ids = $view->param( 'id' ) ) === null )
206
			{
207
				$msg = $context->translate( 'admin', 'Required parameter "%1$s" is missing' );
208
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
209
			}
210
211
			if( !$view->access( ['super', 'admin'] ) )
212
			{
213
				$msg = $context->translate( 'admin', 'Only super users and administrators can delete entries' );
214
				throw new \Aimeos\Admin\JQAdm\Exception( $msg );
215
			}
216
217
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
218
			$search->add( $search->and( [
219
				$search->compare( '==', 'customer.id', $ids ),
220
				$search->compare( '!=', 'customer.siteid', '' )
221
			] ) );
222
223
			$items = $manager->search( $search, $this->getDomains() );
224
225
			foreach( $items as $item )
226
			{
227
				$view->item = $item;
228
				parent::delete();
229
			}
230
231
			$manager->delete( $items );
232
			$manager->commit();
233
234
			if( $items->count() !== count( (array) $ids ) )
235
			{
236
				$msg = $context->translate( 'admin', 'Not all entries could be deleted' );
237
				throw new \Aimeos\Admin\JQAdm\Exception( $msg );
238
			}
239
240
			return $this->redirect( 'customer', 'search', null, 'delete' );
241
		}
242
		catch( \Exception $e )
243
		{
244
			$manager->rollback();
245
			$this->report( $e, 'delete' );
246
		}
247
248
		return $this->search();
249
	}
250
251
252
	/**
253
	 * Returns a single resource
254
	 *
255
	 * @return string|null HTML output
256
	 */
257
	public function get() : ?string
258
	{
259
		$view = $this->object()->data( $this->view() );
260
261
		try
262
		{
263
			if( ( $id = $view->param( 'id' ) ) === null )
264
			{
265
				$msg = $this->context()->translate( 'admin', 'Required parameter "%1$s" is missing' );
266
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( $msg, 'id' ) );
267
			}
268
269
			$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
270
271
			$view->item = $manager->get( $id, $this->getDomains() );
272
			$view->itemData = $this->toArray( $view->item );
273
			$view->itemBody = parent::get();
274
		}
275
		catch( \Exception $e )
276
		{
277
			$this->report( $e, 'get' );
278
		}
279
280
		return $this->render( $view );
281
	}
282
283
284
	/**
285
	 * Saves the data
286
	 *
287
	 * @return string|null HTML output
288
	 */
289
	public function save() : ?string
290
	{
291
		$view = $this->view();
292
293
		$manager = \Aimeos\MShop::create( $this->context(), 'customer' );
294
		$manager->begin();
295
296
		try
297
		{
298
			$item = $this->fromArray( $view->param( 'item', [] ) );
299
			$view->item = $item->getId() ? $item : $manager->save( $item );
300
			$view->itemBody = parent::save();
301
302
			$manager->save( clone $view->item );
303
			$manager->commit();
304
305
			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

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