Passed
Push — master ( 596853...253ba3 )
by Aimeos
16:40
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\Attribute;
12
13
sprintf( 'goods' ); // for translation
14
sprintf( 'attribute' ); // for translation
15
16
17
/**
18
 * Default implementation of attribute 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
		$view->itemTypes = $this->getTypeItems();
37
38
		return $view;
39
	}
40
41
42
	/**
43
	 * Copies a resource
44
	 *
45
	 * @return string|null HTML output
46
	 */
47
	public function copy() : ?string
48
	{
49
		$view = $this->getObject()->addData( $this->getView() );
50
51
		try
52
		{
53
			if( ( $id = $view->param( 'id' ) ) === null ) {
54
				throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );
55
			}
56
57
			$manager = \Aimeos\MShop::create( $this->getContext(), 'attribute' );
58
			$view->item = $manager->get( $id, $this->getDomains() );
59
60
			$view->itemData = $this->toArray( $view->item, true );
61
			$view->itemBody = parent::copy();
62
		}
63
		catch( \Exception $e )
64
		{
65
			$this->report( $e, 'copy' );
66
		}
67
68
		return $this->render( $view );
69
	}
70
71
72
	/**
73
	 * Creates a new resource
74
	 *
75
	 * @return string|null HTML output
76
	 */
77
	public function create() : ?string
78
	{
79
		$view = $this->getObject()->addData( $this->getView() );
80
81
		try
82
		{
83
			$data = $view->param( 'item', [] );
84
85
			if( !isset( $view->item ) ) {
86
				$view->item = \Aimeos\MShop::create( $this->getContext(), 'attribute' )->create();
87
			}
88
89
			$data['attribute.siteid'] = $view->item->getSiteId();
90
91
			$view->itemData = array_replace_recursive( $this->toArray( $view->item ), $data );
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(), 'attribute' );
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( '==', 'attribute.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( 'attribute', '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(), 'attribute' );
162
163
			$view->item = $manager->get( $id, $this->getDomains() );
164
			$view->itemData = $this->toArray( $view->item );
165
			$view->itemBody = parent::get();
166
		}
167
		catch( \Exception $e )
168
		{
169
			$this->report( $e, 'get' );
170
		}
171
172
		return $this->render( $view );
173
	}
174
175
176
	/**
177
	 * Saves the data
178
	 *
179
	 * @return string|null HTML output
180
	 */
181
	public function save() : ?string
182
	{
183
		$view = $this->getView();
184
185
		$manager = \Aimeos\MShop::create( $this->getContext(), 'attribute' );
186
		$manager->begin();
187
188
		try
189
		{
190
			$item = $this->fromArray( $view->param( 'item', [] ) );
191
			$view->item = $item->getId() ? $item : $manager->save( $item );
192
			$view->itemBody = parent::save();
193
194
			$manager->save( clone $view->item );
195
			$manager->commit();
196
197
			return $this->redirect( 'attribute', $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

197
			return $this->redirect( 'attribute', $view->param( 'next' ), /** @scrutinizer ignore-type */ $view->item->getId(), 'save' );
Loading history...
198
		}
199
		catch( \Exception $e )
200
		{
201
			$manager->rollback();
202
			$this->report( $e, 'save' );
203
		}
204
205
		return $this->create();
206
	}
207
208
209
	/**
210
	 * Returns a list of resource according to the conditions
211
	 *
212
	 * @return string|null HTML output
213
	 */
214
	public function search() : ?string
215
	{
216
		$view = $this->getView();
217
218
		try
219
		{
220
			$total = 0;
221
			$params = $this->storeFilter( $view->param(), 'attribute' );
222
			$manager = \Aimeos\MShop::create( $this->getContext(), 'attribute' );
223
			$search = $this->initCriteria( $manager->filter(), $params );
224
225
			$view->items = $manager->search( $search, $this->getDomains(), $total );
226
			$view->filterAttributes = $manager->getSearchAttributes( true );
227
			$view->filterOperators = $search->getOperators();
228
			$view->itemTypes = $this->getTypeItems();
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/attribute/template-list
238
		 * Relative path to the HTML body template for the attribute 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 2017.07
254
		 * @category Developer
255
		 */
256
		$tplconf = 'admin/jqadm/attribute/template-list';
257
		$default = 'attribute/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/attribute/decorators/excludes
273
		 * Excludes decorators added by the "common" option from the attribute 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/attribute/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/attribute/decorators/global
295
		 * @see admin/jqadm/attribute/decorators/local
296
		 */
297
298
		/** admin/jqadm/attribute/decorators/global
299
		 * Adds a list of globally available decorators only to the attribute 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/attribute/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/attribute/decorators/excludes
319
		 * @see admin/jqadm/attribute/decorators/local
320
		 */
321
322
		/** admin/jqadm/attribute/decorators/local
323
		 * Adds a list of local decorators only to the attribute 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\Attribute\Decorator\*") around the JQAdm client.
332
		 *
333
		 *  admin/jqadm/attribute/decorators/local = array( 'decorator2' )
334
		 *
335
		 * This would add the decorator named "decorator2" defined by
336
		 * "\Aimeos\Admin\JQAdm\Attribute\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/attribute/decorators/excludes
343
		 * @see admin/jqadm/attribute/decorators/global
344
		 */
345
		return $this->createSubClient( 'attribute/' . $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/attribute/domains
357
		 * List of domain items that should be fetched along with the attribute
358
		 *
359
		 * If you need to display additional content, you can configure your own
360
		 * list of domains (attribute, media, price, attribute, 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/attribute/domains', [] );
368
	}
369
370
371
	/**
372
	 * Returns the list of sub-client names configured for the client.
373
	 *
374
	 * @return array List of JQAdm client names
375
	 */
376
	protected function getSubClientNames() : array
377
	{
378
		/** admin/jqadm/attribute/subparts
379
		 * List of JQAdm sub-clients rendered within the attribute section
380
		 *
381
		 * The output of the frontend is composed of the code generated by the JQAdm
382
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
383
		 * that are responsible for rendering certain sub-parts of the output. The
384
		 * sub-clients can contain JQAdm clients themselves and therefore a
385
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
386
		 * the output that is placed inside the container of its parent.
387
		 *
388
		 * At first, always the JQAdm code generated by the parent is printed, then
389
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
390
		 * determines the order of the output of these sub-clients inside the parent
391
		 * container. If the configured list of clients is
392
		 *
393
		 *  array( "subclient1", "subclient2" )
394
		 *
395
		 * you can easily change the order of the output by reordering the subparts:
396
		 *
397
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
398
		 *
399
		 * You can also remove one or more parts if they shouldn't be rendered:
400
		 *
401
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
402
		 *
403
		 * As the clients only generates structural JQAdm, the layout defined via CSS
404
		 * should support adding, removing or reordering content by a fluid like
405
		 * design.
406
		 *
407
		 * @param array List of sub-client names
408
		 * @since 2017.07
409
		 * @category Developer
410
		 */
411
		return $this->getContext()->getConfig()->get( 'admin/jqadm/attribute/subparts', [] );
412
	}
413
414
415
	/**
416
	 * Returns the available attribute type items
417
	 *
418
	 * @return \Aimeos\Map List of item implementing \Aimeos\MShop\Common\Type\Iface
419
	 */
420
	protected function getTypeItems() : \Aimeos\Map
421
	{
422
		$typeManager = \Aimeos\MShop::create( $this->getContext(), 'attribute/type' );
423
424
		$search = $typeManager->filter( true )->slice( 0, 10000 );
425
		$search->setSortations( [$search->sort( '+', 'attribute.type.label' )] );
426
427
		return $typeManager->search( $search );
428
	}
429
430
431
	/**
432
	 * Creates new and updates existing items using the data array
433
	 *
434
	 * @param array $data Data array
435
	 * @return \Aimeos\MShop\Attribute\Item\Iface New attribute item object
436
	 */
437
	protected function fromArray( array $data ) : \Aimeos\MShop\Attribute\Item\Iface
438
	{
439
		$manager = \Aimeos\MShop::create( $this->getContext(), 'attribute' );
440
441
		if( isset( $data['attribute.id'] ) && $data['attribute.id'] != '' ) {
442
			$item = $manager->get( $data['attribute.id'], $this->getDomains() );
443
		} else {
444
			$item = $manager->create();
445
		}
446
447
		$item->fromArray( $data, true );
448
449
		return $item;
450
	}
451
452
453
	/**
454
	 * Constructs the data array for the view from the given item
455
	 *
456
	 * @param \Aimeos\MShop\Attribute\Item\Iface $item Attribute item object
457
	 * @return string[] Multi-dimensional associative list of item data
458
	 */
459
	protected function toArray( \Aimeos\MShop\Attribute\Item\Iface $item, bool $copy = false ) : array
460
	{
461
		$data = $item->toArray( true );
462
463
		if( $copy === true )
464
		{
465
			$data['attribute.siteid'] = $this->getContext()->getLocale()->getSiteId();
466
			$data['attribute.code'] = $data['attribute.code'] . '_' . substr( md5( microtime( true ) ), -5 );
467
			$data['attribute.id'] = '';
468
		}
469
470
		return $data;
471
	}
472
473
474
	/**
475
	 * Returns the rendered template including the view data
476
	 *
477
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
478
	 * @return string HTML output
479
	 */
480
	protected function render( \Aimeos\MW\View\Iface $view ) : string
481
	{
482
		/** admin/jqadm/attribute/template-item
483
		 * Relative path to the HTML body template for the attribute item.
484
		 *
485
		 * The template file contains the HTML code and processing instructions
486
		 * to generate the result shown in the body of the frontend. The
487
		 * configuration string is the path to the template file relative
488
		 * to the templates directory (usually in admin/jqadm/templates).
489
		 *
490
		 * You can overwrite the template file configuration in extensions and
491
		 * provide alternative templates. These alternative templates should be
492
		 * named like the default one but with the string "default" replaced by
493
		 * an unique name. You may use the name of your project for this. If
494
		 * you've implemented an alternative client class as well, "default"
495
		 * should be replaced by the name of the new class.
496
		 *
497
		 * @param string Relative path to the template creating the HTML code
498
		 * @since 2017.07
499
		 * @category Developer
500
		 */
501
		$tplconf = 'admin/jqadm/attribute/template-item';
502
		$default = 'attribute/item-standard';
503
504
		return $view->render( $view->config( $tplconf, $default ) );
505
	}
506
}
507