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() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Initialize necessary used in some functions. |
||
| 125 | */ |
||
| 126 | private function initializeSubtotals() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Mount the structure of order. |
||
| 135 | * |
||
| 136 | * @param \stdClass $response |
||
| 137 | * |
||
| 138 | * @return Orders Response order. |
||
| 139 | */ |
||
| 140 | protected function populate(stdClass $response) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Structure resource. |
||
| 171 | * |
||
| 172 | * @param stdClass $response |
||
| 173 | * @param string $resource |
||
| 174 | * @param \Moip\Resource\Payment|\Moip\Resource\Refund|\Moip\Resource\Entry|\Moip\Resource\Event $class |
||
| 175 | * |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | private function structure(stdClass $response, $resource, $class) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Create a new order in MoIP. |
||
| 194 | * |
||
| 195 | * @return \Moip\Resource\Orders|stdClass |
||
| 196 | */ |
||
| 197 | public function create() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get an order in MoIP. |
||
| 204 | * |
||
| 205 | * @param string $id_moip Id MoIP order id |
||
| 206 | * |
||
| 207 | * @return stdClass |
||
| 208 | */ |
||
| 209 | public function get($id_moip) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get MoIP order id. |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public function getId() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get own request id. external reference. |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function getOwnId() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get total value of order. |
||
| 236 | * |
||
| 237 | * @return int|float |
||
| 238 | */ |
||
| 239 | public function getAmountTotal() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get total value of MoIP rate. |
||
| 246 | * |
||
| 247 | * @return int|float |
||
| 248 | */ |
||
| 249 | public function getAmountFees() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get total amount of refunds. |
||
| 256 | * |
||
| 257 | * @return int|float |
||
| 258 | */ |
||
| 259 | public function getAmountRefunds() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get net total value. |
||
| 266 | * |
||
| 267 | * @return int|float |
||
| 268 | */ |
||
| 269 | public function getAmountLiquid() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get sum of amounts received by other recipients. Used in Marketplaces. |
||
| 276 | * |
||
| 277 | * @return int|float |
||
| 278 | */ |
||
| 279 | public function getAmountOtherReceivers() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get currency used in the application. Possible values: BRL. |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function getCurrenty() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get greight value of the item will be added to the value of the items. |
||
| 296 | * |
||
| 297 | * @return int|float |
||
| 298 | */ |
||
| 299 | public function getSubtotalShipping() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get Additional value to the item will be added to the value of the items. |
||
| 308 | * |
||
| 309 | * @return int|float |
||
| 310 | */ |
||
| 311 | public function getSubtotalAddition() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get discounted value of the item will be subtracted from the total value of the items. |
||
| 320 | * |
||
| 321 | * @return int|float |
||
| 322 | */ |
||
| 323 | public function getSubtotalDiscount() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get summing the values of all items. |
||
| 332 | * |
||
| 333 | * @return int|float |
||
| 334 | */ |
||
| 335 | public function getSubtotalItems() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Ger structure item information request. |
||
| 344 | * |
||
| 345 | * @return \ArrayIterator |
||
| 346 | */ |
||
| 347 | public function getItemIterator() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get Customer associated with the request. |
||
| 354 | * |
||
| 355 | * @return \Moip\Resource\Customer |
||
| 356 | */ |
||
| 357 | public function getCustomer() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Get payments associated with the request. |
||
| 364 | * |
||
| 365 | * @return ArrayIterator |
||
| 366 | */ |
||
| 367 | public function getPaymentIterator() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get recipient structure of payments. |
||
| 374 | * |
||
| 375 | * @return ArrayIterator |
||
| 376 | */ |
||
| 377 | public function getReceiverIterator() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get releases associated with the request. |
||
| 384 | * |
||
| 385 | * @return ArrayIterator |
||
| 386 | */ |
||
| 387 | public function getEventIterator() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get repayments associated with the request. |
||
| 394 | * |
||
| 395 | * @return ArrayIterator |
||
| 396 | */ |
||
| 397 | public function getRefundIterator() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get order status. |
||
| 404 | * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED. |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public function getStatus() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Get date of resource creation. |
||
| 415 | * |
||
| 416 | * @return \DateTime |
||
| 417 | */ |
||
| 418 | public function getCreatedAt() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Get updated resource. |
||
| 425 | * |
||
| 426 | * @return \DateTime |
||
| 427 | */ |
||
| 428 | public function getUpdatedAt() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Get checkout preferences of the order. |
||
| 435 | * |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | public function getCheckoutPreferences() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Structure of payment. |
||
| 445 | * |
||
| 446 | * @return \Moip\Resource\Payment |
||
| 447 | */ |
||
| 448 | public function payments() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Structure of refund. |
||
| 458 | * |
||
| 459 | * @return \Moip\Resource\Refund |
||
| 460 | */ |
||
| 461 | public function refunds() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Set additional value to the item will be added to the value of the items. |
||
| 471 | * |
||
| 472 | * @param int|float $value additional value to the item. |
||
| 473 | * |
||
| 474 | * @return $this |
||
| 475 | */ |
||
| 476 | public function setAddition($value) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Set customer associated with the order. |
||
| 488 | * |
||
| 489 | * @param \Moip\Resource\Customer $customer customer associated with the request. |
||
| 490 | * |
||
| 491 | * @return $this |
||
| 492 | */ |
||
| 493 | public function setCustomer(Customer $customer) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Set customer id associated with the order. |
||
| 502 | * |
||
| 503 | * @param string $id Customer's id. |
||
| 504 | * |
||
| 505 | * @return $this |
||
| 506 | */ |
||
| 507 | public function setCustomerId($id) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
| 519 | * |
||
| 520 | * @param int|float $value discounted value. |
||
| 521 | * |
||
| 522 | * @return $this |
||
| 523 | */ |
||
| 524 | public function setDiscount($value) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
| 533 | * |
||
| 534 | * @deprecated |
||
| 535 | * |
||
| 536 | * @param int|float $value discounted value. |
||
| 537 | * |
||
| 538 | * @return $this |
||
| 539 | */ |
||
| 540 | public function setDiscont($value) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Set own request id. external reference. |
||
| 549 | * |
||
| 550 | * @param string $ownId external reference. |
||
| 551 | * |
||
| 552 | * @return $this |
||
| 553 | */ |
||
| 554 | public function setOwnId($ownId) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Set shipping Amount. |
||
| 563 | * |
||
| 564 | * @param float $value shipping Amount. |
||
| 565 | * |
||
| 566 | * @return $this |
||
| 567 | */ |
||
| 568 | public function setShippingAmount($value) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Set URL for redirection in case of success. |
||
| 577 | * |
||
| 578 | * @param string $urlSuccess UrlSuccess. |
||
| 579 | * |
||
| 580 | * @return $this |
||
| 581 | */ |
||
| 582 | public function setUrlSuccess($urlSuccess = '') |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Set URL for redirection in case of failure. |
||
| 591 | * |
||
| 592 | * @param string $urlFailure UrlFailure. |
||
| 593 | * |
||
| 594 | * @return $this |
||
| 595 | */ |
||
| 596 | public function setUrlFailure($urlFailure = '') |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Set installment settings for checkout preferences |
||
| 605 | * |
||
| 606 | * @param array $quantity |
||
| 607 | * @param int $discountValue |
||
| 608 | * @param int $additionalValue |
||
| 609 | * |
||
| 610 | * @return $this |
||
| 611 | */ |
||
| 612 | public function setInstallmentCheckoutPreferences($quantity, $discountValue = 0, $additionalValue = 0) |
||
| 621 | } |
||
| 622 |