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

Standard::delete()   B

Complexity

Conditions 6
Paths 46

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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