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 |
||
| 11 | class Orders extends MoipResource |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @const string |
||
| 15 | */ |
||
| 16 | const PATH = 'orders'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Defines what kind of payee as pripmary. |
||
| 20 | * |
||
| 21 | * @const string |
||
| 22 | */ |
||
| 23 | const RECEIVER_TYPE_PRIMARY = 'PRIMARY'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Defines what kind of payee as secundary. |
||
| 27 | * |
||
| 28 | * @const string |
||
| 29 | */ |
||
| 30 | const RECEIVER_TYPE_SECONDARY = 'SECONDARY'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Currency used in the application. |
||
| 34 | * |
||
| 35 | * @const string |
||
| 36 | */ |
||
| 37 | const AMOUNT_CURRENCY = 'BRL'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var \Moip\Resource\Orders |
||
| 41 | **/ |
||
| 42 | private $orders; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Adds a new item to order. |
||
| 46 | * |
||
| 47 | * @param string $product Name of the product. |
||
| 48 | * @param int $quantity Product Quantity. |
||
| 49 | * @param string $detail Additional product description. |
||
| 50 | * @param int $price Initial value of the item. |
||
| 51 | * |
||
| 52 | * @return $this |
||
| 53 | */ |
||
| 54 | public function addItem($product, $quantity, $detail, $price) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Adds a new receiver to order. |
||
| 77 | * |
||
| 78 | * @param string $moipAccount Id MoIP MoIP account that will receive payment values. |
||
| 79 | * @param string $type Define qual o tipo de recebedor do pagamento, valores possíveis: PRIMARY, SECONDARY. |
||
| 80 | * @param int $fixed Value that the receiver will receive. |
||
| 81 | * @param int $percentual Percentual value that the receiver will receive. Possible values: 0 - 100 |
||
| 82 | * @param bool $feePayor Flag to know if receiver is the payer of Moip tax. |
||
| 83 | * |
||
| 84 | * @return $this |
||
| 85 | */ |
||
| 86 | public function addReceiver($moipAccount, $type, $fixed = null, $percentual = null, $feePayor = false) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Initialize necessary used in some functions. |
||
| 109 | */ |
||
| 110 | protected function initialize() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Initialize necessary used in some functions. |
||
| 126 | */ |
||
| 127 | private function initializeSubtotals() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Mount the structure of order. |
||
| 136 | * |
||
| 137 | * @param \stdClass $response |
||
| 138 | * |
||
| 139 | * @return Orders Response order. |
||
| 140 | */ |
||
| 141 | protected function populate(stdClass $response) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Structure resource. |
||
| 172 | * |
||
| 173 | * @param stdClass $response |
||
| 174 | * @param string $resource |
||
| 175 | * @param \Moip\Resource\Payment|\Moip\Resource\Refund|\Moip\Resource\Entry|\Moip\Resource\Event $class |
||
| 176 | * |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | private function structure(stdClass $response, $resource, $class) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Create a new order in MoIP. |
||
| 195 | * |
||
| 196 | * @return \Moip\Resource\Orders|stdClass |
||
| 197 | */ |
||
| 198 | public function create() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get an order in MoIP. |
||
| 205 | * |
||
| 206 | * @param string $id_moip Id MoIP order id |
||
| 207 | * |
||
| 208 | * @return stdClass |
||
| 209 | */ |
||
| 210 | public function get($id_moip) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get MoIP order id. |
||
| 217 | * |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | public function getId() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get own request id. external reference. |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function getOwnId() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get total value of order. |
||
| 237 | * |
||
| 238 | * @return int|float |
||
| 239 | */ |
||
| 240 | public function getAmountTotal() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get total value of MoIP rate. |
||
| 247 | * |
||
| 248 | * @return int|float |
||
| 249 | */ |
||
| 250 | public function getAmountFees() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get total amount of refunds. |
||
| 257 | * |
||
| 258 | * @return int|float |
||
| 259 | */ |
||
| 260 | public function getAmountRefunds() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get net total value. |
||
| 267 | * |
||
| 268 | * @return int|float |
||
| 269 | */ |
||
| 270 | public function getAmountLiquid() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get sum of amounts received by other recipients. Used in Marketplaces. |
||
| 277 | * |
||
| 278 | * @return int|float |
||
| 279 | */ |
||
| 280 | public function getAmountOtherReceivers() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get currency used in the application. Possible values: BRL. |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | public function getCurrenty() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get greight value of the item will be added to the value of the items. |
||
| 297 | * |
||
| 298 | * @return int|float |
||
| 299 | */ |
||
| 300 | public function getSubtotalShipping() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Get Additional value to the item will be added to the value of the items. |
||
| 309 | * |
||
| 310 | * @return int|float |
||
| 311 | */ |
||
| 312 | public function getSubtotalAddition() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Get discounted value of the item will be subtracted from the total value of the items. |
||
| 321 | * |
||
| 322 | * @return int|float |
||
| 323 | */ |
||
| 324 | public function getSubtotalDiscount() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get summing the values of all items. |
||
| 333 | * |
||
| 334 | * @return int|float |
||
| 335 | */ |
||
| 336 | public function getSubtotalItems() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Ger structure item information request. |
||
| 345 | * |
||
| 346 | * @return \ArrayIterator |
||
| 347 | */ |
||
| 348 | public function getItemIterator() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get Customer associated with the request. |
||
| 355 | * |
||
| 356 | * @return \Moip\Resource\Customer |
||
| 357 | */ |
||
| 358 | public function getCustomer() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get payments associated with the request. |
||
| 365 | * |
||
| 366 | * @return ArrayIterator |
||
| 367 | */ |
||
| 368 | public function getPaymentIterator() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get recipient structure of payments. |
||
| 375 | * |
||
| 376 | * @return ArrayIterator |
||
| 377 | */ |
||
| 378 | public function getReceiverIterator() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get releases associated with the request. |
||
| 385 | * |
||
| 386 | * @return ArrayIterator |
||
| 387 | */ |
||
| 388 | public function getEventIterator() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Get repayments associated with the request. |
||
| 395 | * |
||
| 396 | * @return ArrayIterator |
||
| 397 | */ |
||
| 398 | public function getRefundIterator() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Get order status. |
||
| 405 | * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED. |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | public function getStatus() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get date of resource creation. |
||
| 416 | * |
||
| 417 | * @return \DateTime |
||
| 418 | */ |
||
| 419 | public function getCreatedAt() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Get updated resource. |
||
| 426 | * |
||
| 427 | * @return \DateTime |
||
| 428 | */ |
||
| 429 | public function getUpdatedAt() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get checkout preferences of the order. |
||
| 436 | * |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | public function getCheckoutPreferences() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Structure of payment. |
||
| 446 | * |
||
| 447 | * @return \Moip\Resource\Payment |
||
| 448 | */ |
||
| 449 | public function payments() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Structure of refund. |
||
| 459 | * |
||
| 460 | * @return \Moip\Resource\Refund |
||
| 461 | */ |
||
| 462 | public function refunds() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Set additional value to the item will be added to the value of the items. |
||
| 472 | * |
||
| 473 | * @param int|float $value additional value to the item. |
||
| 474 | * |
||
| 475 | * @return $this |
||
| 476 | */ |
||
| 477 | public function setAddition($value) |
||
| 478 | { |
||
| 479 | if (!isset($this->data->amount->subtotals)) { |
||
| 480 | $this->data->amount->subtotals = new stdClass(); |
||
| 481 | } |
||
| 482 | $this->data->amount->subtotals->addition = (float) $value; |
||
| 483 | |||
| 484 | return $this; |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Set customer associated with the order. |
||
| 489 | * |
||
| 490 | * @param \Moip\Resource\Customer $customer customer associated with the request. |
||
| 491 | * |
||
| 492 | * @return $this |
||
| 493 | */ |
||
| 494 | public function setCustomer(Customer $customer) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Set customer id associated with the order. |
||
| 503 | * |
||
| 504 | * @param string $id Customer's id. |
||
| 505 | * |
||
| 506 | * @return $this |
||
| 507 | */ |
||
| 508 | public function setCustomerId($id) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
| 520 | * |
||
| 521 | * @param int|float $value discounted value. |
||
| 522 | * |
||
| 523 | * @return $this |
||
| 524 | */ |
||
| 525 | public function setDiscount($value) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
| 534 | * |
||
| 535 | * @deprecated |
||
| 536 | * |
||
| 537 | * @param int|float $value discounted value. |
||
| 538 | * |
||
| 539 | * @return $this |
||
| 540 | */ |
||
| 541 | public function setDiscont($value) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Set own request id. external reference. |
||
| 550 | * |
||
| 551 | * @param string $ownId external reference. |
||
| 552 | * |
||
| 553 | * @return $this |
||
| 554 | */ |
||
| 555 | public function setOwnId($ownId) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Set shipping Amount. |
||
| 564 | * |
||
| 565 | * @param float $value shipping Amount. |
||
| 566 | * |
||
| 567 | * @return $this |
||
| 568 | */ |
||
| 569 | public function setShippingAmount($value) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Set URL for redirection in case of success. |
||
| 578 | * |
||
| 579 | * @param string $urlSuccess UrlSuccess. |
||
| 580 | * |
||
| 581 | * @return $this |
||
| 582 | */ |
||
| 583 | public function setUrlSuccess($urlSuccess = '') |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Set URL for redirection in case of failure. |
||
| 592 | * |
||
| 593 | * @param string $urlFailure UrlFailure. |
||
| 594 | * |
||
| 595 | * @return $this |
||
| 596 | */ |
||
| 597 | public function setUrlFailure($urlFailure = '') |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Set installment settings for checkout preferences. |
||
| 606 | * |
||
| 607 | * @param array $quantity |
||
| 608 | * @param int $discountValue |
||
| 609 | * @param int $additionalValue |
||
| 610 | * |
||
| 611 | * @return $this |
||
| 612 | */ |
||
| 613 | public function addInstallmentCheckoutPreferences($quantity, $discountValue = 0, $additionalValue = 0) |
||
| 624 | } |
||
| 625 |