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

Standard   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 570
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 170
dl 0
loc 570
rs 8.96
c 0
b 0
f 0
wmc 43

15 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 34 4
A create() 0 34 4
A getSubClientNames() 0 36 1
A toArray() 0 21 3
A getDecoratorNames() 0 4 1
A delete() 0 40 5
A save() 0 31 4
B fromArray() 0 31 8
A getTypeItems() 0 8 1
A search() 0 54 3
A getSubClient() 0 76 1
A render() 0 25 1
A getProviderNames() 0 5 1
A copy() 0 34 4
A getConfigAttributes() 0 8 2

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\Plugin;
12
13
sprintf( 'plugin' ); // for translation
14
15
16
/**
17
 * Default implementation of plugin 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, 'plugin' );
43
44
			$view->item = $manager->getItem( $id );
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, 'plugin' )->createItem();
84
			}
85
86
			$data['plugin.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, 'plugin' );
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( '==', 'plugin.id', $ids ) );
131
			$items = $manager->searchItems( $search );
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', 'plugin', 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, 'plugin' );
175
176
			$view->item = $manager->getItem( $id );
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, 'plugin' );
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' ), 'plugin', $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(), 'plugin' );
253
			$manager = \Aimeos\MShop::create( $context, 'plugin' );
254
255
			$search = $manager->createSearch();
256
			$search->setSortations( [$search->sort( '+', 'plugin.type' ), $search->sort( '+', 'plugin.position' )] );
257
			$search = $this->initCriteria( $search, $params );
258
259
			$view->items = $manager->searchItems( $search, [], $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/plugin/template-list
276
		 * Relative path to the HTML body template for the plugin 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/plugin/template-list';
295
		$default = 'plugin/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/plugin/decorators/excludes
311
		 * Excludes decorators added by the "common" option from the plugin 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/plugin/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/plugin/decorators/global
333
		 * @see admin/jqadm/plugin/decorators/local
334
		 */
335
336
		/** admin/jqadm/plugin/decorators/global
337
		 * Adds a list of globally available decorators only to the plugin 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/plugin/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/plugin/decorators/excludes
357
		 * @see admin/jqadm/plugin/decorators/local
358
		 */
359
360
		/** admin/jqadm/plugin/decorators/local
361
		 * Adds a list of local decorators only to the plugin 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\Plugin\Decorator\*") around the JQAdm client.
370
		 *
371
		 *  admin/jqadm/plugin/decorators/local = array( 'decorator2' )
372
		 *
373
		 * This would add the decorator named "decorator2" defined by
374
		 * "\Aimeos\Admin\JQAdm\Plugin\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/plugin/decorators/excludes
381
		 * @see admin/jqadm/plugin/decorators/global
382
		 */
383
		return $this->createSubClient( 'plugin/' . $type, $name );
384
	}
385
386
387
	/**
388
	 * Returns the backend configuration attributes of the provider and decorators
389
	 *
390
	 * @param \Aimeos\MShop\Plugin\Item\Iface $item Plugin item incl. provider/decorator property
391
	 * @return \Aimeos\MW\Common\Critera\Attribute\Iface[] List of configuration attributes
392
	 */
393
	public function getConfigAttributes( \Aimeos\MShop\Plugin\Item\Iface $item ) : array
394
	{
395
		$manager = \Aimeos\MShop::create( $this->getContext(), 'plugin' );
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 names of the available plugin decorators
407
	 *
408
	 * @return string[] List of decorator class names
409
	 */
410
	protected function getDecoratorNames() : array
411
	{
412
		$ds = DIRECTORY_SEPARATOR;
413
		return $this->getClassNames( 'MShop' . $ds . 'Plugin' . $ds . 'Provider' . $ds . 'Decorator' );
414
	}
415
416
417
	/**
418
	 * Returns the names of the available plugin providers
419
	 *
420
	 * @return string[] List of provider class names
421
	 */
422
	protected function getProviderNames() : array
423
	{
424
		$ds = DIRECTORY_SEPARATOR;
425
		return [
426
			'order' => $this->getClassNames( 'MShop' . $ds . 'Plugin' . $ds . 'Provider' . $ds . 'Order' )
427
		];
428
	}
429
430
431
	/**
432
	 * Returns the list of sub-client names configured for the client.
433
	 *
434
	 * @return array List of JQAdm client names
435
	 */
436
	protected function getSubClientNames() : array
437
	{
438
		/** admin/jqadm/plugin/standard/subparts
439
		 * List of JQAdm sub-clients rendered within the plugin section
440
		 *
441
		 * The output of the frontend is composed of the code generated by the JQAdm
442
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
443
		 * that are responsible for rendering certain sub-parts of the output. The
444
		 * sub-clients can contain JQAdm clients themselves and therefore a
445
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
446
		 * the output that is placed inside the container of its parent.
447
		 *
448
		 * At first, always the JQAdm code generated by the parent is printed, then
449
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
450
		 * determines the order of the output of these sub-clients inside the parent
451
		 * container. If the configured list of clients is
452
		 *
453
		 *  array( "subclient1", "subclient2" )
454
		 *
455
		 * you can easily change the order of the output by reordering the subparts:
456
		 *
457
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
458
		 *
459
		 * You can also remove one or more parts if they shouldn't be rendered:
460
		 *
461
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
462
		 *
463
		 * As the clients only generates structural JQAdm, the layout defined via CSS
464
		 * should support adding, removing or reordering content by a fluid like
465
		 * design.
466
		 *
467
		 * @param array List of sub-client names
468
		 * @since 2017.10
469
		 * @category Developer
470
		 */
471
		return $this->getContext()->getConfig()->get( 'admin/jqadm/plugin/standard/subparts', [] );
472
	}
473
474
475
	/**
476
	 * Returns the available plugin type items
477
	 *
478
	 * @return \Aimeos\Map List of IDs as keys and items implementing \Aimeos\MShop\Common\Type\Iface
479
	 */
480
	protected function getTypeItems() : \Aimeos\Map
481
	{
482
		$typeManager = \Aimeos\MShop::create( $this->getContext(), 'plugin/type' );
483
484
		$search = $typeManager->createSearch( true )->setSlice( 0, 10000 );
485
		$search->setSortations( [$search->sort( '+', 'plugin.type.position' )] );
486
487
		return $typeManager->searchItems( $search );
488
	}
489
490
491
	/**
492
	 * Creates new and updates existing items using the data array
493
	 *
494
	 * @param array $data Data array
495
	 * @return \Aimeos\MShop\Plugin\Item\Iface New plugin item object
496
	 */
497
	protected function fromArray( array $data ) : \Aimeos\MShop\Plugin\Item\Iface
498
	{
499
		$conf = [];
500
501
		if( isset( $data['config']['key'] ) )
502
		{
503
			foreach( (array) $data['config']['key'] as $idx => $key )
504
			{
505
				if( trim( $key ) !== '' && isset( $data['config']['val'][$idx] ) )
506
				{
507
					if( ( $val = json_decode( $data['config']['val'][$idx] ) ) === null ) {
508
						$conf[$key] = $data['config']['val'][$idx];
509
					} else {
510
						$conf[$key] = $val;
511
					}
512
				}
513
			}
514
		}
515
516
		$manager = \Aimeos\MShop::create( $this->getContext(), 'plugin' );
517
518
		if( isset( $data['plugin.id'] ) && $data['plugin.id'] != '' ) {
519
			$item = $manager->getItem( $data['plugin.id'] );
520
		} else {
521
			$item = $manager->createItem();
522
		}
523
524
		$item->fromArray( $data, true );
525
		$item->setConfig( $conf );
526
527
		return $item;
528
	}
529
530
531
	/**
532
	 * Constructs the data array for the view from the given item
533
	 *
534
	 * @param \Aimeos\MShop\Plugin\Item\Iface $item Plugin item object
535
	 * @return string[] Multi-dimensional associative list of item data
536
	 */
537
	protected function toArray( \Aimeos\MShop\Plugin\Item\Iface $item, bool $copy = false ) : array
538
	{
539
		$config = $item->getConfig();
540
		$data = $item->toArray( true );
541
		$data['config'] = [];
542
543
		if( $copy === true )
544
		{
545
			$data['plugin.siteid'] = $this->getContext()->getLocale()->getSiteId();
546
			$data['plugin.id'] = '';
547
		}
548
549
		ksort( $config );
550
551
		foreach( $config as $key => $value )
552
		{
553
			$data['config']['key'][] = $key;
554
			$data['config']['val'][] = $value;
555
		}
556
557
		return $data;
558
	}
559
560
561
	/**
562
	 * Returns the rendered template including the view data
563
	 *
564
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
565
	 * @return string|null HTML output
566
	 */
567
	protected function render( \Aimeos\MW\View\Iface $view ) : string
568
	{
569
		/** admin/jqadm/plugin/template-item
570
		 * Relative path to the HTML body template for the plugin item.
571
		 *
572
		 * The template file contains the HTML code and processing instructions
573
		 * to generate the result shown in the body of the frontend. The
574
		 * configuration string is the path to the template file relative
575
		 * to the templates directory (usually in admin/jqadm/templates).
576
		 *
577
		 * You can overwrite the template file configuration in extensions and
578
		 * provide alternative templates. These alternative templates should be
579
		 * named like the default one but with the string "default" replaced by
580
		 * an unique name. You may use the name of your project for this. If
581
		 * you've implemented an alternative client class as well, "default"
582
		 * should be replaced by the name of the new class.
583
		 *
584
		 * @param string Relative path to the template creating the HTML code
585
		 * @since 2016.04
586
		 * @category Developer
587
		 */
588
		$tplconf = 'admin/jqadm/plugin/template-item';
589
		$default = 'plugin/item-standard';
590
591
		return $view->render( $view->config( $tplconf, $default ) );
592
	}
593
}
594