Passed
Push — master ( 154e9f...c95a25 )
by Aimeos
03:44
created

Standard   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 549
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 168
dl 0
loc 549
rs 9.2
c 4
b 0
f 0
wmc 40

12 Methods

Rating   Name   Duplication   Size   Complexity  
A copy() 0 38 5
A create() 0 39 5
A getSubClientNames() 0 36 1
A getSubClient() 0 76 1
A search() 0 58 4
A get() 0 38 5
A getDomains() 0 16 1
A render() 0 25 1
B save() 0 44 6
B delete() 0 51 6
A toArray() 0 12 2
A fromArray() 0 13 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-2018
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Supplier;
12
13
sprintf( 'supplier' ); // for translation
14
15
16
/**
17
 * Default implementation of supplier 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 HTML output
30
	 */
31
	public function copy()
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, 'supplier' );
43
			$view->item = $manager->getItem( $id, $this->getDomains() );
44
45
			$view->itemData = $this->toArray( $view->item, true );
46
			$view->itemSubparts = $this->getSubClientNames();
47
			$view->itemBody = '';
48
49
			foreach( $this->getSubClients() as $idx => $client )
50
			{
51
				$view->tabindex = ++$idx + 1;
52
				$view->itemBody .= $client->copy();
53
			}
54
		}
55
		catch( \Aimeos\MShop\Exception $e )
56
		{
57
			$error = array( 'supplier-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
58
			$view->errors = $view->get( 'errors', [] ) + $error;
59
			$this->logException( $e );
60
		}
61
		catch( \Exception $e )
62
		{
63
			$error = array( 'supplier-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
64
			$view->errors = $view->get( 'errors', [] ) + $error;
65
			$this->logException( $e );
66
		}
67
68
		return $this->render( $view );
69
	}
70
71
72
	/**
73
	 * Creates a new resource
74
	 *
75
	 * @return string HTML output
76
	 */
77
	public function create()
78
	{
79
		$view = $this->getView();
80
		$context = $this->getContext();
81
82
		try
83
		{
84
			$data = $view->param( 'item', [] );
85
86
			if( !isset( $view->item ) ) {
87
				$view->item = \Aimeos\MShop::create( $context, 'supplier' )->createItem();
88
			}
89
90
			$data['supplier.siteid'] = $view->item->getSiteId();
91
92
			$view->itemSubparts = $this->getSubClientNames();
93
			$view->itemData = $data;
94
			$view->itemBody = '';
95
96
			foreach( $this->getSubClients() as $idx => $client )
97
			{
98
				$view->tabindex = ++$idx + 1;
99
				$view->itemBody .= $client->create();
100
			}
101
		}
102
		catch( \Aimeos\MShop\Exception $e )
103
		{
104
			$error = array( 'supplier-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
105
			$view->errors = $view->get( 'errors', [] ) + $error;
106
			$this->logException( $e );
107
		}
108
		catch( \Exception $e )
109
		{
110
			$error = array( 'supplier-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
111
			$view->errors = $view->get( 'errors', [] ) + $error;
112
			$this->logException( $e );
113
		}
114
115
		return $this->render( $view );
116
	}
117
118
119
	/**
120
	 * Deletes a resource
121
	 *
122
	 * @return string|null HTML output
123
	 */
124
	public function delete()
125
	{
126
		$view = $this->getView();
127
		$context = $this->getContext();
128
129
		$manager = \Aimeos\MShop::create( $context, 'supplier' );
130
		$manager->begin();
131
132
		try
133
		{
134
			if( ( $ids = $view->param( 'id' ) ) === null ) {
135
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
136
			}
137
138
			$search = $manager->createSearch()->setSlice( 0, count( (array) $ids ) );
139
			$search->setConditions( $search->compare( '==', 'supplier.id', $ids ) );
140
			$items = $manager->searchItems( $search, $this->getDomains() );
141
142
			foreach( $items as $item )
143
			{
144
				$view->item = $item;
145
146
				foreach( $this->getSubClients() as $client ) {
147
					$client->delete();
148
				}
149
150
				$manager->saveItem( $view->item );
151
			}
152
153
			$manager->deleteItems( array_keys( $items ) );
154
			$manager->commit();
155
156
			$this->nextAction( $view, 'search', 'supplier', null, 'delete' );
157
			return;
158
		}
159
		catch( \Aimeos\MShop\Exception $e )
160
		{
161
			$error = array( 'supplier-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
162
			$view->errors = $view->get( 'errors', [] ) + $error;
163
			$this->logException( $e );
164
		}
165
		catch( \Exception $e )
166
		{
167
			$error = array( 'supplier-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
168
			$view->errors = $view->get( 'errors', [] ) + $error;
169
			$this->logException( $e );
170
		}
171
172
		$manager->rollback();
173
174
		return $this->search();
175
	}
176
177
178
	/**
179
	 * Returns a single resource
180
	 *
181
	 * @return string HTML output
182
	 */
183
	public function get()
184
	{
185
		$view = $this->getView();
186
		$context = $this->getContext();
187
188
		try
189
		{
190
			if( ( $id = $view->param( 'id' ) ) === null ) {
191
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
192
			}
193
194
			$manager = \Aimeos\MShop::create( $context, 'supplier' );
195
196
			$view->item = $manager->getItem( $id, $this->getDomains() );
197
			$view->itemSubparts = $this->getSubClientNames();
198
			$view->itemData = $this->toArray( $view->item );
199
			$view->itemBody = '';
200
201
			foreach( $this->getSubClients() as $idx => $client )
202
			{
203
				$view->tabindex = ++$idx + 1;
204
				$view->itemBody .= $client->get();
205
			}
206
		}
207
		catch( \Aimeos\MShop\Exception $e )
208
		{
209
			$error = array( 'supplier-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
210
			$view->errors = $view->get( 'errors', [] ) + $error;
211
			$this->logException( $e );
212
		}
213
		catch( \Exception $e )
214
		{
215
			$error = array( 'supplier-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
216
			$view->errors = $view->get( 'errors', [] ) + $error;
217
			$this->logException( $e );
218
		}
219
220
		return $this->render( $view );
221
	}
222
223
224
	/**
225
	 * Saves the data
226
	 *
227
	 * @return string HTML output
228
	 */
229
	public function save()
230
	{
231
		$view = $this->getView();
232
		$context = $this->getContext();
233
234
		$manager = \Aimeos\MShop::create( $context, 'supplier' );
235
		$manager->begin();
236
237
		try
238
		{
239
			$item = $this->fromArray( $view->param( 'item', [] ) );
240
			$view->item = $item->getId() ? $item : $manager->saveItem( $item );
241
			$view->itemBody = '';
242
243
			foreach( $this->getSubClients() as $client ) {
244
				$view->itemBody .= $client->save();
245
			}
246
247
			$manager->saveItem( clone $view->item );
248
			$manager->commit();
249
250
			$this->nextAction( $view, $view->param( 'next' ), 'supplier', $view->item->getId(), 'save' );
251
			return;
252
		}
253
		catch( \Aimeos\Admin\JQAdm\Exception $e )
254
		{
255
			// fall through to create
256
		}
257
		catch( \Aimeos\MShop\Exception $e )
258
		{
259
			$error = array( 'supplier-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
260
			$view->errors = $view->get( 'errors', [] ) + $error;
261
			$this->logException( $e );
262
		}
263
		catch( \Exception $e )
264
		{
265
			$error = array( 'supplier-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
266
			$view->errors = $view->get( 'errors', [] ) + $error;
267
			$this->logException( $e );
268
		}
269
270
		$manager->rollback();
271
272
		return $this->create();
273
	}
274
275
276
	/**
277
	 * Returns a list of resource according to the conditions
278
	 *
279
	 * @return string HTML output
280
	 */
281
	public function search()
282
	{
283
		$view = $this->getView();
284
		$context = $this->getContext();
285
286
		try
287
		{
288
			$total = 0;
289
			$params = $this->storeSearchParams( $view->param(), 'supplier' );
290
			$manager = \Aimeos\MShop::create( $context, 'supplier' );
291
			$search = $this->initCriteria( $manager->createSearch(), $params );
292
293
			$view->items = $manager->searchItems( $search, $this->getDomains(), $total );
294
			$view->filterAttributes = $manager->getSearchAttributes( true );
295
			$view->filterOperators = $search->getOperators();
296
			$view->total = $total;
297
			$view->itemBody = '';
298
299
			foreach( $this->getSubClients() as $client ) {
300
				$view->itemBody .= $client->search();
301
			}
302
		}
303
		catch( \Aimeos\MShop\Exception $e )
304
		{
305
			$error = array( 'supplier-item' => $context->getI18n()->dt( 'mshop', $e->getMessage() ) );
306
			$view->errors = $view->get( 'errors', [] ) + $error;
307
			$this->logException( $e );
308
		}
309
		catch( \Exception $e )
310
		{
311
			$error = array( 'supplier-item' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
312
			$view->errors = $view->get( 'errors', [] ) + $error;
313
			$this->logException( $e );
314
		}
315
316
		/** admin/jqadm/supplier/template-list
317
		 * Relative path to the HTML body template for the supplier list.
318
		 *
319
		 * The template file contains the HTML code and processing instructions
320
		 * to generate the result shown in the body of the frontend. The
321
		 * configuration string is the path to the template file relative
322
		 * to the templates directory (usually in admin/jqadm/templates).
323
		 *
324
		 * You can overwrite the template file configuration in extensions and
325
		 * provide alternative templates. These alternative templates should be
326
		 * named like the default one but with the string "default" replaced by
327
		 * an unique name. You may use the name of your project for this. If
328
		 * you've implemented an alternative client class as well, "default"
329
		 * should be replaced by the name of the new class.
330
		 *
331
		 * @param string Relative path to the template creating the HTML code
332
		 * @since 2016.04
333
		 * @category Developer
334
		 */
335
		$tplconf = 'admin/jqadm/supplier/template-list';
336
		$default = 'supplier/list-standard';
337
338
		return $view->render( $view->config( $tplconf, $default ) );
339
	}
340
341
342
	/**
343
	 * Returns the sub-client given by its name.
344
	 *
345
	 * @param string $type Name of the client type
346
	 * @param string|null $name Name of the sub-client (Default if null)
347
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
348
	 */
349
	public function getSubClient( $type, $name = null )
350
	{
351
		/** admin/jqadm/supplier/decorators/excludes
352
		 * Excludes decorators added by the "common" option from the supplier JQAdm client
353
		 *
354
		 * Decorators extend the functionality of a class by adding new aspects
355
		 * (e.g. log what is currently done), executing the methods of the underlying
356
		 * class only in certain conditions (e.g. only for logged in users) or
357
		 * modify what is returned to the caller.
358
		 *
359
		 * This option allows you to remove a decorator added via
360
		 * "client/jqadm/common/decorators/default" before they are wrapped
361
		 * around the JQAdm client.
362
		 *
363
		 *  admin/jqadm/supplier/decorators/excludes = array( 'decorator1' )
364
		 *
365
		 * This would remove the decorator named "decorator1" from the list of
366
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
367
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
368
		 *
369
		 * @param array List of decorator names
370
		 * @since 2017.10
371
		 * @category Developer
372
		 * @see admin/jqadm/common/decorators/default
373
		 * @see admin/jqadm/supplier/decorators/global
374
		 * @see admin/jqadm/supplier/decorators/local
375
		 */
376
377
		/** admin/jqadm/supplier/decorators/global
378
		 * Adds a list of globally available decorators only to the supplier JQAdm client
379
		 *
380
		 * Decorators extend the functionality of a class by adding new aspects
381
		 * (e.g. log what is currently done), executing the methods of the underlying
382
		 * class only in certain conditions (e.g. only for logged in users) or
383
		 * modify what is returned to the caller.
384
		 *
385
		 * This option allows you to wrap global decorators
386
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
387
		 *
388
		 *  admin/jqadm/supplier/decorators/global = array( 'decorator1' )
389
		 *
390
		 * This would add the decorator named "decorator1" defined by
391
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
392
		 *
393
		 * @param array List of decorator names
394
		 * @since 2017.10
395
		 * @category Developer
396
		 * @see admin/jqadm/common/decorators/default
397
		 * @see admin/jqadm/supplier/decorators/excludes
398
		 * @see admin/jqadm/supplier/decorators/local
399
		 */
400
401
		/** admin/jqadm/supplier/decorators/local
402
		 * Adds a list of local decorators only to the supplier JQAdm client
403
		 *
404
		 * Decorators extend the functionality of a class by adding new aspects
405
		 * (e.g. log what is currently done), executing the methods of the underlying
406
		 * class only in certain conditions (e.g. only for logged in users) or
407
		 * modify what is returned to the caller.
408
		 *
409
		 * This option allows you to wrap local decorators
410
		 * ("\Aimeos\Admin\JQAdm\Supplier\Decorator\*") around the JQAdm client.
411
		 *
412
		 *  admin/jqadm/supplier/decorators/local = array( 'decorator2' )
413
		 *
414
		 * This would add the decorator named "decorator2" defined by
415
		 * "\Aimeos\Admin\JQAdm\Supplier\Decorator\Decorator2" only to the JQAdm client.
416
		 *
417
		 * @param array List of decorator names
418
		 * @since 2017.10
419
		 * @category Developer
420
		 * @see admin/jqadm/common/decorators/default
421
		 * @see admin/jqadm/supplier/decorators/excludes
422
		 * @see admin/jqadm/supplier/decorators/global
423
		 */
424
		return $this->createSubClient( 'supplier/' . $type, $name );
425
	}
426
427
428
	/**
429
	 * Returns the domain names whose items should be fetched too
430
	 *
431
	 * @return string[] List of domain names
432
	 */
433
	protected function getDomains()
434
	{
435
		/** admin/jqadm/supplier/domains
436
		 * List of domain items that should be fetched along with the supplier
437
		 *
438
		 * If you need to display additional content, you can configure your own
439
		 * list of domains (attribute, media, price, supplier, text, etc. are
440
		 * domains) whose items are fetched from the storage.
441
		 *
442
		 * @param array List of domain names
443
		 * @since 2017.10
444
		 * @category Developer
445
		 */
446
		$domains = ['media', 'supplier/address', 'text'];
447
448
		return $this->getContext()->getConfig()->get( 'admin/jqadm/supplier/domains', $domains );
449
	}
450
451
452
	/**
453
	 * Returns the list of sub-client names configured for the client.
454
	 *
455
	 * @return array List of JQAdm client names
456
	 */
457
	protected function getSubClientNames()
458
	{
459
		/** admin/jqadm/supplier/standard/subparts
460
		 * List of JQAdm sub-clients rendered within the supplier section
461
		 *
462
		 * The output of the frontend is composed of the code generated by the JQAdm
463
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
464
		 * that are responsible for rendering certain sub-parts of the output. The
465
		 * sub-clients can contain JQAdm clients themselves and therefore a
466
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
467
		 * the output that is placed inside the container of its parent.
468
		 *
469
		 * At first, always the JQAdm code generated by the parent is printed, then
470
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
471
		 * determines the order of the output of these sub-clients inside the parent
472
		 * container. If the configured list of clients is
473
		 *
474
		 *  array( "subclient1", "subclient2" )
475
		 *
476
		 * you can easily change the order of the output by reordering the subparts:
477
		 *
478
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
479
		 *
480
		 * You can also remove one or more parts if they shouldn't be rendered:
481
		 *
482
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
483
		 *
484
		 * As the clients only generates structural JQAdm, the layout defined via CSS
485
		 * should support adding, removing or reordering content by a fluid like
486
		 * design.
487
		 *
488
		 * @param array List of sub-client names
489
		 * @since 2017.10
490
		 * @category Developer
491
		 */
492
		return $this->getContext()->getConfig()->get( 'admin/jqadm/supplier/standard/subparts', [] );
493
	}
494
495
496
497
	/**
498
	 * Creates new and updates existing items using the data array
499
	 *
500
	 * @param array $data Data array
501
	 * @return \Aimeos\MShop\Supplier\Item\Iface New supplier item object
502
	 */
503
	protected function fromArray( array $data )
504
	{
505
		$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier' );
506
507
		if( isset( $data['supplier.id'] ) && $data['supplier.id'] != '' ) {
508
			$item = $manager->getItem( $data['supplier.id'], $this->getDomains() );
509
		} else {
510
			$item = $manager->createItem();
511
		}
512
513
		$item->fromArray( $data, true );
514
515
		return $item;
516
	}
517
518
519
	/**
520
	 * Constructs the data array for the view from the given item
521
	 *
522
	 * @param \Aimeos\MShop\Supplier\Item\Iface $item Supplier item object
523
	 * @return string[] Multi-dimensional associative list of item data
524
	 */
525
	protected function toArray( \Aimeos\MShop\Supplier\Item\Iface $item, $copy = false )
526
	{
527
		$data = $item->toArray( true );
528
529
		if( $copy === true )
530
		{
531
			$data['supplier.siteid'] = $this->getContext()->getLocale()->getSiteId();
532
			$data['supplier.code'] = $data['supplier.code'] . '_copy';
533
			$data['supplier.id'] = '';
534
		}
535
536
		return $data;
537
	}
538
539
540
	/**
541
	 * Returns the rendered template including the view data
542
	 *
543
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
544
	 * @return string HTML output
545
	 */
546
	protected function render( \Aimeos\MW\View\Iface $view )
547
	{
548
		/** admin/jqadm/supplier/template-item
549
		 * Relative path to the HTML body template for the supplier item.
550
		 *
551
		 * The template file contains the HTML code and processing instructions
552
		 * to generate the result shown in the body of the frontend. The
553
		 * configuration string is the path to the template file relative
554
		 * to the templates directory (usually in admin/jqadm/templates).
555
		 *
556
		 * You can overwrite the template file configuration in extensions and
557
		 * provide alternative templates. These alternative templates should be
558
		 * named like the default one but with the string "default" replaced by
559
		 * an unique name. You may use the name of your project for this. If
560
		 * you've implemented an alternative client class as well, "default"
561
		 * should be replaced by the name of the new class.
562
		 *
563
		 * @param string Relative path to the template creating the HTML code
564
		 * @since 2016.04
565
		 * @category Developer
566
		 */
567
		$tplconf = 'admin/jqadm/supplier/template-item';
568
		$default = 'supplier/item-standard';
569
570
		return $view->render( $view->config( $tplconf, $default ) );
571
	}
572
}
573