| Total Complexity | 45 |
| Total Lines | 322 |
| 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 extends Base implements Iface |
||
| 22 | { |
||
| 23 | private $serviceItem; |
||
| 24 | |||
| 25 | |||
| 26 | /** |
||
| 27 | * Initializes the order base service item |
||
| 28 | * |
||
| 29 | * @param \Aimeos\MShop\Price\Item\Iface $price |
||
| 30 | * @param array $values Values to be set on initialisation |
||
| 31 | * @param array $attributes Attributes to be set on initialisation |
||
| 32 | * @param \Aimeos\MShop\Service\Item\Iface|null $serviceItem Service item |
||
| 33 | */ |
||
| 34 | public function __construct( \Aimeos\MShop\Price\Item\Iface $price, array $values = [], array $attributes = [], |
||
| 35 | ?\Aimeos\MShop\Service\Item\Iface $serviceItem = null ) |
||
| 36 | { |
||
| 37 | parent::__construct( $price, $values, $attributes ); |
||
| 38 | |||
| 39 | $this->serviceItem = $serviceItem; |
||
| 40 | } |
||
| 41 | |||
| 42 | |||
| 43 | /** |
||
| 44 | * Returns the associated service item |
||
| 45 | * |
||
| 46 | * @return \Aimeos\MShop\Service\Item\Iface|null Service item |
||
| 47 | */ |
||
| 48 | public function getServiceItem() : ?\Aimeos\MShop\Service\Item\Iface |
||
| 51 | } |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Returns the ID of the site the item is stored |
||
| 56 | * |
||
| 57 | * @return string Site ID (or null if not available) |
||
| 58 | */ |
||
| 59 | public function getSiteId() : string |
||
| 60 | { |
||
| 61 | return $this->get( 'order.base.service.siteid', '' ); |
||
| 62 | } |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Sets the site ID of the item. |
||
| 67 | * |
||
| 68 | * @param string $value Unique site ID of the item |
||
| 69 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base service item for chaining method calls |
||
| 70 | */ |
||
| 71 | public function setSiteId( string $value ) : \Aimeos\MShop\Order\Item\Base\Service\Iface |
||
| 74 | } |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * Returns the order base ID of the order service if available. |
||
| 79 | * |
||
| 80 | * @return string|null Base ID of the item. |
||
| 81 | */ |
||
| 82 | public function getBaseId() : ?string |
||
| 83 | { |
||
| 84 | return $this->get( 'order.base.service.baseid' ); |
||
| 85 | } |
||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * Sets the order service base ID of the order service item. |
||
| 90 | * |
||
| 91 | * @param string|null $value Order service base ID |
||
| 92 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base service item for chaining method calls |
||
| 93 | */ |
||
| 94 | public function setBaseId( ?string $value ) : \Aimeos\MShop\Order\Item\Base\Service\Iface |
||
| 95 | { |
||
| 96 | return $this->set( 'order.base.service.baseid', $value ); |
||
| 97 | } |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns the original ID of the service item used for the order. |
||
| 102 | * |
||
| 103 | * @return string Original service ID |
||
| 104 | */ |
||
| 105 | public function getServiceId() : string |
||
| 108 | } |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * Sets a new ID of the service item used for the order. |
||
| 113 | * |
||
| 114 | * @param string $servid ID of the service item used for the order |
||
| 115 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base service item for chaining method calls |
||
| 116 | */ |
||
| 117 | public function setServiceId( string $servid ) : \Aimeos\MShop\Order\Item\Base\Service\Iface |
||
| 118 | { |
||
| 119 | return $this->set( 'order.base.service.serviceid', $servid ); |
||
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns the code of the service item. |
||
| 125 | * |
||
| 126 | * @return string Service item code |
||
| 127 | */ |
||
| 128 | public function getCode() : string |
||
| 129 | { |
||
| 130 | return $this->get( 'order.base.service.code', '' ); |
||
| 131 | } |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * Sets a new code for the service item. |
||
| 136 | * |
||
| 137 | * @param string $code Code as defined by the service provider |
||
| 138 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base service item for chaining method calls |
||
| 139 | */ |
||
| 140 | public function setCode( string $code ) : \Aimeos\MShop\Order\Item\Base\Service\Iface |
||
| 141 | { |
||
| 142 | return $this->set( 'order.base.service.code', $this->checkCode( $code ) ); |
||
| 143 | } |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns the name of the service item. |
||
| 148 | * |
||
| 149 | * @return string Service item name |
||
| 150 | */ |
||
| 151 | public function getName() : string |
||
| 154 | } |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * Sets a new name for the service item. |
||
| 159 | * |
||
| 160 | * @param string $name service item name |
||
| 161 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base service item for chaining method calls |
||
| 162 | */ |
||
| 163 | public function setName( string $name ) : \Aimeos\MShop\Order\Item\Base\Service\Iface |
||
| 164 | { |
||
| 165 | return $this->set( 'order.base.service.name', $name ); |
||
| 166 | } |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * Returns the type of the service item. |
||
| 171 | * |
||
| 172 | * @return string service item type |
||
| 173 | */ |
||
| 174 | public function getType() : string |
||
| 175 | { |
||
| 176 | return $this->get( 'order.base.service.type', '' ); |
||
| 177 | } |
||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * Sets a new type for the service item. |
||
| 182 | * |
||
| 183 | * @param string $type Type of the service item |
||
| 184 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base service item for chaining method calls |
||
| 185 | */ |
||
| 186 | public function setType( string $type ) : \Aimeos\MShop\Common\Item\Iface |
||
| 187 | { |
||
| 188 | return $this->set( 'order.base.service.type', $this->checkCode( $type ) ); |
||
| 189 | } |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns the location of the media. |
||
| 194 | * |
||
| 195 | * @return string Location of the media |
||
| 196 | */ |
||
| 197 | public function getMediaUrl() : string |
||
| 200 | } |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * Sets the media url of the service item. |
||
| 205 | * |
||
| 206 | * @param string $value Location of the media/picture |
||
| 207 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base service item for chaining method calls |
||
| 208 | */ |
||
| 209 | public function setMediaUrl( string $value ) : \Aimeos\MShop\Order\Item\Base\Service\Iface |
||
| 212 | } |
||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * Returns the position of the service in the order. |
||
| 217 | * |
||
| 218 | * @return int|null Service position in the order from 0-n |
||
| 219 | */ |
||
| 220 | public function getPosition() : ?int |
||
| 227 | } |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * Sets the position of the service within the list of ordered servicees |
||
| 232 | * |
||
| 233 | * @param int|null $value Service position in the order from 0-n or null for resetting the position |
||
| 234 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base service item for chaining method calls |
||
| 235 | * @throws \Aimeos\MShop\Order\Exception If the position is invalid |
||
| 236 | */ |
||
| 237 | public function setPosition( ?int $value ) : \Aimeos\MShop\Order\Item\Base\Service\Iface |
||
| 244 | } |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * Sets the item values from the given array and removes that entries from the list |
||
| 249 | * |
||
| 250 | * @param array &$list Associative list of item keys and their values |
||
| 251 | * @param bool True to set private properties too, false for public only |
||
| 252 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order service item for chaining method calls |
||
| 253 | */ |
||
| 254 | public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface |
||
| 255 | { |
||
| 256 | $price = $this->getPrice(); |
||
| 257 | $item = parent::fromArray( $list, $private ); |
||
| 258 | |||
| 259 | foreach( $list as $key => $value ) |
||
| 260 | { |
||
| 261 | switch( $key ) |
||
| 262 | { |
||
| 263 | case 'order.base.service.siteid': !$private ?: $item = $item->setSiteId( $value ); break; |
||
| 264 | case 'order.base.service.baseid': !$private ?: $item = $item->setBaseId( $value ); break; |
||
| 265 | case 'order.base.service.serviceid': !$private ?: $item = $item->setServiceId( $value ); break; |
||
| 266 | case 'order.base.service.type': $item = $item->setType( $value ); break; |
||
| 267 | case 'order.base.service.code': $item = $item->setCode( $value ); break; |
||
| 268 | case 'order.base.service.name': $item = $item->setName( $value ); break; |
||
| 269 | case 'order.base.service.currencyid': $price = $price->setCurrencyId( $value ); break; |
||
| 270 | case 'order.base.service.price': $price = $price->setValue( $value ); break; |
||
| 271 | case 'order.base.service.costs': $price = $price->setCosts( $value ); break; |
||
| 272 | case 'order.base.service.rebate': $price = $price->setRebate( $value ); break; |
||
| 273 | case 'order.base.service.taxrates': $price = $price->setTaxRates( $value ); break; |
||
| 274 | case 'order.base.service.taxvalue': $price = $price->setTaxValue( $value ); break; |
||
| 275 | case 'order.base.service.taxflag': $price = $price->setTaxFlag( $value ); break; |
||
| 276 | case 'order.base.service.position': $item = $item->setPosition( $value ); break; |
||
| 277 | case 'order.base.service.mediaurl': $item = $item->setMediaUrl( $value ); break; |
||
| 278 | default: continue 2; |
||
| 279 | } |
||
| 280 | |||
| 281 | unset( $list[$key] ); |
||
| 282 | } |
||
| 283 | |||
| 284 | return $item; |
||
| 285 | } |
||
| 286 | |||
| 287 | |||
| 288 | /** |
||
| 289 | * Returns the item values as array. |
||
| 290 | * |
||
| 291 | * @param bool True to return private properties, false for public only |
||
| 292 | * @return array Associative list of item properties and their values. |
||
| 293 | */ |
||
| 294 | public function toArray( bool $private = false ) : array |
||
| 318 | } |
||
| 319 | |||
| 320 | |||
| 321 | /** |
||
| 322 | * Copys all data from a given service item. |
||
| 323 | * |
||
| 324 | * @param \Aimeos\MShop\Service\Item\Iface $service New service item |
||
| 325 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base service item for chaining method calls |
||
| 326 | */ |
||
| 327 | public function copyFrom( \Aimeos\MShop\Service\Item\Iface $service ) : \Aimeos\MShop\Order\Item\Base\Service\Iface |
||
| 345 |