| Total Complexity | 46 |
| Total Lines | 338 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 20 | class Standard extends Base implements Iface |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Clones internal objects of the order product item. |
||
| 24 | */ |
||
| 25 | public function __clone() |
||
| 26 | { |
||
| 27 | $this->set( '.transactions', map( $this->get( '.transactions', [] ) )->clone() ); |
||
| 28 | $this->set( '.attributes', map( $this->get( '.attributes', [] ) )->clone() ); |
||
| 29 | $this->set( '.price', clone $this->get( '.price' ) ); |
||
| 30 | |||
| 31 | parent::__clone(); |
||
| 32 | } |
||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * Returns the price item for the service. |
||
| 37 | * |
||
| 38 | * @return \Aimeos\MShop\Price\Item\Iface Price item with price, costs and rebate |
||
| 39 | */ |
||
| 40 | public function getPrice() : \Aimeos\MShop\Price\Item\Iface |
||
| 41 | { |
||
| 42 | return $this->get( '.price' ); |
||
| 43 | } |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Sets the price item for the service. |
||
| 48 | * |
||
| 49 | * @param \Aimeos\MShop\Price\Item\Iface $price Price item containing price and additional costs |
||
| 50 | * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls |
||
| 51 | */ |
||
| 52 | public function setPrice( \Aimeos\MShop\Price\Item\Iface $price ) : \Aimeos\MShop\Order\Item\Service\Iface |
||
| 53 | { |
||
| 54 | return $this->set( '.price', $price ); |
||
| 55 | } |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Returns the associated service item |
||
| 60 | * |
||
| 61 | * @return \Aimeos\MShop\Service\Item\Iface|null Service item |
||
| 62 | */ |
||
| 63 | public function getServiceItem() : ?\Aimeos\MShop\Service\Item\Iface |
||
| 64 | { |
||
| 65 | return $this->get( '.service' ); |
||
| 66 | } |
||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * Returns the ID of the site the item is stored |
||
| 71 | * |
||
| 72 | * @return string Site ID (or null if not available) |
||
| 73 | */ |
||
| 74 | public function getSiteId() : string |
||
| 75 | { |
||
| 76 | return $this->get( 'order.service.siteid', '' ); |
||
| 77 | } |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * Sets the site ID of the item. |
||
| 82 | * |
||
| 83 | * @param string $value Unique site ID of the item |
||
| 84 | * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls |
||
| 85 | */ |
||
| 86 | public function setSiteId( string $value ) : \Aimeos\MShop\Order\Item\Service\Iface |
||
| 87 | { |
||
| 88 | return $this->set( 'order.service.siteid', $value ); |
||
| 89 | } |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns the order base ID of the order service if available. |
||
| 94 | * |
||
| 95 | * @return string|null Base ID of the item. |
||
| 96 | */ |
||
| 97 | public function getParentId() : ?string |
||
| 98 | { |
||
| 99 | return $this->get( 'order.service.parentid' ); |
||
| 100 | } |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Sets the order service base ID of the order service item. |
||
| 105 | * |
||
| 106 | * @param string|null $value Order service base ID |
||
| 107 | * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls |
||
| 108 | */ |
||
| 109 | public function setParentId( ?string $value ) : \Aimeos\MShop\Order\Item\Service\Iface |
||
| 110 | { |
||
| 111 | return $this->set( 'order.service.parentid', $value ); |
||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * Returns the original ID of the service item used for the order. |
||
| 117 | * |
||
| 118 | * @return string Original service ID |
||
| 119 | */ |
||
| 120 | public function getServiceId() : string |
||
| 121 | { |
||
| 122 | return $this->get( 'order.service.serviceid', '' ); |
||
| 123 | } |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * Sets a new ID of the service item used for the order. |
||
| 128 | * |
||
| 129 | * @param string $servid ID of the service item used for the order |
||
| 130 | * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls |
||
| 131 | */ |
||
| 132 | public function setServiceId( string $servid ) : \Aimeos\MShop\Order\Item\Service\Iface |
||
| 133 | { |
||
| 134 | return $this->set( 'order.service.serviceid', $servid ); |
||
| 135 | } |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns the code of the service item. |
||
| 140 | * |
||
| 141 | * @return string Service item code |
||
| 142 | */ |
||
| 143 | public function getCode() : string |
||
| 144 | { |
||
| 145 | return $this->get( 'order.service.code', '' ); |
||
| 146 | } |
||
| 147 | |||
| 148 | |||
| 149 | /** |
||
| 150 | * Sets a new code for the service item. |
||
| 151 | * |
||
| 152 | * @param string $code Code as defined by the service provider |
||
| 153 | * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls |
||
| 154 | */ |
||
| 155 | public function setCode( string $code ) : \Aimeos\MShop\Order\Item\Service\Iface |
||
| 156 | { |
||
| 157 | return $this->set( 'order.service.code', $this->checkCode( $code ) ); |
||
| 158 | } |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Returns the name of the service item. |
||
| 163 | * |
||
| 164 | * @return string Service item name |
||
| 165 | */ |
||
| 166 | public function getName() : string |
||
| 167 | { |
||
| 168 | return $this->get( 'order.service.name', '' ); |
||
| 169 | } |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Sets a new name for the service item. |
||
| 174 | * |
||
| 175 | * @param string $name service item name |
||
| 176 | * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls |
||
| 177 | */ |
||
| 178 | public function setName( string $name ) : \Aimeos\MShop\Order\Item\Service\Iface |
||
| 179 | { |
||
| 180 | return $this->set( 'order.service.name', $name ); |
||
| 181 | } |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * Returns the type of the service item. |
||
| 186 | * |
||
| 187 | * @return string service item type |
||
| 188 | */ |
||
| 189 | public function getType() : string |
||
| 190 | { |
||
| 191 | return $this->get( 'order.service.type', '' ); |
||
| 192 | } |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * Sets a new type for the service item. |
||
| 197 | * |
||
| 198 | * @param string $type Type of the service item |
||
| 199 | * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls |
||
| 200 | */ |
||
| 201 | public function setType( string $type ) : \Aimeos\MShop\Common\Item\Iface |
||
| 204 | } |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns the location of the media. |
||
| 209 | * |
||
| 210 | * @return string Location of the media |
||
| 211 | */ |
||
| 212 | public function getMediaUrl() : string |
||
| 213 | { |
||
| 214 | return $this->get( 'order.service.mediaurl', '' ); |
||
| 215 | } |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * Sets the media url of the service item. |
||
| 220 | * |
||
| 221 | * @param string $value Location of the media/picture |
||
| 222 | * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls |
||
| 223 | */ |
||
| 224 | public function setMediaUrl( string $value ) : \Aimeos\MShop\Order\Item\Service\Iface |
||
| 227 | } |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * Returns the position of the service in the order. |
||
| 232 | * |
||
| 233 | * @return int|null Service position in the order from 0-n |
||
| 234 | */ |
||
| 235 | public function getPosition() : ?int |
||
| 236 | { |
||
| 237 | return $this->get( 'order.service.position' ); |
||
| 238 | } |
||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * Sets the position of the service within the list of ordered servicees |
||
| 243 | * |
||
| 244 | * @param int|null $value Service position in the order from 0-n or null for resetting the position |
||
| 245 | * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls |
||
| 246 | * @throws \Aimeos\MShop\Order\Exception If the position is invalid |
||
| 247 | */ |
||
| 248 | public function setPosition( ?int $value ) : \Aimeos\MShop\Order\Item\Service\Iface |
||
| 249 | { |
||
| 250 | if( $value < 0 ) { |
||
| 251 | throw new \Aimeos\MShop\Order\Exception( sprintf( 'Order service position "%1$s" must be greater than 0', $value ) ); |
||
| 252 | } |
||
| 253 | |||
| 254 | return $this->set( 'order.service.position', $value ); |
||
| 255 | } |
||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * Sets the item values from the given array and removes that entries from the list |
||
| 260 | * |
||
| 261 | * @param array &$list Associative list of item keys and their values |
||
| 262 | * @param bool True to set private properties too, false for public only |
||
| 263 | * @return \Aimeos\MShop\Order\Item\Service\Iface Order service item for chaining method calls |
||
| 264 | */ |
||
| 265 | public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface |
||
| 296 | } |
||
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * Returns the item values as array. |
||
| 301 | * |
||
| 302 | * @param bool True to return private properties, false for public only |
||
| 303 | * @return array Associative list of item properties and their values. |
||
| 304 | */ |
||
| 305 | public function toArray( bool $private = false ) : array |
||
| 306 | { |
||
| 307 | $price = $this->getPrice(); |
||
| 308 | $list = parent::toArray( $private ); |
||
| 309 | |||
| 310 | $list['order.service.type'] = $this->getType(); |
||
| 311 | $list['order.service.code'] = $this->getCode(); |
||
| 312 | $list['order.service.name'] = $this->getName(); |
||
| 313 | $list['order.service.currencyid'] = $price->getCurrencyId(); |
||
| 314 | $list['order.service.price'] = $price->getValue(); |
||
| 315 | $list['order.service.costs'] = $price->getCosts(); |
||
| 316 | $list['order.service.rebate'] = $price->getRebate(); |
||
| 317 | $list['order.service.taxrates'] = $price->getTaxRates(); |
||
| 318 | $list['order.service.taxvalue'] = $price->getTaxValue(); |
||
| 319 | $list['order.service.taxflag'] = $price->getTaxFlag(); |
||
| 320 | $list['order.service.position'] = $this->getPosition(); |
||
| 321 | $list['order.service.mediaurl'] = $this->getMediaUrl(); |
||
| 322 | $list['order.service.serviceid'] = $this->getServiceId(); |
||
| 323 | |||
| 324 | if( $private === true ) { |
||
| 325 | $list['order.service.parentid'] = $this->getParentId(); |
||
| 326 | } |
||
| 327 | |||
| 328 | return $list; |
||
| 329 | } |
||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * Copys all data from a given service item. |
||
| 334 | * |
||
| 335 | * @param \Aimeos\MShop\Service\Item\Iface $service New service item |
||
| 336 | * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls |
||
| 337 | */ |
||
| 338 | public function copyFrom( \Aimeos\MShop\Service\Item\Iface $service ) : \Aimeos\MShop\Order\Item\Service\Iface |
||
| 358 | } |
||
| 359 | } |
||
| 360 |