Passed
Push — master ( 033ba7...92bdf9 )
by Aimeos
14:09
created

Standard::getSubClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 76
rs 10
c 0
b 0
f 0

How to fix   Long Method   

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\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
	/**
28
	 * Adds the required data used in the template
29
	 *
30
	 * @param \Aimeos\MW\View\Iface $view View object
31
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
32
	 */
33
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
34
	{
35
		$view->itemSubparts = $this->getSubClientNames();
36
		return $view;
37
	}
38
39
40
	/**
41
	 * Copies a resource
42
	 *
43
	 * @return string|null HTML output
44
	 */
45
	public function copy() : ?string
46
	{
47
		$view = $this->getObject()->addData( $this->getView() );
48
49
		try
50
		{
51
			if( ( $id = $view->param( 'id' ) ) === null ) {
52
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
53
			}
54
55
			$manager = \Aimeos\MShop::create( $this->getContext(), 'customer' );
56
			$view->item = $manager->get( $id, $this->getDomains() );
57
58
			$view->itemGroups = $this->getGroupItems( $view->item );
59
			$view->itemData = $this->toArray( $view->item, true );
60
			$view->itemBody = parent::copy();
61
		}
62
		catch( \Exception $e )
63
		{
64
			$this->report( $e, 'copy' );
65
		}
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->getObject()->addData( $this->getView() );
79
80
		try
81
		{
82
			$data = $view->param( 'item', [] );
83
84
			if( !isset( $view->item ) ) {
85
				$view->item = \Aimeos\MShop::create( $this->getContext(), 'customer' )->create();
86
			}
87
88
			$data['customer.siteid'] = $view->item->getSiteId();
89
90
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
91
			$view->itemGroups = $this->getGroupItems( $view->item );
92
			$view->itemBody = parent::create();
93
		}
94
		catch( \Exception $e )
95
		{
96
			$this->report( $e, 'create' );
97
		}
98
99
		return $this->render( $view );
100
	}
101
102
103
	/**
104
	 * Deletes a resource
105
	 *
106
	 * @return string|null HTML output
107
	 */
108
	public function delete() : ?string
109
	{
110
		$view = $this->getView();
111
112
		$manager = \Aimeos\MShop::create( $this->getContext(), 'customer' );
113
		$manager->begin();
114
115
		try
116
		{
117
			if( ( $ids = $view->param( 'id' ) ) === null ) {
118
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
119
			}
120
121
			$search = $manager->filter()->slice( 0, count( (array) $ids ) );
122
			$search->setConditions( $search->compare( '==', 'customer.id', $ids ) );
123
			$items = $manager->search( $search, $this->getDomains() );
124
125
			foreach( $items as $item )
126
			{
127
				$view->item = $item;
128
				parent::delete();
129
			}
130
131
			$manager->delete( $items->toArray() );
132
			$manager->commit();
133
134
			return $this->redirect( 'customer', 'search', null, 'delete' );
135
		}
136
		catch( \Exception $e )
137
		{
138
			$manager->rollback();
139
			$this->report( $e, 'delete' );
140
		}
141
142
		return $this->search();
143
	}
144
145
146
	/**
147
	 * Returns a single resource
148
	 *
149
	 * @return string|null HTML output
150
	 */
151
	public function get() : ?string
152
	{
153
		$view = $this->getObject()->addData( $this->getView() );
154
155
		try
156
		{
157
			if( ( $id = $view->param( 'id' ) ) === null ) {
158
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
159
			}
160
161
			$manager = \Aimeos\MShop::create( $this->getContext(), 'customer' );
162
163
			$view->item = $manager->get( $id, $this->getDomains() );
164
			$view->itemGroups = $this->getGroupItems( $view->item );
165
			$view->itemData = $this->toArray( $view->item );
166
			$view->itemBody = parent::get();
167
		}
168
		catch( \Exception $e )
169
		{
170
			$this->report( $e, 'get' );
171
		}
172
173
		return $this->render( $view );
174
	}
175
176
177
	/**
178
	 * Saves the data
179
	 *
180
	 * @return string|null HTML output
181
	 */
182
	public function save() : ?string
183
	{
184
		$view = $this->getView();
185
186
		$manager = \Aimeos\MShop::create( $this->getContext(), 'customer' );
187
		$manager->begin();
188
189
		try
190
		{
191
			$item = $this->fromArray( $view->param( 'item', [] ) );
192
			$view->item = $item->getId() ? $item : $manager->save( $item );
193
			$view->itemBody = parent::save();
194
195
			$manager->save( clone $view->item );
196
			$manager->commit();
197
198
			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

198
			return $this->redirect( 'customer', $view->param( 'next' ), /** @scrutinizer ignore-type */ $view->item->getId(), 'save' );
Loading history...
199
		}
200
		catch( \Exception $e )
201
		{
202
			$manager->rollback();
203
			$this->report( $e, 'save' );
204
		}
205
206
		return $this->create();
207
	}
208
209
210
	/**
211
	 * Returns a list of resource according to the conditions
212
	 *
213
	 * @return string|null HTML output
214
	 */
215
	public function search() : ?string
216
	{
217
		$view = $this->getView();
218
219
		try
220
		{
221
			$total = 0;
222
			$params = $this->storeFilter( $view->param(), 'customer' );
223
			$manager = \Aimeos\MShop::create( $this->getContext(), 'customer' );
224
			$search = $this->initCriteria( $manager->filter(), $params );
225
226
			$view->items = $manager->search( $search, $this->getDomains(), $total );
227
			$view->filterAttributes = $manager->getSearchAttributes( true );
228
			$view->filterOperators = $search->getOperators();
229
			$view->itemBody = parent::search();
230
			$view->total = $total;
231
		}
232
		catch( \Exception $e )
233
		{
234
			$this->report( $e, 'search' );
235
		}
236
237
		/** admin/jqadm/customer/template-list
238
		 * Relative path to the HTML body template for the customer list.
239
		 *
240
		 * The template file contains the HTML code and processing instructions
241
		 * to generate the result shown in the body of the frontend. The
242
		 * configuration string is the path to the template file relative
243
		 * to the templates directory (usually in admin/jqadm/templates).
244
		 *
245
		 * You can overwrite the template file configuration in extensions and
246
		 * provide alternative templates. These alternative templates should be
247
		 * named like the default one but with the string "default" replaced by
248
		 * an unique name. You may use the name of your project for this. If
249
		 * you've implemented an alternative client class as well, "default"
250
		 * should be replaced by the name of the new class.
251
		 *
252
		 * @param string Relative path to the template creating the HTML code
253
		 * @since 2016.04
254
		 * @category Developer
255
		 */
256
		$tplconf = 'admin/jqadm/customer/template-list';
257
		$default = 'customer/list-standard';
258
259
		return $view->render( $view->config( $tplconf, $default ) );
260
	}
261
262
263
	/**
264
	 * Returns the sub-client given by its name.
265
	 *
266
	 * @param string $type Name of the client type
267
	 * @param string|null $name Name of the sub-client (Default if null)
268
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
269
	 */
270
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
271
	{
272
		/** admin/jqadm/customer/decorators/excludes
273
		 * Excludes decorators added by the "common" option from the customer JQAdm client
274
		 *
275
		 * Decorators extend the functionality of a class by adding new aspects
276
		 * (e.g. log what is currently done), executing the methods of the underlying
277
		 * class only in certain conditions (e.g. only for logged in users) or
278
		 * modify what is returned to the caller.
279
		 *
280
		 * This option allows you to remove a decorator added via
281
		 * "client/jqadm/common/decorators/default" before they are wrapped
282
		 * around the JQAdm client.
283
		 *
284
		 *  admin/jqadm/customer/decorators/excludes = array( 'decorator1' )
285
		 *
286
		 * This would remove the decorator named "decorator1" from the list of
287
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
288
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
289
		 *
290
		 * @param array List of decorator names
291
		 * @since 2017.07
292
		 * @category Developer
293
		 * @see admin/jqadm/common/decorators/default
294
		 * @see admin/jqadm/customer/decorators/global
295
		 * @see admin/jqadm/customer/decorators/local
296
		 */
297
298
		/** admin/jqadm/customer/decorators/global
299
		 * Adds a list of globally available decorators only to the customer JQAdm client
300
		 *
301
		 * Decorators extend the functionality of a class by adding new aspects
302
		 * (e.g. log what is currently done), executing the methods of the underlying
303
		 * class only in certain conditions (e.g. only for logged in users) or
304
		 * modify what is returned to the caller.
305
		 *
306
		 * This option allows you to wrap global decorators
307
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
308
		 *
309
		 *  admin/jqadm/customer/decorators/global = array( 'decorator1' )
310
		 *
311
		 * This would add the decorator named "decorator1" defined by
312
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
313
		 *
314
		 * @param array List of decorator names
315
		 * @since 2017.07
316
		 * @category Developer
317
		 * @see admin/jqadm/common/decorators/default
318
		 * @see admin/jqadm/customer/decorators/excludes
319
		 * @see admin/jqadm/customer/decorators/local
320
		 */
321
322
		/** admin/jqadm/customer/decorators/local
323
		 * Adds a list of local decorators only to the customer JQAdm client
324
		 *
325
		 * Decorators extend the functionality of a class by adding new aspects
326
		 * (e.g. log what is currently done), executing the methods of the underlying
327
		 * class only in certain conditions (e.g. only for logged in users) or
328
		 * modify what is returned to the caller.
329
		 *
330
		 * This option allows you to wrap local decorators
331
		 * ("\Aimeos\Admin\JQAdm\Customer\Decorator\*") around the JQAdm client.
332
		 *
333
		 *  admin/jqadm/customer/decorators/local = array( 'decorator2' )
334
		 *
335
		 * This would add the decorator named "decorator2" defined by
336
		 * "\Aimeos\Admin\JQAdm\Customer\Decorator\Decorator2" only to the JQAdm client.
337
		 *
338
		 * @param array List of decorator names
339
		 * @since 2017.07
340
		 * @category Developer
341
		 * @see admin/jqadm/common/decorators/default
342
		 * @see admin/jqadm/customer/decorators/excludes
343
		 * @see admin/jqadm/customer/decorators/global
344
		 */
345
		return $this->createSubClient( 'customer/' . $type, $name );
346
	}
347
348
349
	/**
350
	 * Returns the domain names whose items should be fetched too
351
	 *
352
	 * @return string[] List of domain names
353
	 */
354
	protected function getDomains() : array
355
	{
356
		/** admin/jqadm/customer/domains
357
		 * List of domain items that should be fetched along with the customer
358
		 *
359
		 * If you need to display additional content, you can configure your own
360
		 * list of domains (attribute, media, price, customer, text, etc. are
361
		 * domains) whose items are fetched from the storage.
362
		 *
363
		 * @param array List of domain names
364
		 * @since 2017.07
365
		 * @category Developer
366
		 */
367
		return $this->getContext()->getConfig()->get( 'admin/jqadm/customer/domains', [] );
368
	}
369
370
371
	/**
372
	 * Returns the available group items
373
	 *
374
	 * @param \Aimeos\MShop\Customer\Item\Iface|null $item Customer item that should be updated
375
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface[] Associative list of group IDs as keys and group items as values
376
	 */
377
	protected function getGroupItems( \Aimeos\MShop\Customer\Item\Iface $item = null ) : array
378
	{
379
		$list = [];
380
		$context = $this->getContext();
381
382
		$isSuper = $this->getView()->access( ['super'] );
383
		$isAdmin = $this->getView()->access( ['admin'] );
384
		$isEditor = $this->getView()->access( ['editor'] );
385
386
		$manager = \Aimeos\MShop::create( $context, 'customer/group' );
387
		$search = $manager->filter( true )->slice( 0, 10000 );
388
		$search->setSortations( [$search->sort( '+', 'customer.group.label' )] );
389
390
		foreach( $manager->search( $search ) as $groupId => $groupItem )
391
		{
392
			if( !$isSuper && $groupItem->getCode() === 'super' ) {
393
				continue;
394
			}
395
396
			if( !$isSuper && !$isAdmin && $groupItem->getCode() === 'admin' ) {
397
				continue;
398
			}
399
400
			if( !$isSuper && !$isAdmin && $groupItem->getCode() === 'editor'
401
				&& ( !$isEditor || $item === null || (string) $context->getUserId() !== (string) $item->getId() )
402
			) {
403
				continue;
404
			}
405
406
			$list[$groupId] = $groupItem;
407
		}
408
409
		return $list;
410
	}
411
412
413
	/**
414
	 * Returns the list of sub-client names configured for the client.
415
	 *
416
	 * @return array List of JQAdm client names
417
	 */
418
	protected function getSubClientNames() : array
419
	{
420
		/** admin/jqadm/customer/subparts
421
		 * List of JQAdm sub-clients rendered within the customer section
422
		 *
423
		 * The output of the frontend is composed of the code generated by the JQAdm
424
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
425
		 * that are responsible for rendering certain sub-parts of the output. The
426
		 * sub-clients can contain JQAdm clients themselves and therefore a
427
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
428
		 * the output that is placed inside the container of its parent.
429
		 *
430
		 * At first, always the JQAdm code generated by the parent is printed, then
431
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
432
		 * determines the order of the output of these sub-clients inside the parent
433
		 * container. If the configured list of clients is
434
		 *
435
		 *  array( "subclient1", "subclient2" )
436
		 *
437
		 * you can easily change the order of the output by reordering the subparts:
438
		 *
439
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
440
		 *
441
		 * You can also remove one or more parts if they shouldn't be rendered:
442
		 *
443
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
444
		 *
445
		 * As the clients only generates structural JQAdm, the layout defined via CSS
446
		 * should support adding, removing or reordering content by a fluid like
447
		 * design.
448
		 *
449
		 * @param array List of sub-client names
450
		 * @since 2017.07
451
		 * @category Developer
452
		 */
453
		return $this->getContext()->getConfig()->get( 'admin/jqadm/customer/subparts', [] );
454
	}
455
456
457
458
	/**
459
	 * Creates new and updates existing items using the data array
460
	 *
461
	 * @param array $data Data array
462
	 * @return \Aimeos\MShop\Customer\Item\Iface New customer item object
463
	 */
464
	protected function fromArray( array $data ) : \Aimeos\MShop\Customer\Item\Iface
465
	{
466
		$manager = \Aimeos\MShop::create( $this->getContext(), 'customer' );
467
468
		if( isset( $data['customer.id'] ) && $data['customer.id'] != '' ) {
469
			$item = $manager->get( $data['customer.id'], $this->getDomains() );
470
		} else {
471
			$item = $manager->create();
472
		}
473
474
		$addr = $item->getPaymentAddress();
475
		$label = ( $addr->getFirstname() ? $addr->getFirstname() . ' ' : '' ) . $addr->getLastname();
476
		$label .= ( $addr->getCompany() ? ' (' . $addr->getCompany() . ')' : '' );
477
		$groupIds = $this->getValue( $data, 'customer.groups', [] );
478
479
		$pass = $data['customer.password'] ?? null;
480
		unset( $data['customer.password'] );
481
482
		$item->fromArray( $data, true )
483
			->setGroups( array_intersect( array_keys( $this->getGroupItems( $item ) ), $groupIds ) )
484
			->setCode( $item->getCode() ?: $addr->getEmail() )
485
			->setLabel( $label );
486
487
		if( $this->getView()->access( ['super'] ) ) {
488
			$item->setPassword( $pass );
489
		}
490
491
		return $item;
492
	}
493
494
495
	/**
496
	 * Constructs the data array for the view from the given item
497
	 *
498
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object
499
	 * @return string[] Multi-dimensional associative list of item data
500
	 */
501
	protected function toArray( \Aimeos\MShop\Customer\Item\Iface $item, bool $copy = false ) : array
502
	{
503
		$data = $item->toArray( true );
504
505
		if( !$this->getView()->access( ['super'] ) ) {
506
			unset( $data['customer.password'] );
507
		}
508
509
		if( $copy === true )
510
		{
511
			$data['customer.siteid'] = $this->getContext()->getLocale()->getSiteId();
512
			$data['customer.email'] = '';
513
			$data['customer.id'] = '';
514
		}
515
516
		return $data;
517
	}
518
519
520
	/**
521
	 * Returns the rendered template including the view data
522
	 *
523
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
524
	 * @return string HTML output
525
	 */
526
	protected function render( \Aimeos\MW\View\Iface $view ) : string
527
	{
528
		/** admin/jqadm/customer/template-item
529
		 * Relative path to the HTML body template for the customer item.
530
		 *
531
		 * The template file contains the HTML code and processing instructions
532
		 * to generate the result shown in the body of the frontend. The
533
		 * configuration string is the path to the template file relative
534
		 * to the templates directory (usually in admin/jqadm/templates).
535
		 *
536
		 * You can overwrite the template file configuration in extensions and
537
		 * provide alternative templates. These alternative templates should be
538
		 * named like the default one but with the string "default" replaced by
539
		 * an unique name. You may use the name of your project for this. If
540
		 * you've implemented an alternative client class as well, "default"
541
		 * should be replaced by the name of the new class.
542
		 *
543
		 * @param string Relative path to the template creating the HTML code
544
		 * @since 2016.04
545
		 * @category Developer
546
		 */
547
		$tplconf = 'admin/jqadm/customer/template-item';
548
		$default = 'customer/item-standard';
549
550
		return $view->render( $view->config( $tplconf, $default ) );
551
	}
552
}
553