| Total Complexity | 44 |
| 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 Price object |
||
| 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 | array $transactions = [], ?\Aimeos\MShop\Service\Item\Iface $serviceItem = null ) |
||
| 36 | { |
||
| 37 | parent::__construct( $price, $values, $attributes, $transactions ); |
||
| 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 |
||
| 49 | { |
||
| 50 | return $this->serviceItem; |
||
| 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.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\Service\Iface Order base service item for chaining method calls |
||
| 70 | */ |
||
| 71 | public function setSiteId( string $value ) : \Aimeos\MShop\Order\Item\Service\Iface |
||
| 72 | { |
||
| 73 | return $this->set( 'order.service.siteid', $value ); |
||
| 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 getParentId() : ?string |
||
| 83 | { |
||
| 84 | return $this->get( 'order.service.parentid' ); |
||
| 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\Service\Iface Order base service item for chaining method calls |
||
| 93 | */ |
||
| 94 | public function setParentId( ?string $value ) : \Aimeos\MShop\Order\Item\Service\Iface |
||
| 95 | { |
||
| 96 | return $this->set( 'order.service.parentid', $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 |
||
| 106 | { |
||
| 107 | return $this->get( 'order.service.serviceid', '' ); |
||
| 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\Service\Iface Order base service item for chaining method calls |
||
| 116 | */ |
||
| 117 | public function setServiceId( string $servid ) : \Aimeos\MShop\Order\Item\Service\Iface |
||
| 118 | { |
||
| 119 | return $this->set( 'order.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.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\Service\Iface Order base service item for chaining method calls |
||
| 139 | */ |
||
| 140 | public function setCode( string $code ) : \Aimeos\MShop\Order\Item\Service\Iface |
||
| 141 | { |
||
| 142 | return $this->set( 'order.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 |
||
| 152 | { |
||
| 153 | return $this->get( 'order.service.name', '' ); |
||
| 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\Service\Iface Order base service item for chaining method calls |
||
| 162 | */ |
||
| 163 | public function setName( string $name ) : \Aimeos\MShop\Order\Item\Service\Iface |
||
| 164 | { |
||
| 165 | return $this->set( 'order.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.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\Service\Iface Order base service item for chaining method calls |
||
| 185 | */ |
||
| 186 | public function setType( string $type ) : \Aimeos\MShop\Common\Item\Iface |
||
| 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 |
||
| 198 | { |
||
| 199 | return $this->get( 'order.service.mediaurl', '' ); |
||
| 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\Service\Iface Order base service item for chaining method calls |
||
| 208 | */ |
||
| 209 | public function setMediaUrl( string $value ) : \Aimeos\MShop\Order\Item\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 |
||
| 221 | { |
||
| 222 | if( ( $result = $this->get( 'order.service.position' ) ) !== null ) { |
||
| 223 | return $result; |
||
| 224 | } |
||
| 225 | |||
| 226 | return null; |
||
| 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\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\Service\Iface |
||
| 238 | { |
||
| 239 | if( $value < 0 ) { |
||
| 240 | throw new \Aimeos\MShop\Order\Exception( sprintf( 'Order service position "%1$s" must be greater than 0', $value ) ); |
||
| 241 | } |
||
| 242 | |||
| 243 | return $this->set( 'order.service.position', $value ); |
||
| 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\Service\Iface Order service item for chaining method calls |
||
| 253 | */ |
||
| 254 | public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface |
||
| 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 |
||
| 295 | { |
||
| 296 | $price = $this->getPrice(); |
||
| 297 | $list = parent::toArray( $private ); |
||
| 298 | |||
| 299 | $list['order.service.type'] = $this->getType(); |
||
| 300 | $list['order.service.code'] = $this->getCode(); |
||
| 301 | $list['order.service.name'] = $this->getName(); |
||
| 302 | $list['order.service.currencyid'] = $price->getCurrencyId(); |
||
| 303 | $list['order.service.price'] = $price->getValue(); |
||
| 304 | $list['order.service.costs'] = $price->getCosts(); |
||
| 305 | $list['order.service.rebate'] = $price->getRebate(); |
||
| 306 | $list['order.service.taxrates'] = $price->getTaxRates(); |
||
| 307 | $list['order.service.taxvalue'] = $price->getTaxValue(); |
||
| 308 | $list['order.service.taxflag'] = $price->getTaxFlag(); |
||
| 309 | $list['order.service.position'] = $this->getPosition(); |
||
| 310 | $list['order.service.mediaurl'] = $this->getMediaUrl(); |
||
| 311 | $list['order.service.serviceid'] = $this->getServiceId(); |
||
| 312 | |||
| 313 | if( $private === true ) { |
||
| 314 | $list['order.service.parentid'] = $this->getParentId(); |
||
| 315 | } |
||
| 316 | |||
| 317 | return $list; |
||
| 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\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\Service\Iface |
||
| 343 | } |
||
| 344 | } |
||
| 345 |