Passed
Push — master ( f317d9...8fa1ff )
by Aimeos
05:07
created

Standard   F

Complexity

Total Complexity 61

Size/Duplication

Total Lines 429
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 61
eloc 101
c 1
b 0
f 0
dl 0
loc 429
rs 3.52

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 getUrl() 0 3 1
A __clone() 0 5 1
A getLabel() 0 3 1
A __construct() 0 12 4
A setUrl() 0 3 1
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 setPreview() 0 3 1
B getPreview() 0 35 10
C fromArray() 0 25 12
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 14 4

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
	 * @return string URL of the media file
243
	 */
244
	public function getUrl() : string
245
	{
246
		return (string) $this->get( 'media.url', '' );
247
	}
248
249
250
	/**
251
	 * Sets the new url of the media item.
252
	 *
253
	 * @param string|null $url URL of the media file
254
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
255
	 */
256
	public function setUrl( ?string $url ) : \Aimeos\MShop\Media\Item\Iface
257
	{
258
		return $this->set( 'media.url', (string) $url );
259
	}
260
261
262
	/**
263
	 * Returns the preview url of the media item.
264
	 *
265
	 * @param bool|int $size TRUE for the largest image, FALSE for the smallest or a concrete image width
266
	 * @return string Preview URL of the media file
267
	 */
268
	public function getPreview( $width = false ) : string
269
	{
270
		if( ( $list = (array) $this->get( 'media.previews', [] ) ) === [] ) {
271
			return '';
272
		}
273
274
		ksort( $list );
275
		$time = $this->getTimeModified() ? '?v=' . str_replace( ['-', ' ', ':'], '', $this->getTimeModified() ) : '';
276
277
		if( $width === false ) {
278
			return (string) reset( $list ) . $time;
279
		} elseif( $width === true ) {
280
			return (string) end( $list ) . $time;
281
		} elseif( isset( $list[$width] ) ) {
282
			return (string) $list[$width] . $time;
283
		}
284
285
		$before = $after = [];
286
287
		foreach( $list as $idx => $path )
288
		{
289
			if( $idx < $width ) {
290
				$before[$idx] = $path;
291
			} else {
292
				$after[$idx] = $path;
293
			}
294
		}
295
296
		if( ( $path = array_shift( $after ) ) !== null ) {
297
			return (string) $path . $time;
298
		} elseif( ( $path = array_pop( $before ) ) !== null ) {
299
			return (string) $path . $time;
300
		}
301
302
		return '';
303
	}
304
305
306
	/**
307
	 * Returns all preview urls for images of different sizes.
308
	 *
309
	 * @return array Associative list of widths in pixels as keys and urls as values
310
	 */
311
	public function getPreviews( bool $version = false ) : array
312
	{
313
		$previews = (array) $this->get( 'media.previews', [] );
314
315
		if( $version )
316
		{
317
			$time = $this->getTimeModified() ? '?v=' . str_replace( ['-', ' ', ':'], '', $this->getTimeModified() ) : '';
318
319
			foreach( $previews as $key => $path ) {
320
				$previews[$key] = $path . $time;
321
			}
322
		}
323
324
		return $previews;
325
	}
326
327
328
	/**
329
	 * Sets the new preview url of the media item.
330
	 *
331
	 * @param string $url Preview URL of the media file
332
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
333
	 */
334
	public function setPreview( string $url ) : \Aimeos\MShop\Media\Item\Iface
335
	{
336
		return $this->set( 'media.previews', [1 => $url] );
337
	}
338
339
340
	/**
341
	 * Sets the new preview urls for images of different sizes.
342
	 *
343
	 * @param array $url List of preview URLs with widths of the media file in pixels as keys
344
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
345
	 */
346
	public function setPreviews( array $urls ) : \Aimeos\MShop\Media\Item\Iface
347
	{
348
		return $this->set( 'media.previews', $urls );
349
	}
350
351
352
	/**
353
	 * Returns the localized text type of the item or the internal label if no name is available.
354
	 *
355
	 * @param string $type Text type to be returned
356
	 * @param string|null $langId Two letter ISO Language code of the text
357
	 * @return string Specified text type or label of the item
358
	 */
359
	public function getName( string $type = 'name', string $langId = null ) : string
360
	{
361
		foreach( $this->getPropertyItems( $type ) as $propItem )
362
		{
363
			if( $propItem->getLanguageId() === $langId || $langId === null ) {
364
				return $propItem->getValue();
365
			}
366
		}
367
368
		return $this->getNameList( $type );
369
	}
370
371
372
	/**
373
	 * Returns the item type
374
	 *
375
	 * @return string Item type, subtypes are separated by slashes
376
	 */
377
	public function getResourceType() : string
378
	{
379
		return 'media';
380
	}
381
382
383
	/**
384
	 * Tests if the item is available based on status, time, language and currency
385
	 *
386
	 * @return bool True if available, false if not
387
	 */
388
	public function isAvailable() : bool
389
	{
390
		return parent::isAvailable() && $this->getStatus() > 0
391
			&& ( $this->langid === null || $this->getLanguageId() === null
392
			|| $this->getLanguageId() === $this->langid );
393
	}
394
395
396
	/*
397
	 * Sets the item values from the given array and removes that entries from the list
398
	 *
399
	 * @param array &$list Associative list of item keys and their values
400
	 * @param bool True to set private properties too, false for public only
401
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
402
	 */
403
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
404
	{
405
		$item = parent::fromArray( $list, $private );
406
407
		foreach( $list as $key => $value )
408
		{
409
			switch( $key )
410
			{
411
				case 'media.filesystem': $item = $item->setFileSystem( $value ); break;
412
				case 'media.domain': $item = $item->setDomain( $value ); break;
413
				case 'media.label': $item = $item->setLabel( $value ); break;
414
				case 'media.languageid': $item = $item->setLanguageId( $value ); break;
415
				case 'media.mimetype': $item = $item->setMimeType( $value ); break;
416
				case 'media.type': $item = $item->setType( $value ); break;
417
				case 'media.url': $item = $item->setUrl( $value ); break;
418
				case 'media.preview': $item = $item->setPreview( $value ); break;
419
				case 'media.previews': $item = $item->setPreviews( (array) $value ); break;
420
				case 'media.status': $item = $item->setStatus( (int) $value ); break;
421
				default: continue 2;
422
			}
423
424
			unset( $list[$key] );
425
		}
426
427
		return $item;
428
	}
429
430
431
	/**
432
	 * Returns the item values as array.
433
	 *
434
	 * @param bool True to return private properties, false for public only
435
	 * @return array Associative list of item properties and their values
436
	 */
437
	public function toArray( bool $private = false ) : array
438
	{
439
		$list = parent::toArray( $private );
440
441
		$list['media.filesystem'] = $this->getFileSystem();
442
		$list['media.domain'] = $this->getDomain();
443
		$list['media.label'] = $this->getLabel();
444
		$list['media.languageid'] = $this->getLanguageId();
445
		$list['media.mimetype'] = $this->getMimeType();
446
		$list['media.type'] = $this->getType();
447
		$list['media.preview'] = $this->getPreview();
448
		$list['media.previews'] = $this->getPreviews();
449
		$list['media.url'] = $this->getUrl();
450
		$list['media.status'] = $this->getStatus();
451
452
		return $list;
453
	}
454
}
455