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 |
||
13 | class Orders extends MoipResource |
||
14 | { |
||
15 | /** |
||
16 | * @const string |
||
17 | */ |
||
18 | const PATH = 'orders'; |
||
19 | |||
20 | /** |
||
21 | * Defines what kind of payee as pripmary. |
||
22 | * |
||
23 | * @const string |
||
24 | */ |
||
25 | const RECEIVER_TYPE_PRIMARY = 'PRIMARY'; |
||
26 | |||
27 | /** |
||
28 | * Defines what kind of payee as secundary. |
||
29 | * |
||
30 | * @const string |
||
31 | */ |
||
32 | const RECEIVER_TYPE_SECONDARY = 'SECONDARY'; |
||
33 | |||
34 | /** |
||
35 | * Currency used in the application. |
||
36 | * |
||
37 | * @const string |
||
38 | */ |
||
39 | const AMOUNT_CURRENCY = 'BRL'; |
||
40 | |||
41 | /** |
||
42 | * @var \Moip\Resource\Orders |
||
43 | **/ |
||
44 | private $orders; |
||
45 | |||
46 | /** |
||
47 | * Adds a new item to order. |
||
48 | * |
||
49 | * @param string $product Name of the product. |
||
50 | * @param int $quantity Product Quantity. |
||
51 | * @param string $detail Additional product description. |
||
52 | * @param int $price Initial value of the item. |
||
53 | * @param string category Product category. see: https://dev.moip.com.br/v2.1/reference#tabela-de-categorias-de-produtos. |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function addItem($product, $quantity, $detail, $price, $category = 'OTHER_CATEGORIES') |
||
77 | |||
78 | /** |
||
79 | * Adds a new receiver to order. |
||
80 | * |
||
81 | * @param string $moipAccount Id MoIP MoIP account that will receive payment values. |
||
82 | * @param string $type Define qual o tipo de recebedor do pagamento, valores possíveis: PRIMARY, SECONDARY. |
||
83 | * @param int $fixed Value that the receiver will receive. |
||
84 | * @param int $percentual Percentual value that the receiver will receive. Possible values: 0 - 100 |
||
85 | * @param bool $feePayor Flag to know if receiver is the payer of Moip tax. |
||
86 | * |
||
87 | * @return $this |
||
88 | */ |
||
89 | public function addReceiver($moipAccount, $type, $fixed = null, $percentual = null, $feePayor = false) |
||
109 | |||
110 | /** |
||
111 | * Initialize necessary used in some functions. |
||
112 | */ |
||
113 | protected function initialize() |
||
114 | { |
||
115 | $this->data = new stdClass(); |
||
116 | $this->data->ownId = null; |
||
117 | $this->data->amount = new stdClass(); |
||
118 | $this->data->amount->currency = self::AMOUNT_CURRENCY; |
||
119 | $this->data->amount->subtotals = new stdClass(); |
||
120 | $this->data->items = []; |
||
121 | $this->data->receivers = []; |
||
122 | $this->data->checkoutPreferences = new stdClass(); |
||
123 | $this->data->checkoutPreferences->redirectUrls = new stdClass(); |
||
124 | $this->data->checkoutPreferences->installments = []; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Initialize necessary used in some functions. |
||
129 | */ |
||
130 | private function initializeSubtotals() |
||
136 | |||
137 | /** |
||
138 | * Mount the structure of order. |
||
139 | * |
||
140 | * @param \stdClass $response |
||
141 | * |
||
142 | * @return Orders Response order. |
||
143 | */ |
||
144 | protected function populate(stdClass $response) |
||
145 | { |
||
146 | $this->orders = clone $this; |
||
147 | $this->orders->data->id = $response->id; |
||
148 | $this->orders->data->ownId = $response->ownId; |
||
149 | $this->orders->data->createdAt = $response->createdAt; |
||
150 | $this->orders->data->updatedAt = $response->updatedAt; |
||
151 | $this->orders->data->amount->paid = $response->amount->paid; |
||
152 | $this->orders->data->amount->total = $response->amount->total; |
||
153 | $this->orders->data->amount->fees = $response->amount->fees; |
||
154 | $this->orders->data->amount->refunds = $response->amount->refunds; |
||
155 | $this->orders->data->amount->liquid = $response->amount->liquid; |
||
156 | $this->orders->data->amount->otherReceivers = $response->amount->otherReceivers; |
||
157 | $this->orders->data->amount->subtotals = $response->amount->subtotals; |
||
158 | $this->orders->data->items = $response->items; |
||
159 | $customer = new Customer($this->moip); |
||
160 | $this->orders->data->customer = $customer->populate($response->customer); |
||
161 | |||
162 | $this->orders->data->payments = $this->structure($response, Payment::PATH, Payment::class); |
||
163 | $this->orders->data->escrows = $this->structure($response, Payment::PATH, Escrow::class); |
||
164 | $this->orders->data->refunds = $this->structure($response, Refund::PATH, Refund::class); |
||
165 | $this->orders->data->entries = $this->structure($response, Entry::PATH, Entry::class); |
||
166 | $this->orders->data->events = $this->structure($response, Event::PATH, Event::class); |
||
167 | |||
168 | $this->orders->data->receivers = $this->getIfSet('receivers', $response); |
||
169 | |||
170 | $this->orders->data->status = $response->status; |
||
171 | $this->orders->data->_links = $response->_links; |
||
172 | |||
173 | return $this->orders; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Structure resource. |
||
178 | * |
||
179 | * @param stdClass $response |
||
180 | * @param string $resource |
||
181 | * @param \Moip\Resource\Payment|\Moip\Resource\Refund|\Moip\Resource\Entry|\Moip\Resource\Event $class |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | private function structure(stdClass $response, $resource, $class) |
||
198 | |||
199 | /** |
||
200 | * Create a new order in MoIP. |
||
201 | * |
||
202 | * @return \Moip\Resource\Orders|stdClass |
||
203 | */ |
||
204 | public function create() |
||
208 | |||
209 | /** |
||
210 | * Get an order in MoIP. |
||
211 | * |
||
212 | * @param string $id_moip Id MoIP order id |
||
213 | * |
||
214 | * @return stdClass |
||
215 | */ |
||
216 | public function get($id_moip) |
||
220 | |||
221 | /** |
||
222 | * Get MoIP order id. |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | public function getId() |
||
230 | |||
231 | /** |
||
232 | * Get own request id. external reference. |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | public function getOwnId() |
||
240 | |||
241 | /** |
||
242 | * Get paid value of order. |
||
243 | * |
||
244 | * @return int|float |
||
245 | */ |
||
246 | public function getAmountPaid() |
||
247 | { |
||
248 | return $this->getIfSet('paid', $this->data->amount); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Get total value of order. |
||
253 | * |
||
254 | * @return int|float |
||
255 | */ |
||
256 | public function getAmountTotal() |
||
260 | |||
261 | /** |
||
262 | * Get total value of MoIP rate. |
||
263 | * |
||
264 | * @return int|float |
||
265 | */ |
||
266 | public function getAmountFees() |
||
270 | |||
271 | /** |
||
272 | * Get total amount of refunds. |
||
273 | * |
||
274 | * @return int|float |
||
275 | */ |
||
276 | public function getAmountRefunds() |
||
280 | |||
281 | /** |
||
282 | * Get net total value. |
||
283 | * |
||
284 | * @return int|float |
||
285 | */ |
||
286 | public function getAmountLiquid() |
||
290 | |||
291 | /** |
||
292 | * Get sum of amounts received by other recipients. Used in Marketplaces. |
||
293 | * |
||
294 | * @return int|float |
||
295 | */ |
||
296 | public function getAmountOtherReceivers() |
||
300 | |||
301 | /** |
||
302 | * Get currency used in the application. Possible values: BRL. |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | public function getCurrenty() |
||
310 | |||
311 | /** |
||
312 | * Get greight value of the item will be added to the value of the items. |
||
313 | * |
||
314 | * @return int|float |
||
315 | */ |
||
316 | public function getSubtotalShipping() |
||
322 | |||
323 | /** |
||
324 | * Get Additional value to the item will be added to the value of the items. |
||
325 | * |
||
326 | * @return int|float |
||
327 | */ |
||
328 | public function getSubtotalAddition() |
||
334 | |||
335 | /** |
||
336 | * Get discounted value of the item will be subtracted from the total value of the items. |
||
337 | * |
||
338 | * @return int|float |
||
339 | */ |
||
340 | public function getSubtotalDiscount() |
||
346 | |||
347 | /** |
||
348 | * Get summing the values of all items. |
||
349 | * |
||
350 | * @return int|float |
||
351 | */ |
||
352 | public function getSubtotalItems() |
||
358 | |||
359 | /** |
||
360 | * Ger structure item information request. |
||
361 | * |
||
362 | * @return \ArrayIterator |
||
363 | */ |
||
364 | public function getItemIterator() |
||
368 | |||
369 | /** |
||
370 | * Get Customer associated with the request. |
||
371 | * |
||
372 | * @return \Moip\Resource\Customer |
||
373 | */ |
||
374 | public function getCustomer() |
||
378 | |||
379 | /** |
||
380 | * Get zipCode of shippingAddress. |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | public function getShippingAddressZipCode() |
||
385 | { |
||
386 | return $this->getIfSet('zipCode', $this->data->shippingAddress); |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * Get street of shippingAddress. |
||
391 | * |
||
392 | * @return string |
||
393 | */ |
||
394 | public function getShippingAddressStreet() |
||
395 | { |
||
396 | return $this->getIfSet('street', $this->data->shippingAddress); |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Get streetNumber of shippingAddress. |
||
401 | * |
||
402 | * @return string |
||
403 | */ |
||
404 | public function getShippingAddressStreetNumber() |
||
405 | { |
||
406 | return $this->getIfSet('streetNumber', $this->data->shippingAddress); |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * Get complement of shippingAddress. |
||
411 | * |
||
412 | * @return string |
||
413 | */ |
||
414 | public function getShippingAddressComplement() |
||
415 | { |
||
416 | return $this->getIfSet('complement', $this->data->shippingAddress); |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * Get city of shippingAddress. |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | public function getShippingAddressCity() |
||
425 | { |
||
426 | return $this->getIfSet('city', $this->data->shippingAddress); |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Get district of shippingAddress. |
||
431 | * |
||
432 | * @return string |
||
433 | */ |
||
434 | public function getShippingAddressDistrict() |
||
435 | { |
||
436 | return $this->getIfSet('district', $this->data->shippingAddress); |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Get state of shippingAddress. |
||
441 | * |
||
442 | * @return string |
||
443 | */ |
||
444 | public function getShippingAddressState() |
||
445 | { |
||
446 | return $this->getIfSet('state', $this->data->shippingAddress); |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Get country of shippingAddress. |
||
451 | * |
||
452 | * @return string |
||
453 | */ |
||
454 | public function getShippingAddressCountry() |
||
455 | { |
||
456 | return $this->getIfSet('country', $this->data->shippingAddress); |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * Get payments associated with the request. |
||
461 | * |
||
462 | * @return ArrayIterator |
||
463 | */ |
||
464 | public function getPaymentIterator() |
||
468 | |||
469 | /** |
||
470 | * Get escrows associated with the request. |
||
471 | * |
||
472 | * @return ArrayIterator |
||
473 | */ |
||
474 | public function getEscrowIterator() |
||
475 | { |
||
476 | return new ArrayIterator($this->data->escrows); |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Get refunds associated with the request. |
||
481 | * |
||
482 | * @return ArrayIterator |
||
483 | */ |
||
484 | public function getRefundIterator() |
||
485 | { |
||
486 | return new ArrayIterator($this->data->refunds); |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * Get entries associated with the request. |
||
491 | * |
||
492 | * @return ArrayIterator |
||
493 | */ |
||
494 | public function getEntryIterator() |
||
498 | |||
499 | /** |
||
500 | * Get releases associated with the request. |
||
501 | * |
||
502 | * @return ArrayIterator |
||
503 | */ |
||
504 | public function getEventIterator() |
||
508 | |||
509 | /** |
||
510 | * Get recipient structure of payments. |
||
511 | * |
||
512 | * @return ArrayIterator |
||
513 | */ |
||
514 | public function getReceiverIterator() |
||
518 | |||
519 | /** |
||
520 | * Get order status. |
||
521 | * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED. |
||
522 | * |
||
523 | * @return string |
||
524 | */ |
||
525 | public function getStatus() |
||
529 | |||
530 | /** |
||
531 | * Get date of resource creation. |
||
532 | * |
||
533 | * @return \DateTime |
||
534 | */ |
||
535 | public function getCreatedAt() |
||
539 | |||
540 | /** |
||
541 | * Get updated resource. |
||
542 | * |
||
543 | * @return \DateTime |
||
544 | */ |
||
545 | public function getUpdatedAt() |
||
549 | |||
550 | /** |
||
551 | * Get checkout preferences of the order. |
||
552 | * |
||
553 | * @return string |
||
554 | */ |
||
555 | public function getCheckoutPreferences() |
||
559 | |||
560 | /** |
||
561 | * Create a new Orders list instance. |
||
562 | * |
||
563 | * @return \Moip\Resource\OrdersList |
||
564 | */ |
||
565 | public function getList(Pagination $pagination = null, Filters $filters = null, $qParam = '') |
||
571 | |||
572 | /** |
||
573 | * Structure of payment. |
||
574 | * |
||
575 | * @return \Moip\Resource\Payment |
||
576 | */ |
||
577 | public function payments() |
||
584 | |||
585 | /** |
||
586 | * Structure of refund. |
||
587 | * |
||
588 | * @return \Moip\Resource\Refund |
||
589 | */ |
||
590 | public function refunds() |
||
597 | |||
598 | /** |
||
599 | * Set additional value to the item will be added to the value of the items. |
||
600 | * |
||
601 | * @param int|float $value additional value to the item. |
||
602 | * |
||
603 | * @return $this |
||
604 | */ |
||
605 | public function setAddition($value) |
||
614 | |||
615 | /** |
||
616 | * Set customer associated with the order. |
||
617 | * |
||
618 | * @param \Moip\Resource\Customer $customer customer associated with the request. |
||
619 | * |
||
620 | * @return $this |
||
621 | */ |
||
622 | public function setCustomer(Customer $customer) |
||
628 | |||
629 | /** |
||
630 | * Set customer id associated with the order. |
||
631 | * |
||
632 | * @param string $id Customer's id. |
||
633 | * |
||
634 | * @return $this |
||
635 | */ |
||
636 | public function setCustomerId($id) |
||
645 | |||
646 | /** |
||
647 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
648 | * |
||
649 | * @param int|float $value discounted value. |
||
650 | * |
||
651 | * @return $this |
||
652 | */ |
||
653 | public function setDiscount($value) |
||
659 | |||
660 | /** |
||
661 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
662 | * |
||
663 | * @deprecated |
||
664 | * |
||
665 | * @param int|float $value discounted value. |
||
666 | * |
||
667 | * @return $this |
||
668 | */ |
||
669 | public function setDiscont($value) |
||
675 | |||
676 | /** |
||
677 | * Set own request id. external reference. |
||
678 | * |
||
679 | * @param string $ownId external reference. |
||
680 | * |
||
681 | * @return $this |
||
682 | */ |
||
683 | public function setOwnId($ownId) |
||
689 | |||
690 | /** |
||
691 | * Set shipping Amount. |
||
692 | * |
||
693 | * @param float $value shipping Amount. |
||
694 | * |
||
695 | * @return $this |
||
696 | */ |
||
697 | public function setShippingAmount($value) |
||
703 | |||
704 | /** |
||
705 | * Set URL for redirection in case of success. |
||
706 | * |
||
707 | * @param string $urlSuccess UrlSuccess. |
||
708 | * |
||
709 | * @return $this |
||
710 | */ |
||
711 | public function setUrlSuccess($urlSuccess = '') |
||
717 | |||
718 | /** |
||
719 | * Set URL for redirection in case of failure. |
||
720 | * |
||
721 | * @param string $urlFailure UrlFailure. |
||
722 | * |
||
723 | * @return $this |
||
724 | */ |
||
725 | public function setUrlFailure($urlFailure = '') |
||
731 | |||
732 | /** |
||
733 | * Set installment settings for checkout preferences. |
||
734 | * |
||
735 | * @param array $quantity |
||
736 | * @param int $discountValue |
||
737 | * @param int $additionalValue |
||
738 | * |
||
739 | * @return $this |
||
740 | */ |
||
741 | public function addInstallmentCheckoutPreferences($quantity, $discountValue = 0, $additionalValue = 0) |
||
752 | } |
||
753 |