Completed
Push — master ( a13539...bd1b6d )
by Aimeos
04:27
created

Standard   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 594
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 173
dl 0
loc 594
rs 8.8798
c 0
b 0
f 0
wmc 44

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypeItems() 0 8 1
A getDecoratorNames() 0 4 1
A delete() 0 40 5
A search() 0 54 3
A getSubClientNames() 0 36 1
A getDomains() 0 14 1
A getConfigAttributes() 0 8 2
A save() 0 31 4
A copy() 0 34 4
A getProviderNames() 0 6 1
A get() 0 34 4
A getSubClient() 0 76 1
A create() 0 34 4
B fromArray() 0 31 8
A toArray() 0 22 3
A render() 0 25 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-2020
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Service;
12
13
sprintf( 'service' ); // for translation
14
15
16
/**
17
 * Default implementation of service 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
	/**
27
	 * Copies a resource
28
	 *
29
	 * @return string|null HTML output
30
	 */
31
	public function copy() : ?string
32
	{
33
		$view = $this->getView();
34
		$context = $this->getContext();
35
36
		try
37
		{
38
			if( ( $id = $view->param( 'id' ) ) === null ) {
39
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
40
			}
41
42
			$manager = \Aimeos\MShop::create( $context, 'service' );
43
44
			$view->item = $manager->getItem( $id, $this->getDomains() );
45
			$view->itemData = $this->toArray( $view->item, true );
46
			$view->itemSubparts = $this->getSubClientNames();
47
			$view->itemProviders = $this->getProviderNames();
48
			$view->itemDecorators = $this->getDecoratorNames();
49
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
50
			$view->itemTypes = $this->getTypeItems();
51
			$view->itemBody = '';
52
53
			foreach( $this->getSubClients() as $idx => $client )
54
			{
55
				$view->tabindex = ++$idx + 1;
56
				$view->itemBody .= $client->copy();
57
			}
58
		}
59
		catch( \Exception $e )
60
		{
61
			$this->report( $e, 'copy' );
62
		}
63
64
		return $this->render( $view );
65
	}
66
67
68
	/**
69
	 * Creates a new resource
70
	 *
71
	 * @return string|null HTML output
72
	 */
73
	public function create() : ?string
74
	{
75
		$view = $this->getView();
76
		$context = $this->getContext();
77
78
		try
79
		{
80
			$data = $view->param( 'item', [] );
81
82
			if( !isset( $view->item ) ) {
83
				$view->item = \Aimeos\MShop::create( $context, 'service' )->createItem();
84
			}
85
86
			$data['service.siteid'] = $view->item->getSiteId();
87
88
			$view->itemSubparts = $this->getSubClientNames();
89
			$view->itemDecorators = $this->getDecoratorNames();
90
			$view->itemProviders = $this->getProviderNames();
91
			$view->itemTypes = $this->getTypeItems();
92
			$view->itemData = $data;
93
			$view->itemBody = '';
94
95
			foreach( $this->getSubClients() as $idx => $client )
96
			{
97
				$view->tabindex = ++$idx + 1;
98
				$view->itemBody .= $client->create();
99
			}
100
		}
101
		catch( \Exception $e )
102
		{
103
			$this->report( $e, 'create' );
104
		}
105
106
		return $this->render( $view );
107
	}
108
109
110
	/**
111
	 * Deletes a resource
112
	 *
113
	 * @return string|null HTML output
114
	 */
115
	public function delete() : ?string
116
	{
117
		$view = $this->getView();
118
		$context = $this->getContext();
119
120
		$manager = \Aimeos\MShop::create( $context, 'service' );
121
		$manager->begin();
122
123
		try
124
		{
125
			if( ( $ids = $view->param( 'id' ) ) === null ) {
126
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
127
			}
128
129
			$search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) );
130
			$search->setConditions( $search->compare( '==', 'service.id', $ids ) );
131
			$items = $manager->searchItems( $search, $this->getDomains() );
132
133
			foreach( $items as $item )
134
			{
135
				$view->item = $item;
136
137
				foreach( $this->getSubClients() as $client ) {
138
					$client->delete();
139
				}
140
			}
141
142
			$manager->deleteItems( $items->toArray() );
143
			$manager->commit();
144
145
			$this->nextAction( $view, 'search', 'service', null, 'delete' );
146
			return null;
147
		}
148
		catch( \Exception $e )
149
		{
150
			$manager->rollback();
151
			$this->report( $e, 'delete' );
152
		}
153
154
		return $this->search();
155
	}
156
157
158
	/**
159
	 * Returns a single resource
160
	 *
161
	 * @return string|null HTML output
162
	 */
163
	public function get() : ?string
164
	{
165
		$view = $this->getView();
166
		$context = $this->getContext();
167
168
		try
169
		{
170
			if( ( $id = $view->param( 'id' ) ) === null ) {
171
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
172
			}
173
174
			$manager = \Aimeos\MShop::create( $context, 'service' );
175
176
			$view->item = $manager->getItem( $id, $this->getDomains() );
177
			$view->itemData = $this->toArray( $view->item );
178
			$view->itemSubparts = $this->getSubClientNames();
179
			$view->itemDecorators = $this->getDecoratorNames();
180
			$view->itemProviders = $this->getProviderNames();
181
			$view->itemAttributes = $this->getConfigAttributes( $view->item );
182
			$view->itemTypes = $this->getTypeItems();
183
			$view->itemBody = '';
184
185
			foreach( $this->getSubClients() as $idx => $client )
186
			{
187
				$view->tabindex = ++$idx + 1;
188
				$view->itemBody .= $client->get();
189
			}
190
		}
191
		catch( \Exception $e )
192
		{
193
			$this->report( $e, 'get' );
194
		}
195
196
		return $this->render( $view );
197
	}
198
199
200
	/**
201
	 * Saves the data
202
	 *
203
	 * @return string|null HTML output
204
	 */
205
	public function save() : ?string
206
	{
207
		$view = $this->getView();
208
		$context = $this->getContext();
209
210
		$manager = \Aimeos\MShop::create( $context, 'service' );
211
		$manager->begin();
212
213
		try
214
		{
215
			$item = $this->fromArray( $view->param( 'item', [] ) );
216
			$view->item = $item->getId() ? $item : $manager->saveItem( $item );
0 ignored issues
show
Bug introduced by
The method saveItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean saveItems()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

216
			$view->item = $item->getId() ? $item : $manager->/** @scrutinizer ignore-call */ saveItem( $item );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
217
			$view->itemBody = '';
218
219
			foreach( $this->getSubClients() as $client ) {
220
				$view->itemBody .= $client->save();
221
			}
222
223
			$manager->saveItem( clone $view->item );
224
			$manager->commit();
225
226
			$this->nextAction( $view, $view->param( 'next' ), 'service', $view->item->getId(), 'save' );
227
			return null;
228
		}
229
		catch( \Exception $e )
230
		{
231
			$manager->rollback();
232
			$this->report( $e, 'save' );
233
		}
234
235
		return $this->create();
236
	}
237
238
239
	/**
240
	 * Returns a list of resource according to the conditions
241
	 *
242
	 * @return string|null HTML output
243
	 */
244
	public function search() : ?string
245
	{
246
		$view = $this->getView();
247
		$context = $this->getContext();
248
249
		try
250
		{
251
			$total = 0;
252
			$params = $this->storeSearchParams( $view->param(), 'service' );
253
			$manager = \Aimeos\MShop::create( $context, 'service' );
254
255
			$search = $manager->createSearch();
256
			$search->setSortations( [$search->sort( '+', 'service.type' ), $search->sort( '+', 'service.position' )] );
257
			$search = $this->initCriteria( $search, $params );
258
259
			$view->items = $manager->searchItems( $search, $this->getDomains(), $total );
260
			$view->filterAttributes = $manager->getSearchAttributes( true );
261
			$view->filterOperators = $search->getOperators();
262
			$view->itemTypes = $this->getTypeItems();
263
			$view->total = $total;
264
			$view->itemBody = '';
265
266
			foreach( $this->getSubClients() as $client ) {
267
				$view->itemBody .= $client->search();
268
			}
269
		}
270
		catch( \Exception $e )
271
		{
272
			$this->report( $e, 'search' );
273
		}
274
275
		/** admin/jqadm/service/template-list
276
		 * Relative path to the HTML body template for the service list.
277
		 *
278
		 * The template file contains the HTML code and processing instructions
279
		 * to generate the result shown in the body of the frontend. The
280
		 * configuration string is the path to the template file relative
281
		 * to the templates directory (usually in admin/jqadm/templates).
282
		 *
283
		 * You can overwrite the template file configuration in extensions and
284
		 * provide alternative templates. These alternative templates should be
285
		 * named like the default one but with the string "default" replaced by
286
		 * an unique name. You may use the name of your project for this. If
287
		 * you've implemented an alternative client class as well, "default"
288
		 * should be replaced by the name of the new class.
289
		 *
290
		 * @param string Relative path to the template creating the HTML code
291
		 * @since 2016.04
292
		 * @category Developer
293
		 */
294
		$tplconf = 'admin/jqadm/service/template-list';
295
		$default = 'service/list-standard';
296
297
		return $view->render( $view->config( $tplconf, $default ) );
298
	}
299
300
301
	/**
302
	 * Returns the sub-client given by its name.
303
	 *
304
	 * @param string $type Name of the client type
305
	 * @param string|null $name Name of the sub-client (Default if null)
306
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
307
	 */
308
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
309
	{
310
		/** admin/jqadm/service/decorators/excludes
311
		 * Excludes decorators added by the "common" option from the service JQAdm client
312
		 *
313
		 * Decorators extend the functionality of a class by adding new aspects
314
		 * (e.g. log what is currently done), executing the methods of the underlying
315
		 * class only in certain conditions (e.g. only for logged in users) or
316
		 * modify what is returned to the caller.
317
		 *
318
		 * This option allows you to remove a decorator added via
319
		 * "client/jqadm/common/decorators/default" before they are wrapped
320
		 * around the JQAdm client.
321
		 *
322
		 *  admin/jqadm/service/decorators/excludes = array( 'decorator1' )
323
		 *
324
		 * This would remove the decorator named "decorator1" from the list of
325
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
326
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
327
		 *
328
		 * @param array List of decorator names
329
		 * @since 2017.10
330
		 * @category Developer
331
		 * @see admin/jqadm/common/decorators/default
332
		 * @see admin/jqadm/service/decorators/global
333
		 * @see admin/jqadm/service/decorators/local
334
		 */
335
336
		/** admin/jqadm/service/decorators/global
337
		 * Adds a list of globally available decorators only to the service JQAdm client
338
		 *
339
		 * Decorators extend the functionality of a class by adding new aspects
340
		 * (e.g. log what is currently done), executing the methods of the underlying
341
		 * class only in certain conditions (e.g. only for logged in users) or
342
		 * modify what is returned to the caller.
343
		 *
344
		 * This option allows you to wrap global decorators
345
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
346
		 *
347
		 *  admin/jqadm/service/decorators/global = array( 'decorator1' )
348
		 *
349
		 * This would add the decorator named "decorator1" defined by
350
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
351
		 *
352
		 * @param array List of decorator names
353
		 * @since 2017.10
354
		 * @category Developer
355
		 * @see admin/jqadm/common/decorators/default
356
		 * @see admin/jqadm/service/decorators/excludes
357
		 * @see admin/jqadm/service/decorators/local
358
		 */
359
360
		/** admin/jqadm/service/decorators/local
361
		 * Adds a list of local decorators only to the service JQAdm client
362
		 *
363
		 * Decorators extend the functionality of a class by adding new aspects
364
		 * (e.g. log what is currently done), executing the methods of the underlying
365
		 * class only in certain conditions (e.g. only for logged in users) or
366
		 * modify what is returned to the caller.
367
		 *
368
		 * This option allows you to wrap local decorators
369
		 * ("\Aimeos\Admin\JQAdm\Service\Decorator\*") around the JQAdm client.
370
		 *
371
		 *  admin/jqadm/service/decorators/local = array( 'decorator2' )
372
		 *
373
		 * This would add the decorator named "decorator2" defined by
374
		 * "\Aimeos\Admin\JQAdm\Service\Decorator\Decorator2" only to the JQAdm client.
375
		 *
376
		 * @param array List of decorator names
377
		 * @since 2017.10
378
		 * @category Developer
379
		 * @see admin/jqadm/common/decorators/default
380
		 * @see admin/jqadm/service/decorators/excludes
381
		 * @see admin/jqadm/service/decorators/global
382
		 */
383
		return $this->createSubClient( 'service/' . $type, $name );
384
	}
385
386
387
	/**
388
	 * Returns the backend configuration attributes of the provider and decorators
389
	 *
390
	 * @param \Aimeos\MShop\Service\Item\Iface $item Service item incl. provider/decorator property
391
	 * @return \Aimeos\MW\Common\Critera\Attribute\Iface[] List of configuration attributes
392
	 */
393
	public function getConfigAttributes( \Aimeos\MShop\Service\Item\Iface $item ) : array
394
	{
395
		$manager = \Aimeos\MShop::create( $this->getContext(), 'service' );
396
397
		try {
398
			return $manager->getProvider( $item, $item->getType() )->getConfigBE();
399
		} catch( \Aimeos\MShop\Exception $e ) {
400
			return [];
401
		}
402
	}
403
404
405
	/**
406
	 * Returns the domain names whose items should be fetched too
407
	 *
408
	 * @return string[] List of domain names
409
	 */
410
	protected function getDomains() : array
411
	{
412
		/** admin/jqadm/service/domains
413
		 * List of domain items that should be fetched along with the service
414
		 *
415
		 * If you need to display additional content, you can configure your own
416
		 * list of domains (attribute, media, price, service, text, etc. are
417
		 * domains) whose items are fetched from the storage.
418
		 *
419
		 * @param array List of domain names
420
		 * @since 2017.10
421
		 * @category Developer
422
		 */
423
		return $this->getContext()->getConfig()->get( 'admin/jqadm/service/domains', [] );
424
	}
425
426
427
	/**
428
	 * Returns the names of the available service decorators
429
	 *
430
	 * @return string[] List of decorator class names
431
	 */
432
	protected function getDecoratorNames() : array
433
	{
434
		$ds = DIRECTORY_SEPARATOR;
435
		return $this->getClassNames( 'MShop' . $ds . 'Service' . $ds . 'Provider' . $ds . 'Decorator' );
436
	}
437
438
439
	/**
440
	 * Returns the names of the available service providers
441
	 *
442
	 * @return string[] List of provider class names
443
	 */
444
	protected function getProviderNames() : array
445
	{
446
		$ds = DIRECTORY_SEPARATOR;
447
		return [
448
			'delivery' => $this->getClassNames( 'MShop' . $ds . 'Service' . $ds . 'Provider' . $ds . 'Delivery' ),
449
			'payment' => $this->getClassNames( 'MShop' . $ds . 'Service' . $ds . 'Provider' . $ds . 'Payment' ),
450
		];
451
	}
452
453
454
	/**
455
	 * Returns the list of sub-client names configured for the client.
456
	 *
457
	 * @return array List of JQAdm client names
458
	 */
459
	protected function getSubClientNames() : array
460
	{
461
		/** admin/jqadm/service/standard/subparts
462
		 * List of JQAdm sub-clients rendered within the service section
463
		 *
464
		 * The output of the frontend is composed of the code generated by the JQAdm
465
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
466
		 * that are responsible for rendering certain sub-parts of the output. The
467
		 * sub-clients can contain JQAdm clients themselves and therefore a
468
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
469
		 * the output that is placed inside the container of its parent.
470
		 *
471
		 * At first, always the JQAdm code generated by the parent is printed, then
472
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
473
		 * determines the order of the output of these sub-clients inside the parent
474
		 * container. If the configured list of clients is
475
		 *
476
		 *  array( "subclient1", "subclient2" )
477
		 *
478
		 * you can easily change the order of the output by reordering the subparts:
479
		 *
480
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
481
		 *
482
		 * You can also remove one or more parts if they shouldn't be rendered:
483
		 *
484
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
485
		 *
486
		 * As the clients only generates structural JQAdm, the layout defined via CSS
487
		 * should support adding, removing or reordering content by a fluid like
488
		 * design.
489
		 *
490
		 * @param array List of sub-client names
491
		 * @since 2017.10
492
		 * @category Developer
493
		 */
494
		return $this->getContext()->getConfig()->get( 'admin/jqadm/service/standard/subparts', [] );
495
	}
496
497
498
	/**
499
	 * Returns the available service type items
500
	 *
501
	 * @return \Aimeos\Map List of item implementing \Aimeos\MShop\Common\Type\Iface
502
	 */
503
	protected function getTypeItems() : \Aimeos\Map
504
	{
505
		$typeManager = \Aimeos\MShop::create( $this->getContext(), 'service/type' );
506
507
		$search = $typeManager->createSearch( true )->setSlice( 0, 10000 );
508
		$search->setSortations( [$search->sort( '+', 'service.type.position' )] );
509
510
		return $typeManager->searchItems( $search );
511
	}
512
513
514
	/**
515
	 * Creates new and updates existing items using the data array
516
	 *
517
	 * @param array $data Data array
518
	 * @return \Aimeos\MShop\Service\Item\Iface New service item object
519
	 */
520
	protected function fromArray( array $data ) : \Aimeos\MShop\Service\Item\Iface
521
	{
522
		$conf = [];
523
524
		if( isset( $data['config']['key'] ) )
525
		{
526
			foreach( (array) $data['config']['key'] as $idx => $key )
527
			{
528
				if( trim( $key ) !== '' && isset( $data['config']['val'][$idx] ) )
529
				{
530
					if( ( $val = json_decode( $data['config']['val'][$idx] ) ) === null ) {
531
						$conf[$key] = $data['config']['val'][$idx];
532
					} else {
533
						$conf[$key] = $val;
534
					}
535
				}
536
			}
537
		}
538
539
		$manager = \Aimeos\MShop::create( $this->getContext(), 'service' );
540
541
		if( isset( $data['service.id'] ) && $data['service.id'] != '' ) {
542
			$item = $manager->getItem( $data['service.id'], $this->getDomains() );
543
		} else {
544
			$item = $manager->createItem();
545
		}
546
547
		$item->fromArray( $data, true );
548
		$item->setConfig( $conf );
549
550
		return $item;
551
	}
552
553
554
	/**
555
	 * Constructs the data array for the view from the given item
556
	 *
557
	 * @param \Aimeos\MShop\Service\Item\Iface $item Service item object
558
	 * @return string[] Multi-dimensional associative list of item data
559
	 */
560
	protected function toArray( \Aimeos\MShop\Service\Item\Iface $item, bool $copy = false ) : array
561
	{
562
		$config = $item->getConfig();
563
		$data = $item->toArray( true );
564
		$data['config'] = [];
565
566
		if( $copy === true )
567
		{
568
			$data['service.siteid'] = $this->getContext()->getLocale()->getSiteId();
569
			$data['service.code'] = $data['service.code'] . '_copy';
570
			$data['service.id'] = '';
571
		}
572
573
		ksort( $config );
574
575
		foreach( $config as $key => $value )
576
		{
577
			$data['config']['key'][] = $key;
578
			$data['config']['val'][] = $value;
579
		}
580
581
		return $data;
582
	}
583
584
585
	/**
586
	 * Returns the rendered template including the view data
587
	 *
588
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
589
	 * @return string|null HTML output
590
	 */
591
	protected function render( \Aimeos\MW\View\Iface $view ) : string
592
	{
593
		/** admin/jqadm/service/template-item
594
		 * Relative path to the HTML body template for the service item.
595
		 *
596
		 * The template file contains the HTML code and processing instructions
597
		 * to generate the result shown in the body of the frontend. The
598
		 * configuration string is the path to the template file relative
599
		 * to the templates directory (usually in admin/jqadm/templates).
600
		 *
601
		 * You can overwrite the template file configuration in extensions and
602
		 * provide alternative templates. These alternative templates should be
603
		 * named like the default one but with the string "default" replaced by
604
		 * an unique name. You may use the name of your project for this. If
605
		 * you've implemented an alternative client class as well, "default"
606
		 * should be replaced by the name of the new class.
607
		 *
608
		 * @param string Relative path to the template creating the HTML code
609
		 * @since 2016.04
610
		 * @category Developer
611
		 */
612
		$tplconf = 'admin/jqadm/service/template-item';
613
		$default = 'service/item-standard';
614
615
		return $view->render( $view->config( $tplconf, $default ) );
616
	}
617
}
618