Passed
Push — master ( 1e8088...bb7713 )
by Aimeos
09:29
created

Standard   F

Complexity

Total Complexity 69

Size/Duplication

Total Lines 440
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 69
eloc 105
c 1
b 0
f 0
dl 0
loc 440
rs 2.88

27 Methods

Rating   Name   Duplication   Size   Complexity  
A getMimeType() 0 3 1
A setFileSystem() 0 3 1
A setDomain() 0 3 1
A getFileSystem() 0 3 1
A setLabel() 0 3 1
A __clone() 0 5 1
A getLabel() 0 3 1
A __construct() 0 12 4
A setType() 0 3 1
A getStatus() 0 3 1
A getDomain() 0 3 1
A setMimeType() 0 7 2
A setStatus() 0 3 1
A setLanguageId() 0 3 1
A getType() 0 3 1
A getLanguageId() 0 3 1
A setUrl() 0 3 1
A setPreview() 0 3 1
C getPreview() 0 37 12
C fromArray() 0 25 12
A getUrl() 0 9 5
A setPreviews() 0 3 1
A toArray() 0 16 1
A getResourceType() 0 3 1
A getName() 0 10 4
A isAvailable() 0 5 5
A getPreviews() 0 15 6

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, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package MShop
8
 * @subpackage Media
9
 */
10
11
12
namespace Aimeos\MShop\Media\Item;
13
14
use \Aimeos\MShop\Common\Item\ListsRef;
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\ListsRef was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use \Aimeos\MShop\Common\Item\PropertyRef;
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\PropertyRef was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
18
/**
19
 * Default implementation of the media item.
20
 *
21
 * @package MShop
22
 * @subpackage Media
23
 */
24
class Standard
25
	extends \Aimeos\MShop\Common\Item\Base
26
	implements \Aimeos\MShop\Media\Item\Iface
27
{
28
	use ListsRef\Traits, PropertyRef\Traits {
0 ignored issues
show
Bug introduced by
The type \Aimeos\MShop\Common\Item\ListsRef\Traits was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type \Aimeos\MShop\Common\Item\PropertyRef\Traits was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
		PropertyRef\Traits::__clone as __cloneProperty;
30
		ListsRef\Traits::__clone insteadof PropertyRef\Traits;
31
		ListsRef\Traits::__clone as __cloneList;
32
		ListsRef\Traits::getName as getNameList;
33
	}
34
35
36
	private $langid;
37
38
39
	/**
40
	 * Initializes the media item object.
41
	 *
42
	 * @param array $values Initial values of the media item
43
	 * @param \Aimeos\MShop\Common\Item\Lists\Iface[] $listItems List of list items
44
	 * @param \Aimeos\MShop\Common\Item\Iface[] $refItems List of referenced items
45
	 * @param \Aimeos\MShop\Common\Item\Property\Iface[] $propItems List of property items
46
	 */
47
	public function __construct( array $values = [], array $listItems = [],
48
		array $refItems = [], array $propItems = [] )
49
	{
50
		if( isset( $values['media.preview'] ) && !isset( $values['media.previews'] ) ) {
51
			$values['media.previews'] = [1 => $values['media.preview']];
52
		}
53
54
		parent::__construct( 'media.', $values );
55
56
		$this->langid = ( isset( $values['.languageid'] ) ? $values['.languageid'] : null );
57
		$this->initListItems( $listItems, $refItems );
58
		$this->initPropertyItems( $propItems );
59
	}
60
61
62
	/**
63
	 * Creates a deep clone of all objects
64
	 */
65
	public function __clone()
66
	{
67
		parent::__clone();
68
		$this->__cloneList();
69
		$this->__cloneProperty();
70
	}
71
72
73
	/**
74
	 * Returns the name of the file system the referenced file is stored.
75
	 *
76
	 * @return string Name of the file system
77
	 */
78
	public function getFileSystem() : string
79
	{
80
		return $this->get( 'media.filesystem', 'fs-media' );
81
	}
82
83
84
	/**
85
	 * Sets the name of the file system the referenced file is stored.
86
	 *
87
	 * @param string $value Name of the file system
88
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
89
	 */
90
	public function setFileSystem( string $value ) : \Aimeos\MShop\Media\Item\Iface
91
	{
92
		return $this->set( 'media.filesystem', $value );
93
	}
94
95
96
	/**
97
	 * Returns the ISO language code.
98
	 *
99
	 * @return string|null ISO language code (e.g. de or de_DE)
100
	 */
101
	public function getLanguageId() : ?string
102
	{
103
		return $this->get( 'media.languageid' );
104
	}
105
106
107
	/**
108
	 * Sets the ISO language code.
109
	 *
110
	 * @param string|null $id ISO language code (e.g. de or de_DE)
111
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
112
	 * @throws \Aimeos\MShop\Exception If the language ID is invalid
113
	 */
114
	public function setLanguageId( ?string $id ) : \Aimeos\MShop\Media\Item\Iface
115
	{
116
		return $this->set( 'media.languageid', $this->checkLanguageId( $id ) );
117
	}
118
119
120
	/**
121
	 * Returns the type code of the media item.
122
	 *
123
	 * @return string|null Type code of the media item
124
	 */
125
	public function getType() : ?string
126
	{
127
		return $this->get( 'media.type', 'default' );
128
	}
129
130
131
	/**
132
	 * Sets the new type of the media.
133
	 *
134
	 * @param string $type Type of the media
135
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
136
	 */
137
	public function setType( string $type ) : \Aimeos\MShop\Common\Item\Iface
138
	{
139
		return $this->set( 'media.type', $this->checkCode( $type ) );
140
	}
141
142
143
	/**
144
	 * Returns the domain of the media item, if available.
145
	 *
146
	 * @return string Domain the media item belongs to
147
	 */
148
	public function getDomain() : string
149
	{
150
		return (string) $this->get( 'media.domain', '' );
151
	}
152
153
154
	/**
155
	 * Sets the domain of the media item.
156
	 *
157
	 * @param string $domain Domain of media item
158
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
159
	 */
160
	public function setDomain( string $domain ) : \Aimeos\MShop\Common\Item\Iface
161
	{
162
		return $this->set( 'media.domain', (string) $domain );
163
	}
164
165
166
	/**
167
	 * Returns the label of the media item.
168
	 *
169
	 * @return string Label of the media item
170
	 */
171
	public function getLabel() : string
172
	{
173
		return (string) $this->get( 'media.label', '' );
174
	}
175
176
177
	/**
178
	 * Sets the new label of the media item.
179
	 *
180
	 * @param string $label Label of the media item
181
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
182
	 */
183
	public function setLabel( ?string $label ) : \Aimeos\MShop\Media\Item\Iface
184
	{
185
		return $this->set( 'media.label', (string) $label );
186
	}
187
188
189
	/**
190
	 * Returns the status of the media item.
191
	 *
192
	 * @return int Status of the item
193
	 */
194
	public function getStatus() : int
195
	{
196
		return (int) $this->get( 'media.status', 1 );
197
	}
198
199
200
	/**
201
	 * Sets the new status of the media item.
202
	 *
203
	 * @param int $status Status of the item
204
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
205
	 */
206
	public function setStatus( int $status ) : \Aimeos\MShop\Common\Item\Iface
207
	{
208
		return $this->set( 'media.status', $status );
209
	}
210
211
212
	/**
213
	 * Returns the mime type of the media item.
214
	 *
215
	 * @return string Mime type of the media item
216
	 */
217
	public function getMimeType() : string
218
	{
219
		return (string) $this->get( 'media.mimetype', '' );
220
	}
221
222
223
	/**
224
	 * Sets the new mime type of the media.
225
	 *
226
	 * @param string $mimetype Mime type of the media item
227
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
228
	 */
229
	public function setMimeType( string $mimetype ) : \Aimeos\MShop\Media\Item\Iface
230
	{
231
		if( preg_match( '/^[a-z\-]+\/[a-zA-Z0-9\.\-\+]+$/', $mimetype ) !== 1 ) {
232
			throw new \Aimeos\MShop\Media\Exception( sprintf( 'Invalid mime type "%1$s"', $mimetype ) );
233
		}
234
235
		return $this->set( 'media.mimetype', (string) $mimetype );
236
	}
237
238
239
	/**
240
	 * Returns the url of the media item.
241
	 *
242
	 * @param bool $version TRUE to add file version as parameter, FALSE for path only
243
	 * @return string URL of the media file
244
	 */
245
	public function getUrl( bool $version = false ) : string
246
	{
247
		$url = (string) $this->get( 'media.url', '' );
248
249
		if( $url && $version && !\Aimeos\Base\Str::starts( $url, ['http', 'data:', '/'] ) && $this->getTimeModified() ) {
250
			$url .= '?v=' . str_replace( ['-', ' ', ':'], '', $this->getTimeModified() );
251
		}
252
253
		return $url;
254
	}
255
256
257
	/**
258
	 * Sets the new url of the media item.
259
	 *
260
	 * @param string|null $url URL of the media file
261
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
262
	 */
263
	public function setUrl( ?string $url ) : \Aimeos\MShop\Media\Item\Iface
264
	{
265
		return $this->set( 'media.url', (string) $url );
266
	}
267
268
269
	/**
270
	 * Returns the preview url of the media item.
271
	 *
272
	 * @param bool|int $size TRUE for the largest image, FALSE for the smallest or a concrete image width
273
	 * @return string Preview URL of the media file
274
	 */
275
	public function getPreview( $width = false ) : string
276
	{
277
		if( ( $list = (array) $this->get( 'media.previews', [] ) ) === [] ) {
278
			return '';
279
		}
280
281
		ksort( $list );
282
		$path = '';
283
284
		if( $width === false ) {
285
			$path = reset( $list );
286
		} elseif( $width === true ) {
287
			$path = end( $list );
288
		} elseif( isset( $list[$width] ) ) {
289
			$path = $list[$width];
290
		} else {
291
			$before = $after = [];
292
293
			foreach( $list as $idx => $path )
294
			{
295
				if( $idx < $width ) {
296
					$before[$idx] = $path;
297
				} else {
298
					$after[$idx] = $path;
299
				}
300
			}
301
302
			if( ( $path = array_shift( $after ) ) === null && ( $path = array_pop( $before ) ) === null ) {
303
				return '';
304
			}
305
		}
306
307
		if( $path && !\Aimeos\Base\Str::starts( $path, ['http', 'data:', '/'] ) && $this->getTimeModified() ) {
308
			$path .= '?v=' . str_replace( ['-', ' ', ':'], '', $this->getTimeModified() );
309
		}
310
311
		return $path;
312
	}
313
314
315
	/**
316
	 * Returns all preview urls for images of different sizes.
317
	 *
318
	 * @param bool $version TRUE to add file version as parameter, FALSE for path only
319
	 * @return array Associative list of widths in pixels as keys and urls as values
320
	 */
321
	public function getPreviews( bool $version = false ) : array
322
	{
323
		$previews = (array) $this->get( 'media.previews', [] );
324
325
		if( $version && $this->getTimeModified() )
326
		{
327
			foreach( $previews as $key => $path )
328
			{
329
				if( $path && !\Aimeos\Base\Str::starts( $path, ['http', 'data:', '/'] ) ) {
330
					$previews[$key] = $path . '?v=' . str_replace( ['-', ' ', ':'], '', $this->getTimeModified() );
331
				}
332
			}
333
		}
334
335
		return $previews;
336
	}
337
338
339
	/**
340
	 * Sets the new preview url of the media item.
341
	 *
342
	 * @param string $url Preview URL of the media file
343
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
344
	 */
345
	public function setPreview( string $url ) : \Aimeos\MShop\Media\Item\Iface
346
	{
347
		return $this->set( 'media.previews', [1 => $url] );
348
	}
349
350
351
	/**
352
	 * Sets the new preview urls for images of different sizes.
353
	 *
354
	 * @param array $url List of preview URLs with widths of the media file in pixels as keys
355
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
356
	 */
357
	public function setPreviews( array $urls ) : \Aimeos\MShop\Media\Item\Iface
358
	{
359
		return $this->set( 'media.previews', $urls );
360
	}
361
362
363
	/**
364
	 * Returns the localized text type of the item or the internal label if no name is available.
365
	 *
366
	 * @param string $type Text type to be returned
367
	 * @param string|null $langId Two letter ISO Language code of the text
368
	 * @return string Specified text type or label of the item
369
	 */
370
	public function getName( string $type = 'name', string $langId = null ) : string
371
	{
372
		foreach( $this->getPropertyItems( $type ) as $propItem )
373
		{
374
			if( $propItem->getLanguageId() === $langId || $langId === null ) {
375
				return $propItem->getValue();
376
			}
377
		}
378
379
		return $this->getNameList( $type );
380
	}
381
382
383
	/**
384
	 * Returns the item type
385
	 *
386
	 * @return string Item type, subtypes are separated by slashes
387
	 */
388
	public function getResourceType() : string
389
	{
390
		return 'media';
391
	}
392
393
394
	/**
395
	 * Tests if the item is available based on status, time, language and currency
396
	 *
397
	 * @return bool True if available, false if not
398
	 */
399
	public function isAvailable() : bool
400
	{
401
		return parent::isAvailable() && $this->getStatus() > 0
402
			&& ( $this->langid === null || $this->getLanguageId() === null
403
			|| $this->getLanguageId() === $this->langid );
404
	}
405
406
407
	/*
408
	 * Sets the item values from the given array and removes that entries from the list
409
	 *
410
	 * @param array &$list Associative list of item keys and their values
411
	 * @param bool True to set private properties too, false for public only
412
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
413
	 */
414
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
415
	{
416
		$item = parent::fromArray( $list, $private );
417
418
		foreach( $list as $key => $value )
419
		{
420
			switch( $key )
421
			{
422
				case 'media.filesystem': $item = $item->setFileSystem( $value ); break;
423
				case 'media.domain': $item = $item->setDomain( $value ); break;
424
				case 'media.label': $item = $item->setLabel( $value ); break;
425
				case 'media.languageid': $item = $item->setLanguageId( $value ); break;
426
				case 'media.mimetype': $item = $item->setMimeType( $value ); break;
427
				case 'media.type': $item = $item->setType( $value ); break;
428
				case 'media.url': $item = $item->setUrl( $value ); break;
429
				case 'media.preview': $item = $item->setPreview( $value ); break;
430
				case 'media.previews': $item = $item->setPreviews( (array) $value ); break;
431
				case 'media.status': $item = $item->setStatus( (int) $value ); break;
432
				default: continue 2;
433
			}
434
435
			unset( $list[$key] );
436
		}
437
438
		return $item;
439
	}
440
441
442
	/**
443
	 * Returns the item values as array.
444
	 *
445
	 * @param bool True to return private properties, false for public only
446
	 * @return array Associative list of item properties and their values
447
	 */
448
	public function toArray( bool $private = false ) : array
449
	{
450
		$list = parent::toArray( $private );
451
452
		$list['media.filesystem'] = $this->getFileSystem();
453
		$list['media.domain'] = $this->getDomain();
454
		$list['media.label'] = $this->getLabel();
455
		$list['media.languageid'] = $this->getLanguageId();
456
		$list['media.mimetype'] = $this->getMimeType();
457
		$list['media.type'] = $this->getType();
458
		$list['media.preview'] = $this->getPreview();
459
		$list['media.previews'] = $this->getPreviews();
460
		$list['media.url'] = $this->getUrl();
461
		$list['media.status'] = $this->getStatus();
462
463
		return $list;
464
	}
465
}
466