Passed
Push — master ( 8c5804...4577b9 )
by Aimeos
03:39
created

Standard::fromArray()   B

Complexity

Conditions 11
Paths 13

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 24
nc 13
nop 2
dl 0
loc 44
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

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-2023
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Service\Media;
12
13
sprintf( 'media' ); // for translation
14
15
16
/**
17
 * Default implementation of service media 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
	/** admin/jqadm/service/media/name
27
	 * Name of the media subpart used by the JQAdm service implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Service\Media\Myname".
30
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
31
	 *
32
	 * @param string Last part of the JQAdm class name
33
	 * @since 2017.07
34
	 */
35
36
37
	/**
38
	 * Adds the required data used in the service template
39
	 *
40
	 * @param \Aimeos\Base\View\Iface $view View object
41
	 * @return \Aimeos\Base\View\Iface View object with assigned parameters
42
	 */
43
	public function data( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
44
	{
45
		$context = $this->context();
46
47
		$typeManager = \Aimeos\MShop::create( $context, 'media/type' );
48
		$listTypeManager = \Aimeos\MShop::create( $context, 'service/lists/type' );
49
50
		$search = $typeManager->filter( true )->slice( 0, 10000 );
51
		$search->add( 'media.type.domain', '==', 'service' )->order( 'media.type.code' );
52
53
		$listSearch = $listTypeManager->filter( true )->slice( 0, 10000 );
54
		$listSearch->add( 'service.lists.type.domain', '==', 'media' )->order( 'service.lists.type.code' );
55
56
		$view->mediaListTypes = $listTypeManager->search( $listSearch );
57
		$view->mediaTypes = $typeManager->search( $search );
58
59
		return $view;
60
	}
61
62
63
	/**
64
	 * Copies a resource
65
	 *
66
	 * @return string|null HTML output
67
	 */
68
	public function copy() : ?string
69
	{
70
		$view = $this->object()->data( $this->view() );
71
		$view->mediaData = $this->toArray( $view->item, true );
72
		$view->mediaBody = parent::copy();
73
74
		return $this->render( $view );
75
	}
76
77
78
	/**
79
	 * Creates a new resource
80
	 *
81
	 * @return string|null HTML output
82
	 */
83
	public function create() : ?string
84
	{
85
		$view = $this->object()->data( $this->view() );
86
		$siteid = $this->context()->locale()->getSiteId();
87
88
		$itemData = $this->toArray( $view->item );
89
		$data = array_replace_recursive( $itemData, $view->param( 'media', [] ) );
90
91
		foreach( $data as $idx => $entry )
92
		{
93
			$data[$idx]['media.siteid'] = $siteid;
94
			$data[$idx]['media.url'] = $entry['media.url'] ?? null;
95
			$data[$idx]['media.preview'] = $entry['media.preview'] ?? null;
96
			$data[$idx]['service.lists.siteid'] = $siteid;
97
		}
98
99
		$view->mediaData = $data;
100
		$view->mediaBody = parent::create();
101
102
		return $this->render( $view );
103
	}
104
105
106
	/**
107
	 * Deletes a resource
108
	 *
109
	 * @return string|null HTML output
110
	 */
111
	public function delete() : ?string
112
	{
113
		parent::delete();
114
115
		$item = $this->view()->item;
116
		$this->deleteMediaItems( $item, $item->getListItems( 'media', null, null, false )->toArray() );
117
118
		return null;
119
	}
120
121
122
	/**
123
	 * Returns a single resource
124
	 *
125
	 * @return string|null HTML output
126
	 */
127
	public function get() : ?string
128
	{
129
		$view = $this->object()->data( $this->view() );
130
		$view->mediaData = $this->toArray( $view->item );
131
		$view->mediaBody = parent::get();
132
133
		return $this->render( $view );
134
	}
135
136
137
	/**
138
	 * Saves the data
139
	 *
140
	 * @return string|null HTML output
141
	 */
142
	public function save() : ?string
143
	{
144
		$view = $this->view();
145
146
		$view->item = $this->fromArray( $view->item, $view->param( 'media', [] ) );
147
		$view->mediaBody = parent::save();
148
149
		return null;
150
	}
151
152
153
	/**
154
	 * Returns the sub-client given by its name.
155
	 *
156
	 * @param string $type Name of the client type
157
	 * @param string|null $name Name of the sub-client (Default if null)
158
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
159
	 */
160
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
161
	{
162
		/** admin/jqadm/service/media/decorators/excludes
163
		 * Excludes decorators added by the "common" option from the service JQAdm client
164
		 *
165
		 * Decorators extend the functionality of a class by adding new aspects
166
		 * (e.g. log what is currently done), executing the methods of the underlying
167
		 * class only in certain conditions (e.g. only for logged in users) or
168
		 * modify what is returned to the caller.
169
		 *
170
		 * This option allows you to remove a decorator added via
171
		 * "admin/jqadm/common/decorators/default" before they are wrapped
172
		 * around the JQAdm client.
173
		 *
174
		 *  admin/jqadm/service/media/decorators/excludes = array( 'decorator1' )
175
		 *
176
		 * This would remove the decorator named "decorator1" from the list of
177
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
178
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
179
		 *
180
		 * @param array List of decorator names
181
		 * @since 2017.07
182
		 * @see admin/jqadm/common/decorators/default
183
		 * @see admin/jqadm/service/media/decorators/global
184
		 * @see admin/jqadm/service/media/decorators/local
185
		 */
186
187
		/** admin/jqadm/service/media/decorators/global
188
		 * Adds a list of globally available decorators only to the service JQAdm client
189
		 *
190
		 * Decorators extend the functionality of a class by adding new aspects
191
		 * (e.g. log what is currently done), executing the methods of the underlying
192
		 * class only in certain conditions (e.g. only for logged in users) or
193
		 * modify what is returned to the caller.
194
		 *
195
		 * This option allows you to wrap global decorators
196
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
197
		 *
198
		 *  admin/jqadm/service/media/decorators/global = array( 'decorator1' )
199
		 *
200
		 * This would add the decorator named "decorator1" defined by
201
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
202
		 *
203
		 * @param array List of decorator names
204
		 * @since 2017.07
205
		 * @see admin/jqadm/common/decorators/default
206
		 * @see admin/jqadm/service/media/decorators/excludes
207
		 * @see admin/jqadm/service/media/decorators/local
208
		 */
209
210
		/** admin/jqadm/service/media/decorators/local
211
		 * Adds a list of local decorators only to the service JQAdm client
212
		 *
213
		 * Decorators extend the functionality of a class by adding new aspects
214
		 * (e.g. log what is currently done), executing the methods of the underlying
215
		 * class only in certain conditions (e.g. only for logged in users) or
216
		 * modify what is returned to the caller.
217
		 *
218
		 * This option allows you to wrap local decorators
219
		 * ("\Aimeos\Admin\JQAdm\Service\Decorator\*") around the JQAdm client.
220
		 *
221
		 *  admin/jqadm/service/media/decorators/local = array( 'decorator2' )
222
		 *
223
		 * This would add the decorator named "decorator2" defined by
224
		 * "\Aimeos\Admin\JQAdm\Service\Decorator\Decorator2" only to the JQAdm client.
225
		 *
226
		 * @param array List of decorator names
227
		 * @since 2017.07
228
		 * @see admin/jqadm/common/decorators/default
229
		 * @see admin/jqadm/service/media/decorators/excludes
230
		 * @see admin/jqadm/service/media/decorators/global
231
		 */
232
		return $this->createSubClient( 'service/media/' . $type, $name );
233
	}
234
235
236
	/**
237
	 * Removes the media reference and the media item if not shared
238
	 *
239
	 * @param \Aimeos\MShop\Service\Item\Iface $item Service item including media reference
240
	 * @param array $listItems Media list items to be removed
241
	 * @return \Aimeos\MShop\Service\Item\Iface Modified service item
242
	 */
243
	protected function deleteMediaItems( \Aimeos\MShop\Service\Item\Iface $item, array $listItems )
244
	{
245
		$context = $this->context();
246
		$mediaManager = \Aimeos\MShop::create( $context, 'media' );
247
		$manager = \Aimeos\MShop::create( $context, 'service' );
248
		$search = $manager->filter();
249
250
		foreach( $listItems as $listItem )
251
		{
252
			$func = $search->make( 'service:has', ['media', $listItem->getType(), $listItem->getRefId()] );
253
			$search->setConditions( $search->compare( '!=', $func, null ) );
254
			$items = $manager->search( $search );
255
			$refItem = null;
256
257
			if( count( $items ) === 1 && ( $refItem = $listItem->getRefItem() ) !== null ) {
258
				$mediaManager->delete( $refItem );
259
			}
260
261
			$item->deleteListItem( 'media', $listItem, $refItem );
262
		}
263
264
		return $item;
265
	}
266
267
268
	/**
269
	 * Returns the list of sub-client names configured for the client.
270
	 *
271
	 * @return array List of JQAdm client names
272
	 */
273
	protected function getSubClientNames() : array
274
	{
275
		/** admin/jqadm/service/media/subparts
276
		 * List of JQAdm sub-clients rendered within the service media section
277
		 *
278
		 * The output of the frontend is composed of the code generated by the JQAdm
279
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
280
		 * that are responsible for rendering certain sub-parts of the output. The
281
		 * sub-clients can contain JQAdm clients themselves and therefore a
282
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
283
		 * the output that is placed inside the container of its parent.
284
		 *
285
		 * At first, always the JQAdm code generated by the parent is printed, then
286
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
287
		 * determines the order of the output of these sub-clients inside the parent
288
		 * container. If the configured list of clients is
289
		 *
290
		 *  array( "subclient1", "subclient2" )
291
		 *
292
		 * you can easily change the order of the output by reordering the subparts:
293
		 *
294
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
295
		 *
296
		 * You can also remove one or more parts if they shouldn't be rendered:
297
		 *
298
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
299
		 *
300
		 * As the clients only generates structural JQAdm, the layout defined via CSS
301
		 * should support adding, removing or reordering content by a fluid like
302
		 * design.
303
		 *
304
		 * @param array List of sub-client names
305
		 * @since 2017.07
306
		 */
307
		return $this->context()->config()->get( 'admin/jqadm/service/media/subparts', [] );
308
	}
309
310
311
	/**
312
	 * Creates new and updates existing items using the data array
313
	 *
314
	 * @param \Aimeos\MShop\Service\Item\Iface $item Service item object without referenced domain items
315
	 * @param array $data Data array
316
	 * @return \Aimeos\MShop\Service\Item\Iface Modified service item
317
	 */
318
	protected function fromArray( \Aimeos\MShop\Service\Item\Iface $item, array $data ) : \Aimeos\MShop\Service\Item\Iface
319
	{
320
		$context = $this->context();
321
322
		$manager = \Aimeos\MShop::create( $context, 'service' );
323
		$mediaManager = \Aimeos\MShop::create( $context, 'media' );
324
325
		$listItems = $item->getListItems( 'media', null, null, false );
326
		$files = (array) $this->view()->request()->getUploadedFiles();
327
328
		foreach( $data as $idx => $entry )
329
		{
330
			$id = $this->val( $entry, 'media.id', '' );
331
			$type = $this->val( $entry, 'service.lists.type', 'default' );
332
333
			$listItem = $item->getListItem( 'media', $type, $id, false ) ?: $manager->createListItem();
0 ignored issues
show
Bug introduced by
The method createListItem() does not exist on Aimeos\MShop\Common\Manager\Iface. Did you maybe mean create()? ( Ignorable by Annotation )

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

333
			$listItem = $item->getListItem( 'media', $type, $id, false ) ?: $manager->/** @scrutinizer ignore-call */ createListItem();

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...
334
			$refItem = $listItem->getRefItem() ?: $mediaManager->create();
335
336
			$refItem->fromArray( $entry, true )->setDomain( 'service' );
337
			$file = $this->val( $files, 'media/' . $idx . '/file' );
338
			$preview = $this->val( $files, 'media/' . $idx . '/preview' );
0 ignored issues
show
Unused Code introduced by
The assignment to $preview is dead and can be removed.
Loading history...
339
340
			if( $refItem->getId() === null && $refItem->getUrl() !== '' ) {
341
				$refItem = $mediaManager->copy( $refItem );
342
			}
343
344
			if( $file && $file->getError() !== UPLOAD_ERR_NO_FILE ) {
345
				$refItem = $mediaManager->upload( $refItem, $file );
346
			}
347
348
			$listItem->fromArray( $entry, true )->setPosition( $idx )->setConfig( [] );
349
350
			foreach( (array) $this->val( $entry, 'config', [] ) as $cfg )
351
			{
352
				if( ( $key = trim( $cfg['key'] ?? '' ) ) !== '' && ( $val = trim( $cfg['val'] ?? '' ) ) !== '' ) {
353
					$listItem->setConfigValue( $key, json_decode( $val, true ) ?? $val );
354
				}
355
			}
356
357
			$item->addListItem( 'media', $listItem, $refItem );
358
			unset( $listItems[$listItem->getId()] );
359
		}
360
361
		return $this->deleteMediaItems( $item, $listItems->toArray() );
362
	}
363
364
365
	/**
366
	 * Constructs the data array for the view from the given item
367
	 *
368
	 * @param \Aimeos\MShop\Service\Item\Iface $item Service item object including referenced domain items
369
	 * @param bool $copy True if items should be copied, false if not
370
	 * @return string[] Multi-dimensional associative list of item data
371
	 */
372
	protected function toArray( \Aimeos\MShop\Service\Item\Iface $item, bool $copy = false ) : array
373
	{
374
		$data = [];
375
		$siteId = $this->context()->locale()->getSiteId();
376
377
		foreach( $item->getListItems( 'media', null, null, false ) as $listItem )
378
		{
379
			if( ( $refItem = $listItem->getRefItem() ) === null ) {
380
				continue;
381
			}
382
383
			$list = $listItem->toArray( true ) + $refItem->toArray( true );
384
385
			if( $copy === true )
386
			{
387
				$list['service.lists.siteid'] = $siteId;
388
				$list['service.lists.id'] = '';
389
				$list['media.siteid'] = $siteId;
390
				$list['media.id'] = null;
391
			}
392
393
			$list['media.previews'] = $this->view()->imageset( $refItem->getPreviews(), $refItem->getFileSystem() );
394
			$list['media.preview'] = $this->view()->content( $refItem->getPreview(), $refItem->getFileSystem() );
395
396
			$list['service.lists.datestart'] = str_replace( ' ', 'T', $list['service.lists.datestart'] ?? '' );
397
			$list['service.lists.dateend'] = str_replace( ' ', 'T', $list['service.lists.dateend'] ?? '' );
398
			$list['config'] = [];
399
400
			foreach( $listItem->getConfig() as $key => $value ) {
401
				$list['config'][] = ['key' => $key, 'val' => $value];
402
			}
403
404
			$data[] = $list;
405
		}
406
407
		return $data;
408
	}
409
410
411
	/**
412
	 * Returns the rendered template including the view data
413
	 *
414
	 * @param \Aimeos\Base\View\Iface $view View object with data assigned
415
	 * @return string HTML output
416
	 */
417
	protected function render( \Aimeos\Base\View\Iface $view ) : string
418
	{
419
		/** admin/jqadm/service/media/template-item
420
		 * Relative path to the HTML body template of the media subpart for services.
421
		 *
422
		 * The template file contains the HTML code and processing instructions
423
		 * to generate the result shown in the body of the frontend. The
424
		 * configuration string is the path to the template file relative
425
		 * to the templates directory (usually in templates/admin/jqadm).
426
		 *
427
		 * You can overwrite the template file configuration in extensions and
428
		 * provide alternative templates. These alternative templates should be
429
		 * named like the default one but with the string "default" replaced by
430
		 * an unique name. You may use the name of your project for this. If
431
		 * you've implemented an alternative client class as well, "default"
432
		 * should be replaced by the name of the new class.
433
		 *
434
		 * @param string Relative path to the template creating the HTML code
435
		 * @since 2017.07
436
		 */
437
		$tplconf = 'admin/jqadm/service/media/template-item';
438
		$default = 'service/item-media';
439
440
		return $view->render( $view->config( $tplconf, $default ) );
441
	}
442
}
443