Passed
Push — master ( 484f92...9dd3d0 )
by Aimeos
04:56
created

Standard   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 437
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 110
dl 0
loc 437
rs 5.04
c 0
b 0
f 0
wmc 57

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 5 1
A __construct() 0 8 1
A getResourceType() 0 3 1
A getName() 0 9 2
A isAvailable() 0 6 5
A setLanguageId() 0 9 2
A getLanguageId() 0 7 2
A getMimeType() 0 7 2
A setDomain() 0 9 2
A getPreview() 0 7 2
A setLabel() 0 9 2
A getUrl() 0 7 2
A getLabel() 0 7 2
A setUrl() 0 9 2
A setType() 0 9 2
A getStatus() 0 7 2
A setStatus() 0 9 2
A setMimeType() 0 13 3
A getDomain() 0 7 2
A getType() 0 4 2
A setPreview() 0 3 1
B fromArray() 0 23 10
A setPreviews() 0 9 2
A toArray() 0 14 1
A getPreviews() 0 7 2

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