Passed
Push — master ( ecc17a...1d1141 )
by Aimeos
05:23
created

Standard   C

Complexity

Total Complexity 54

Size/Duplication

Total Lines 390
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 90
c 2
b 0
f 0
dl 0
loc 390
rs 6.4799
wmc 54

25 Methods

Rating   Name   Duplication   Size   Complexity  
A getMimeType() 0 3 1
A setDomain() 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 32 9
B fromArray() 0 24 11
A setPreviews() 0 3 1
A toArray() 0 15 1
A getResourceType() 0 3 1
A getName() 0 10 4
A isAvailable() 0 5 5
A getPreviews() 0 3 1

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-2021
7
 * @package MShop
8
 * @subpackage Media
9
 */
10
11
12
namespace Aimeos\MShop\Media\Item;
13
14
use \Aimeos\MShop\Common\Item\ListsRef;
15
use \Aimeos\MShop\Common\Item\PropertyRef;
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 {
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 ISO language code.
75
	 *
76
	 * @return string|null ISO language code (e.g. de or de_DE)
77
	 */
78
	public function getLanguageId() : ?string
79
	{
80
		return $this->get( 'media.languageid' );
81
	}
82
83
84
	/**
85
	 * Sets the ISO language code.
86
	 *
87
	 * @param string|null $id ISO language code (e.g. de or de_DE)
88
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
89
	 * @throws \Aimeos\MShop\Exception If the language ID is invalid
90
	 */
91
	public function setLanguageId( ?string $id ) : \Aimeos\MShop\Media\Item\Iface
92
	{
93
		return $this->set( 'media.languageid', $this->checkLanguageId( $id ) );
94
	}
95
96
97
	/**
98
	 * Returns the type code of the media item.
99
	 *
100
	 * @return string|null Type code of the media item
101
	 */
102
	public function getType() : ?string
103
	{
104
		return $this->get( 'media.type', 'default' );
105
	}
106
107
108
	/**
109
	 * Sets the new type of the media.
110
	 *
111
	 * @param string $type Type of the media
112
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
113
	 */
114
	public function setType( string $type ) : \Aimeos\MShop\Common\Item\Iface
115
	{
116
		return $this->set( 'media.type', $this->checkCode( $type ) );
117
	}
118
119
120
	/**
121
	 * Returns the domain of the media item, if available.
122
	 *
123
	 * @return string Domain the media item belongs to
124
	 */
125
	public function getDomain() : string
126
	{
127
		return (string) $this->get( 'media.domain', '' );
128
	}
129
130
131
	/**
132
	 * Sets the domain of the media item.
133
	 *
134
	 * @param string $domain Domain of media item
135
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
136
	 */
137
	public function setDomain( string $domain ) : \Aimeos\MShop\Common\Item\Iface
138
	{
139
		return $this->set( 'media.domain', (string) $domain );
140
	}
141
142
143
	/**
144
	 * Returns the label of the media item.
145
	 *
146
	 * @return string Label of the media item
147
	 */
148
	public function getLabel() : string
149
	{
150
		return (string) $this->get( 'media.label', '' );
151
	}
152
153
154
	/**
155
	 * Sets the new label of the media item.
156
	 *
157
	 * @param string $label Label of the media item
158
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
159
	 */
160
	public function setLabel( ?string $label ) : \Aimeos\MShop\Media\Item\Iface
161
	{
162
		return $this->set( 'media.label', (string) $label );
163
	}
164
165
166
	/**
167
	 * Returns the status of the media item.
168
	 *
169
	 * @return int Status of the item
170
	 */
171
	public function getStatus() : int
172
	{
173
		return (int) $this->get( 'media.status', 1 );
174
	}
175
176
177
	/**
178
	 * Sets the new status of the media item.
179
	 *
180
	 * @param int $status Status of the item
181
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
182
	 */
183
	public function setStatus( int $status ) : \Aimeos\MShop\Common\Item\Iface
184
	{
185
		return $this->set( 'media.status', $status );
186
	}
187
188
189
	/**
190
	 * Returns the mime type of the media item.
191
	 *
192
	 * @return string Mime type of the media item
193
	 */
194
	public function getMimeType() : string
195
	{
196
		return (string) $this->get( 'media.mimetype', '' );
197
	}
198
199
200
	/**
201
	 * Sets the new mime type of the media.
202
	 *
203
	 * @param string $mimetype Mime type of the media item
204
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
205
	 */
206
	public function setMimeType( string $mimetype ) : \Aimeos\MShop\Media\Item\Iface
207
	{
208
		if( preg_match( '/^[a-z\-]+\/[a-zA-Z0-9\.\-\+]+$/', $mimetype ) !== 1 ) {
209
			throw new \Aimeos\MShop\Media\Exception( sprintf( 'Invalid mime type "%1$s"', $mimetype ) );
210
		}
211
212
		return $this->set( 'media.mimetype', (string) $mimetype );
213
	}
214
215
216
	/**
217
	 * Returns the url of the media item.
218
	 *
219
	 * @return string URL of the media file
220
	 */
221
	public function getUrl() : string
222
	{
223
		return (string) $this->get( 'media.url', '' );
224
	}
225
226
227
	/**
228
	 * Sets the new url of the media item.
229
	 *
230
	 * @param string|null $url URL of the media file
231
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
232
	 */
233
	public function setUrl( ?string $url ) : \Aimeos\MShop\Media\Item\Iface
234
	{
235
		return $this->set( 'media.url', (string) $url );
236
	}
237
238
239
	/**
240
	 * Returns the preview url of the media item.
241
	 *
242
	 * @param bool|int $size TRUE for the largest image, FALSE for the smallest or a concrete image width
243
	 * @return string Preview URL of the media file
244
	 */
245
	public function getPreview( $width = false ) : string
246
	{
247
		if( ( $list = (array) $this->get( 'media.previews', [] ) ) === [] ) {
248
			return '';
249
		}
250
251
		if( $width === false ) {
252
			return (string) reset( $list );
253
		} elseif ( $width === true ) {
254
			return (string) end( $list );
255
		} elseif( isset( $list[$width] ) ) {
256
			return (string) $list[$width];
257
		}
258
259
		$before = $after = [];
260
261
		foreach( $list as $idx => $path )
262
		{
263
			if( $idx < $width ) {
264
				$before[$idx] = $path;
265
			} else {
266
				$after[$idx] = $path;
267
			}
268
		}
269
270
		if( ( $path = array_shift( $after ) ) !== null ) {
271
			return (string) $path;
272
		} elseif( ( $path = array_pop( $before ) ) !== null ) {
273
			return (string) $path;
274
		}
275
276
		return '';
277
	}
278
279
280
	/**
281
	 * Returns all preview urls for images of different sizes.
282
	 *
283
	 * @return array Associative list of widths in pixels as keys and urls as values
284
	 */
285
	public function getPreviews() : array
286
	{
287
		return (array) $this->get( 'media.previews', [] );
288
	}
289
290
291
	/**
292
	 * Sets the new preview url of the media item.
293
	 *
294
	 * @param string $url Preview URL of the media file
295
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
296
	 */
297
	public function setPreview( string $url ) : \Aimeos\MShop\Media\Item\Iface
298
	{
299
		return $this->set( 'media.previews', [1 => $url] );
300
	}
301
302
303
	/**
304
	 * Sets the new preview urls for images of different sizes.
305
	 *
306
	 * @param array $url List of preview URLs with widths of the media file in pixels as keys
307
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
308
	 */
309
	public function setPreviews( array $urls ) : \Aimeos\MShop\Media\Item\Iface
310
	{
311
		return $this->set( 'media.previews', $urls );
312
	}
313
314
315
	/**
316
	 * Returns the localized text type of the item or the internal label if no name is available.
317
	 *
318
	 * @param string $type Text type to be returned
319
	 * @param string|null $langId Two letter ISO Language code of the text
320
	 * @return string Specified text type or label of the item
321
	 */
322
	public function getName( string $type = 'name', string $langId = null ) : string
323
	{
324
		foreach( $this->getPropertyItems( $type ) as $propItem )
325
		{
326
			if( $propItem->getLanguageId() === $langId || $langId === null ) {
327
				return $propItem->getValue();
328
			}
329
		}
330
331
		return $this->getNameList( $type );
332
	}
333
334
335
	/**
336
	 * Returns the item type
337
	 *
338
	 * @return string Item type, subtypes are separated by slashes
339
	 */
340
	public function getResourceType() : string
341
	{
342
		return 'media';
343
	}
344
345
346
	/**
347
	 * Tests if the item is available based on status, time, language and currency
348
	 *
349
	 * @return bool True if available, false if not
350
	 */
351
	public function isAvailable() : bool
352
	{
353
		return parent::isAvailable() && $this->getStatus() > 0
354
			&& ( $this->langid === null || $this->getLanguageId() === null
355
			|| $this->getLanguageId() === $this->langid );
356
	}
357
358
359
	/*
360
	 * Sets the item values from the given array and removes that entries from the list
361
	 *
362
	 * @param array &$list Associative list of item keys and their values
363
	 * @param bool True to set private properties too, false for public only
364
	 * @return \Aimeos\MShop\Media\Item\Iface Media item for chaining method calls
365
	 */
366
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
367
	{
368
		$item = parent::fromArray( $list, $private );
369
370
		foreach( $list as $key => $value )
371
		{
372
			switch( $key )
373
			{
374
				case 'media.domain': $item = $item->setDomain( $value ); break;
375
				case 'media.label': $item = $item->setLabel( $value ); break;
376
				case 'media.languageid': $item = $item->setLanguageId( $value ); break;
377
				case 'media.mimetype': $item = $item->setMimeType( $value ); break;
378
				case 'media.type': $item = $item->setType( $value ); break;
379
				case 'media.url': $item = $item->setUrl( $value ); break;
380
				case 'media.preview': $item = $item->setPreview( $value ); break;
381
				case 'media.previews': $item = $item->setPreviews( (array) $value ); break;
382
				case 'media.status': $item = $item->setStatus( (int) $value ); break;
383
				default: continue 2;
384
			}
385
386
			unset( $list[$key] );
387
		}
388
389
		return $item;
390
	}
391
392
393
	/**
394
	 * Returns the item values as array.
395
	 *
396
	 * @param bool True to return private properties, false for public only
397
	 * @return array Associative list of item properties and their values
398
	 */
399
	public function toArray( bool $private = false ) : array
400
	{
401
		$list = parent::toArray( $private );
402
403
		$list['media.domain'] = $this->getDomain();
404
		$list['media.label'] = $this->getLabel();
405
		$list['media.languageid'] = $this->getLanguageId();
406
		$list['media.mimetype'] = $this->getMimeType();
407
		$list['media.type'] = $this->getType();
408
		$list['media.preview'] = $this->getPreview();
409
		$list['media.previews'] = $this->getPreviews();
410
		$list['media.url'] = $this->getUrl();
411
		$list['media.status'] = $this->getStatus();
412
413
		return $list;
414
	}
415
}
416