| Total Complexity | 51 |
| Total Lines | 479 |
| 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\Locale\Item\Site\Iface |
||
| 24 | { |
||
| 25 | use \Aimeos\MShop\Common\Item\Config\Traits; |
||
| 26 | |||
| 27 | |||
| 28 | private $children; |
||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * Initializes the site object. |
||
| 33 | * |
||
| 34 | * @param array $values Associative list of item key/value pairs |
||
| 35 | * @param \Aimeos\MW\Tree\Node\Iface[] $children List of tree nodes |
||
| 36 | */ |
||
| 37 | public function __construct( array $values = [], array $children = [] ) |
||
| 38 | { |
||
| 39 | map( $children )->implements( \Aimeos\MShop\Locale\Item\Site\Iface::class, true ); |
||
| 40 | |||
| 41 | parent::__construct( 'locale.site.', $values ); |
||
| 42 | $this->children = $children; |
||
| 43 | } |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Creates a deep clone of all objects |
||
| 48 | */ |
||
| 49 | public function __clone() |
||
| 50 | { |
||
| 51 | foreach( $this->children as $key => $item ) { |
||
| 52 | $this->children[$key] = clone $item; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Returns the ID of the site. |
||
| 59 | * |
||
| 60 | * @return string Unique ID of the site |
||
| 61 | */ |
||
| 62 | public function getSiteId() : string |
||
| 63 | { |
||
| 64 | return $this->get( 'locale.site.siteid', '' ); |
||
| 65 | } |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * Sets the ID of the site. |
||
| 70 | * |
||
| 71 | * @param string $value Unique ID of the site |
||
| 72 | * @return \Aimeos\MShop\Locale\Item\Site\Iface Locale site item for chaining method calls |
||
| 73 | */ |
||
| 74 | public function setSiteId( string $value ) : \Aimeos\MShop\Locale\Item\Site\Iface |
||
| 75 | { |
||
| 76 | return $this->set( 'locale.site.siteid', $value ); |
||
| 77 | } |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * Returns the code of the site. |
||
| 82 | * |
||
| 83 | * @return string Returns the code of the item |
||
| 84 | */ |
||
| 85 | public function getCode() : string |
||
| 86 | { |
||
| 87 | return $this->get( 'locale.site.code', '' ); |
||
| 88 | } |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * Sets the code of the site. |
||
| 93 | * |
||
| 94 | * @param string $code The code to set |
||
| 95 | * @return \Aimeos\MShop\Locale\Item\Site\Iface Locale site item for chaining method calls |
||
| 96 | */ |
||
| 97 | public function setCode( string $code ) : \Aimeos\MShop\Common\Item\Tree\Iface |
||
| 98 | { |
||
| 99 | return $this->set( 'locale.site.code', $this->checkCode( $code, 255 ) ); |
||
| 100 | } |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns the config property of the site. |
||
| 105 | * |
||
| 106 | * @return array Returns the config of the Site |
||
| 107 | */ |
||
| 108 | public function getConfig() : array |
||
| 109 | { |
||
| 110 | return $this->get( 'locale.site.config', [] ); |
||
| 111 | } |
||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * Sets the config property of the site. |
||
| 116 | * |
||
| 117 | * @param array $options Options to be set for the Site |
||
| 118 | * @return \Aimeos\MShop\Locale\Item\Site\Iface Locale site item for chaining method calls |
||
| 119 | */ |
||
| 120 | public function setConfig( array $options ) : \Aimeos\MShop\Common\Item\Iface |
||
| 121 | { |
||
| 122 | return $this->set( 'locale.site.config', $options ); |
||
| 123 | } |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns the label property of the site. |
||
| 128 | * |
||
| 129 | * @return string Returns the label of the Site |
||
| 130 | */ |
||
| 131 | public function getLabel() : string |
||
| 134 | } |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * Sets the label property of the site. |
||
| 139 | * |
||
| 140 | * @param string $label The label of the Site |
||
| 141 | * @return \Aimeos\MShop\Locale\Item\Site\Iface Locale site item for chaining method calls |
||
| 142 | */ |
||
| 143 | public function setLabel( string $label ) : \Aimeos\MShop\Common\Item\Tree\Iface |
||
| 144 | { |
||
| 145 | return $this->set( 'locale.site.label', $label ); |
||
| 146 | } |
||
| 147 | |||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns the level of the item in the tree |
||
| 151 | * |
||
| 152 | * @return int Level of the item starting with "0" for the root node |
||
| 153 | */ |
||
| 154 | public function getLevel() : int |
||
| 155 | { |
||
| 156 | return 0; |
||
| 157 | } |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * Returns the logo path of the site. |
||
| 162 | * |
||
| 163 | * @param bool $large Return the largest image instead of the smallest |
||
| 164 | * @return string Returns the logo of the site |
||
| 165 | */ |
||
| 166 | public function getLogo( bool $large = false ) : string |
||
| 167 | { |
||
| 168 | if( ( $list = (array) $this->get( 'locale.site.logo', [] ) ) !== [] ) { |
||
| 169 | return (string) ( $large ? end( $list ) : current( $list ) ); |
||
| 170 | } |
||
| 171 | |||
| 172 | return ''; |
||
| 173 | } |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns the logo path of the site. |
||
| 178 | * |
||
| 179 | * @return string Returns the logo of the site |
||
| 180 | */ |
||
| 181 | public function getLogos() : array |
||
| 182 | { |
||
| 183 | return (array) $this->get( 'locale.site.logo', [] ); |
||
| 184 | } |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Returns the icon path of the site. |
||
| 189 | * |
||
| 190 | * @return string Returns the icon of the site |
||
| 191 | */ |
||
| 192 | public function getIcon() : string |
||
| 193 | { |
||
| 194 | return $this->get( 'locale.site.icon', '' ); |
||
| 195 | } |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * Sets the icon path of the site. |
||
| 200 | * |
||
| 201 | * @param string $value The icon of the site |
||
| 202 | * @return \Aimeos\MShop\Locale\Item\Site\Iface Locale site item for chaining method calls |
||
| 203 | */ |
||
| 204 | public function setIcon( string $value ) : \Aimeos\MShop\Common\Item\Tree\Iface |
||
| 207 | } |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * Sets the logo path of the site. |
||
| 212 | * |
||
| 213 | * @param string $value The logo of the site |
||
| 214 | * @return \Aimeos\MShop\Locale\Item\Site\Iface Locale site item for chaining method calls |
||
| 215 | */ |
||
| 216 | public function setLogo( string $value ) : \Aimeos\MShop\Common\Item\Tree\Iface |
||
| 219 | } |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * Sets the logo path of the site. |
||
| 224 | * |
||
| 225 | * @param array $value List of logo URLs with widths of the media file in pixels as keys |
||
| 226 | * @return \Aimeos\MShop\Locale\Item\Site\Iface Locale site item for chaining method calls |
||
| 227 | */ |
||
| 228 | public function setLogos( array $value ) : \Aimeos\MShop\Common\Item\Tree\Iface |
||
| 231 | } |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns the ID of the parent site |
||
| 236 | * |
||
| 237 | * @return string Unique ID of the parent site |
||
| 238 | */ |
||
| 239 | public function getParentId() : string |
||
| 242 | } |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * Returns the rating of the item |
||
| 247 | * |
||
| 248 | * @return string Decimal value of the item rating |
||
| 249 | */ |
||
| 250 | public function getRating() : string |
||
| 251 | { |
||
| 252 | return (string) $this->get( 'locale.site.rating', 0 ); |
||
| 253 | } |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns the total number of ratings for the item |
||
| 258 | * |
||
| 259 | * @return int Total number of ratings for the item |
||
| 260 | */ |
||
| 261 | public function getRatings() : int |
||
| 262 | { |
||
| 263 | return (int) $this->get( 'locale.site.ratings', 0 ); |
||
| 264 | } |
||
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * Returns the ID of the referenced customer/supplier related to the site. |
||
| 269 | * |
||
| 270 | * @return string Returns the referenced customer/supplier ID related to the site |
||
| 271 | */ |
||
| 272 | public function getRefId() : string |
||
| 273 | { |
||
| 274 | return $this->get( 'locale.site.refid', '' ); |
||
| 275 | } |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * Sets the ID of the referenced customer/supplier related to the site. |
||
| 280 | * |
||
| 281 | * @param string $value The referenced customer/supplier ID related to the site |
||
| 282 | * @return \Aimeos\MShop\Locale\Item\Site\Iface Locale site item for chaining method calls |
||
| 283 | */ |
||
| 284 | public function setRefId( string $value ) : \Aimeos\MShop\Common\Item\Tree\Iface |
||
| 285 | { |
||
| 286 | return $this->set( 'locale.site.refid', $value ); |
||
| 287 | } |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns the status property of the Site. |
||
| 292 | * |
||
| 293 | * @return int Returns the status of the Site |
||
| 294 | */ |
||
| 295 | public function getStatus() : int |
||
| 296 | { |
||
| 297 | return $this->get( 'locale.site.status', 1 ); |
||
| 298 | } |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * Sets status property. |
||
| 303 | * |
||
| 304 | * @param int $status The status of the Site |
||
| 305 | * @return \Aimeos\MShop\Locale\Item\Site\Iface Locale site item for chaining method calls |
||
| 306 | */ |
||
| 307 | public function setStatus( int $status ) : \Aimeos\MShop\Common\Item\Iface |
||
| 308 | { |
||
| 309 | return $this->set( 'locale.site.status', $status ); |
||
| 310 | } |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * Returns the theme name for the site. |
||
| 315 | * |
||
| 316 | * @return string|null Returns the theme name for the site or emtpy for default theme |
||
| 317 | */ |
||
| 318 | public function getTheme() : ?string |
||
| 319 | { |
||
| 320 | return $this->get( 'locale.site.theme' ); |
||
| 321 | } |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * Sets the theme name for the site. |
||
| 326 | * |
||
| 327 | * @param string $value The theme name for the site |
||
| 328 | * @return \Aimeos\MShop\Locale\Item\Site\Iface Locale site item for chaining method calls |
||
| 329 | */ |
||
| 330 | public function setTheme( string $value ) : \Aimeos\MShop\Common\Item\Tree\Iface |
||
| 333 | } |
||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * Returns the item type |
||
| 338 | * |
||
| 339 | * @return string Item type, subtypes are separated by slashes |
||
| 340 | */ |
||
| 341 | public function getResourceType() : string |
||
| 342 | { |
||
| 343 | return 'locale/site'; |
||
| 344 | } |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * Tests if the item is available based on status, time, language and currency |
||
| 349 | * |
||
| 350 | * @return bool True if available, false if not |
||
| 351 | */ |
||
| 352 | public function isAvailable() : bool |
||
| 353 | { |
||
| 354 | return parent::isAvailable() && $this->getStatus() > 0; |
||
| 355 | } |
||
| 356 | |||
| 357 | |||
| 358 | /* |
||
| 359 | * Sets the item values from the given array and removes that entries from the list |
||
| 360 | * |
||
| 361 | * @param array &$list Associative list of item keys and their values |
||
| 362 | * @param bool True to set private properties too, false for public only |
||
| 363 | * @return \Aimeos\MShop\Locale\Item\Site\Iface Site item for chaining method calls |
||
| 364 | */ |
||
| 365 | public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface |
||
| 366 | { |
||
| 367 | $item = parent::fromArray( $list, $private ); |
||
| 368 | |||
| 369 | foreach( $list as $key => $value ) |
||
| 370 | { |
||
| 371 | switch( $key ) |
||
| 372 | { |
||
| 373 | case 'locale.site.code': $item = $item->setCode( $value ); break; |
||
| 374 | case 'locale.site.label': $item = $item->setLabel( $value ); break; |
||
| 375 | case 'locale.site.status': $item = $item->setStatus( (int) $value ); break; |
||
| 376 | case 'locale.site.config': $item = $item->setConfig( (array) $value ); break; |
||
| 377 | case 'locale.site.refid': $item = $item->setRefId( $value ); break; |
||
| 378 | case 'locale.site.logo': $item = $item->setLogos( (array) $value ); break; |
||
| 379 | case 'locale.site.theme': $item = $item->setTheme( $value ); break; |
||
| 380 | case 'locale.site.icon': $item = $item->setIcon( $value ); break; |
||
| 381 | default: continue 2; |
||
| 382 | } |
||
| 383 | |||
| 384 | unset( $list[$key] ); |
||
| 385 | } |
||
| 386 | |||
| 387 | return $item; |
||
| 388 | } |
||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * Returns the item values as array. |
||
| 393 | * |
||
| 394 | * @param bool True to return private properties, false for public only |
||
| 395 | * @return array Associative list of item properties and their values |
||
| 396 | */ |
||
| 397 | public function toArray( bool $private = false ) : array |
||
| 398 | { |
||
| 399 | $list = parent::toArray( $private ); |
||
| 400 | |||
| 401 | $list['locale.site.code'] = $this->getCode(); |
||
| 402 | $list['locale.site.icon'] = $this->getIcon(); |
||
| 403 | $list['locale.site.logo'] = $this->getLogos(); |
||
| 404 | $list['locale.site.theme'] = $this->getTheme(); |
||
| 405 | $list['locale.site.label'] = $this->getLabel(); |
||
| 406 | $list['locale.site.status'] = $this->getStatus(); |
||
| 407 | $list['locale.site.refid'] = $this->getRefId(); |
||
| 408 | $list['locale.site.hasChildren'] = $this->hasChildren(); |
||
| 409 | $list['locale.site.ratings'] = $this->getRatings(); |
||
| 410 | $list['locale.site.rating'] = $this->getRating(); |
||
| 411 | |||
| 412 | if( $private === true ) |
||
| 413 | { |
||
| 414 | $list['locale.site.level'] = $this->getLevel(); |
||
| 415 | $list['locale.site.parentid'] = $this->getParentId(); |
||
| 416 | $list['locale.site.config'] = $this->getConfig(); |
||
| 417 | } |
||
| 418 | |||
| 419 | return $list; |
||
| 420 | } |
||
| 421 | |||
| 422 | |||
| 423 | /** |
||
| 424 | * Adds a child node to this node. |
||
| 425 | * |
||
| 426 | * @param \Aimeos\MShop\Common\Item\Tree\Iface $item Child node to add |
||
| 427 | * @return \Aimeos\MShop\Common\Item\Tree\Iface Tree item for chaining method calls |
||
| 428 | */ |
||
| 429 | public function addChild( \Aimeos\MShop\Common\Item\Tree\Iface $item ) : \Aimeos\MShop\Common\Item\Tree\Iface |
||
| 430 | { |
||
| 431 | return $this; |
||
| 432 | } |
||
| 433 | |||
| 434 | |||
| 435 | /** |
||
| 436 | * Removes a child node from this node. |
||
| 437 | * |
||
| 438 | * @param \Aimeos\MShop\Common\Item\Tree\Iface $item Child node to remove |
||
| 439 | * @return \Aimeos\MShop\Common\Item\Tree\Iface Tree item for chaining method calls |
||
| 440 | */ |
||
| 441 | public function deleteChild( \Aimeos\MShop\Common\Item\Tree\Iface $item ) : \Aimeos\MShop\Common\Item\Tree\Iface |
||
| 442 | { |
||
| 443 | return $this; |
||
| 444 | } |
||
| 445 | |||
| 446 | |||
| 447 | /** |
||
| 448 | * Returns a child of this node identified by its index. |
||
| 449 | * |
||
| 450 | * @param int $index Index of child node |
||
| 451 | * @return \Aimeos\MShop\Locale\Item\Site\Iface Selected node |
||
| 452 | */ |
||
| 453 | public function getChild( int $index ) : \Aimeos\MShop\Common\Item\Tree\Iface |
||
| 454 | { |
||
| 455 | throw new \Aimeos\MShop\Locale\Exception( sprintf( 'Child node with index "%1$d" not available', $index ) ); |
||
| 456 | } |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * Returns all children of this node. |
||
| 461 | * |
||
| 462 | * @return \Aimeos\Map Numerically indexed list of items implementing \Aimeos\MShop\Locale\Item\Site\Iface |
||
| 463 | */ |
||
| 464 | public function getChildren() : \Aimeos\Map |
||
| 467 | } |
||
| 468 | |||
| 469 | |||
| 470 | /** |
||
| 471 | * Returns the deleted children. |
||
| 472 | * |
||
| 473 | * @return \Aimeos\Map List of removed children implementing \Aimeos\MShop\Locale\Item\Site\Iface |
||
| 474 | */ |
||
| 475 | public function getChildrenDeleted() : \Aimeos\Map |
||
| 476 | { |
||
| 477 | return map(); |
||
| 478 | } |
||
| 479 | |||
| 480 | |||
| 481 | /** |
||
| 482 | * Tests if a node has children. |
||
| 483 | * |
||
| 484 | * @return bool True if node has children, false if not |
||
| 485 | */ |
||
| 486 | public function hasChildren() : bool |
||
| 489 | } |
||
| 490 | |||
| 491 | |||
| 492 | /** |
||
| 493 | * Returns the node and its children as list |
||
| 494 | * |
||
| 495 | * @return \Aimeos\Map List of IDs as keys and items implementing \Aimeos\MShop\Locale\Item\Site\Iface |
||
| 496 | */ |
||
| 497 | public function toList() : \Aimeos\Map |
||
| 498 | { |
||
| 499 | return map( [$this->getId() => $this] ); |
||
| 500 | } |
||
| 501 | } |
||
| 502 |