Total Complexity | 57 |
Total Lines | 437 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |
||
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() |
||
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() |
||
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() |
||
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 ) |
||
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 ) |
||
458 | } |
||
459 | |||
461 |