Complex classes like Orders 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Orders, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Orders extends MoipResource { |
||
| 9 | /** |
||
| 10 | * @const string |
||
| 11 | */ |
||
| 12 | const PATH = 'orders'; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Defines what kind of payee as pripmary. |
||
| 16 | * |
||
| 17 | * @const string |
||
| 18 | */ |
||
| 19 | const RECEIVER_TYPE_PRIMARY = 'PRIMARY'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Defines what kind of payee as secundary. |
||
| 23 | * |
||
| 24 | * @const string |
||
| 25 | */ |
||
| 26 | const RECEIVER_TYPE_SECONDARY = 'SECONDARY'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Currency used in the application. |
||
| 30 | * |
||
| 31 | * @const string |
||
| 32 | */ |
||
| 33 | const AMOUNT_CURRENCY = 'BRL'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var \Moip\Resource\Orders |
||
| 37 | **/ |
||
| 38 | private $orders; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Adds a new item to order. |
||
| 42 | * |
||
| 43 | * @param string $product Name of the product. |
||
| 44 | * @param integer $quantity Product Quantity. |
||
| 45 | * @param string $detail Additional product description. |
||
| 46 | * @param integer $price Initial value of the item. |
||
| 47 | * |
||
| 48 | * @return $this |
||
| 49 | */ |
||
| 50 | public function addItem($product, $quantity, $detail, $price) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Adds a new receiver to order. |
||
| 64 | * |
||
| 65 | * @param string $moipAccount Id MoIP MoIP account that will receive payment values. |
||
| 66 | * @param string $type Define qual o tipo de recebedor do pagamento, valores possíveis: PRIMARY, SECONDARY. |
||
| 67 | * |
||
| 68 | * @return $this |
||
| 69 | */ |
||
| 70 | public function addReceiver($moipAccount, $type = self::RECEIVER_TYPE_PRIMARY) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Initialize necessary used in some functions. |
||
| 83 | */ |
||
| 84 | protected function initialize() { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Initialize necessary used in some functions. |
||
| 96 | */ |
||
| 97 | private function initializeSubtotals() { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Mount the structure of order. |
||
| 105 | * |
||
| 106 | * @param \stdClass $response |
||
| 107 | * |
||
| 108 | * @return Orders Response order. |
||
| 109 | */ |
||
| 110 | protected function populate(stdClass $response) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Structure resource. |
||
| 139 | * |
||
| 140 | * @param stdClass $response |
||
| 141 | * @param string $resource |
||
| 142 | * @param \Moip\Resource\Payment|\Moip\Resource\Refund|\Moip\Resource\Entry|\Moip\Resource\Event $class |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | private function structure(stdClass $response, $resource, $class) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Create a new order in MoIP. |
||
| 161 | * |
||
| 162 | * @return stdClass |
||
| 163 | */ |
||
| 164 | public function create() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get an order in MoIP. |
||
| 170 | * |
||
| 171 | * @param string $id Id MoIP order id |
||
| 172 | * |
||
| 173 | * @return stdClass |
||
| 174 | */ |
||
| 175 | public function get($id) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get MoIP order id. |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function getId() { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get own request id. external reference. |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getOwnId() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get total value of order. |
||
| 199 | * |
||
| 200 | * @return int|float |
||
| 201 | */ |
||
| 202 | public function getAmountTotal() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get total value of MoIP rate. |
||
| 208 | * |
||
| 209 | * @return int|float |
||
| 210 | */ |
||
| 211 | public function getAmountFees() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get total amount of refunds. |
||
| 217 | * |
||
| 218 | * @return int|float |
||
| 219 | */ |
||
| 220 | public function getAmountRefunds() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get net total value. |
||
| 226 | * |
||
| 227 | * @return int|float |
||
| 228 | */ |
||
| 229 | public function getAmountLiquid() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get sum of amounts received by other recipients. Used in Marketplaces. |
||
| 235 | * |
||
| 236 | * @return int|float |
||
| 237 | */ |
||
| 238 | public function getAmountOtherReceivers() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get currency used in the application. Possible values: BRL. |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getCurrenty() { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get greight value of the item will be added to the value of the items. |
||
| 253 | * |
||
| 254 | * @return int|float |
||
| 255 | */ |
||
| 256 | public function getSubtotalShipping() { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get Additional value to the item will be added to the value of the items. |
||
| 264 | * |
||
| 265 | * @return int|float |
||
| 266 | */ |
||
| 267 | public function getSubtotalAddition() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get discounted value of the item will be subtracted from the total value of the items. |
||
| 275 | * |
||
| 276 | * @return int|float |
||
| 277 | */ |
||
| 278 | public function getSubtotalDiscount() { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get summing the values of all items. |
||
| 286 | * |
||
| 287 | * @return int|float |
||
| 288 | */ |
||
| 289 | public function getSubtotalItems() { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Ger structure item information request. |
||
| 297 | * |
||
| 298 | * @return \ArrayIterator |
||
| 299 | */ |
||
| 300 | public function getItemIterator() { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get Customer associated with the request. |
||
| 306 | * |
||
| 307 | * @return \Moip\Resource\Customer |
||
| 308 | */ |
||
| 309 | public function getCustomer() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get payments associated with the request. |
||
| 315 | * |
||
| 316 | * @return ArrayIterator |
||
| 317 | */ |
||
| 318 | public function getPaymentIterator() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get recipient structure of payments. |
||
| 324 | * |
||
| 325 | * @return ArrayIterator |
||
| 326 | */ |
||
| 327 | public function getReceiverIterator() { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get releases associated with the request. |
||
| 333 | * |
||
| 334 | * @return ArrayIterator |
||
| 335 | */ |
||
| 336 | public function getEventIterator() { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get repayments associated with the request. |
||
| 342 | * |
||
| 343 | * @return ArrayIterator |
||
| 344 | */ |
||
| 345 | public function getRefundIterator() { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get order status. |
||
| 351 | * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED. |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function getStatus() { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Get date of resource creation. |
||
| 361 | * |
||
| 362 | * @return \DateTime |
||
| 363 | */ |
||
| 364 | public function getCreatedAt() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Get updated resource. |
||
| 370 | * |
||
| 371 | * @return \DateTime |
||
| 372 | */ |
||
| 373 | public function getUpdatedAt() { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get hypermedia link structure (HATEOAS) resource Orders. |
||
| 379 | * |
||
| 380 | * @return \stdClass |
||
| 381 | */ |
||
| 382 | public function getLinks() { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Structure of payment. |
||
| 388 | * |
||
| 389 | * @return \Moip\Resource\Payment |
||
| 390 | */ |
||
| 391 | public function payments() { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Structure of refund. |
||
| 400 | * |
||
| 401 | * @return \Moip\Resource\Refund |
||
| 402 | */ |
||
| 403 | public function refunds() { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Set additional value to the item will be added to the value of the items. |
||
| 412 | * |
||
| 413 | * @param int|float $value additional value to the item. |
||
| 414 | * @return $this |
||
| 415 | */ |
||
| 416 | public function setAddition($value) { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Set customer associated with the order. |
||
| 424 | * |
||
| 425 | * @param \Moip\Resource\Customer $customer customer associated with the request. |
||
| 426 | * @return $this |
||
| 427 | */ |
||
| 428 | public function setCustomer(Customer $customer) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
| 436 | * @param int|float $value discounted value. |
||
| 437 | * @return $this |
||
| 438 | */ |
||
| 439 | public function setDiscount($value) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
| 450 | * @deprecated |
||
| 451 | * @param int|float $value discounted value. |
||
| 452 | * @return $this |
||
| 453 | */ |
||
| 454 | public function setDiscont($value) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Set own request id. external reference. |
||
| 462 | * |
||
| 463 | * @param string $ownId external reference. |
||
| 464 | * @return $this |
||
| 465 | */ |
||
| 466 | public function setOwnId($ownId) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Set shipping Amount. |
||
| 474 | * |
||
| 475 | * @param float $value shipping Amount. |
||
| 476 | * |
||
| 477 | * @return $this |
||
| 478 | */ |
||
| 479 | public function setShippingAmount($value) { |
||
| 484 | } |
||
| 485 |