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 | * |
||
54 | * @return $this |
||
55 | */ |
||
56 | public function addItem($product, $quantity, $detail, $price) |
||
76 | |||
77 | /** |
||
78 | * Adds a new receiver to order. |
||
79 | * |
||
80 | * @param string $moipAccount Id MoIP MoIP account that will receive payment values. |
||
81 | * @param string $type Define qual o tipo de recebedor do pagamento, valores possíveis: PRIMARY, SECONDARY. |
||
82 | * @param int $fixed Value that the receiver will receive. |
||
83 | * @param int $percentual Percentual value that the receiver will receive. Possible values: 0 - 100 |
||
84 | * @param bool $feePayor Flag to know if receiver is the payer of Moip tax. |
||
85 | * |
||
86 | * @return $this |
||
87 | */ |
||
88 | public function addReceiver($moipAccount, $type, $fixed = null, $percentual = null, $feePayor = false) |
||
108 | |||
109 | /** |
||
110 | * Initialize necessary used in some functions. |
||
111 | */ |
||
112 | protected function initialize() |
||
125 | |||
126 | /** |
||
127 | * Initialize necessary used in some functions. |
||
128 | */ |
||
129 | private function initializeSubtotals() |
||
135 | |||
136 | /** |
||
137 | * Mount the structure of order. |
||
138 | * |
||
139 | * @param \stdClass $response |
||
140 | * |
||
141 | * @return Orders Response order. |
||
142 | */ |
||
143 | protected function populate(stdClass $response) |
||
144 | { |
||
145 | $this->orders = clone $this; |
||
146 | $this->orders->data->id = $response->id; |
||
147 | $this->orders->data->ownId = $response->ownId; |
||
148 | $this->orders->data->amount->total = $response->amount->total; |
||
149 | $this->orders->data->amount->fees = $response->amount->fees; |
||
150 | $this->orders->data->amount->refunds = $response->amount->refunds; |
||
151 | $this->orders->data->amount->liquid = $response->amount->liquid; |
||
152 | $this->orders->data->amount->otherReceivers = $response->amount->otherReceivers; |
||
153 | $this->orders->data->amount->subtotals = $response->amount->subtotals; |
||
154 | |||
155 | $customer = new Customer($this->moip); |
||
156 | $this->orders->data->customer = $customer->populate($response->customer); |
||
157 | |||
158 | $this->orders->data->payments = $this->structure($response, Payment::PATH, Payment::class); |
||
159 | $this->orders->data->refunds = $this->structure($response, Refund::PATH, Refund::class); |
||
160 | $this->orders->data->entries = $this->structure($response, Entry::PATH, Entry::class); |
||
161 | $this->orders->data->events = $this->structure($response, Event::PATH, Event::class); |
||
162 | |||
163 | $this->orders->data->items = $response->items; |
||
164 | $this->orders->data->receivers = $response->receivers; |
||
165 | $this->orders->data->createdAt = $response->createdAt; |
||
166 | $this->orders->data->status = $response->status; |
||
167 | $this->orders->data->_links = $response->_links; |
||
168 | |||
169 | return $this->orders; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Structure resource. |
||
174 | * |
||
175 | * @param stdClass $response |
||
176 | * @param string $resource |
||
177 | * @param \Moip\Resource\Payment|\Moip\Resource\Refund|\Moip\Resource\Entry|\Moip\Resource\Event $class |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | private function structure(stdClass $response, $resource, $class) |
||
194 | |||
195 | /** |
||
196 | * Create a new order in MoIP. |
||
197 | * |
||
198 | * @return \Moip\Resource\Orders|stdClass |
||
199 | */ |
||
200 | public function create() |
||
204 | |||
205 | /** |
||
206 | * Get an order in MoIP. |
||
207 | * |
||
208 | * @param string $id_moip Id MoIP order id |
||
209 | * |
||
210 | * @return stdClass |
||
211 | */ |
||
212 | public function get($id_moip) |
||
216 | |||
217 | /** |
||
218 | * Get MoIP order id. |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | public function getId() |
||
226 | |||
227 | /** |
||
228 | * Get own request id. external reference. |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | public function getOwnId() |
||
236 | |||
237 | /** |
||
238 | * Get total value of order. |
||
239 | * |
||
240 | * @return int|float |
||
241 | */ |
||
242 | public function getAmountTotal() |
||
246 | |||
247 | /** |
||
248 | * Get total value of MoIP rate. |
||
249 | * |
||
250 | * @return int|float |
||
251 | */ |
||
252 | public function getAmountFees() |
||
256 | |||
257 | /** |
||
258 | * Get total amount of refunds. |
||
259 | * |
||
260 | * @return int|float |
||
261 | */ |
||
262 | public function getAmountRefunds() |
||
266 | |||
267 | /** |
||
268 | * Get net total value. |
||
269 | * |
||
270 | * @return int|float |
||
271 | */ |
||
272 | public function getAmountLiquid() |
||
276 | |||
277 | /** |
||
278 | * Get sum of amounts received by other recipients. Used in Marketplaces. |
||
279 | * |
||
280 | * @return int|float |
||
281 | */ |
||
282 | public function getAmountOtherReceivers() |
||
286 | |||
287 | /** |
||
288 | * Get currency used in the application. Possible values: BRL. |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | public function getCurrenty() |
||
296 | |||
297 | /** |
||
298 | * Get greight value of the item will be added to the value of the items. |
||
299 | * |
||
300 | * @return int|float |
||
301 | */ |
||
302 | public function getSubtotalShipping() |
||
308 | |||
309 | /** |
||
310 | * Get Additional value to the item will be added to the value of the items. |
||
311 | * |
||
312 | * @return int|float |
||
313 | */ |
||
314 | public function getSubtotalAddition() |
||
320 | |||
321 | /** |
||
322 | * Get discounted value of the item will be subtracted from the total value of the items. |
||
323 | * |
||
324 | * @return int|float |
||
325 | */ |
||
326 | public function getSubtotalDiscount() |
||
332 | |||
333 | /** |
||
334 | * Get summing the values of all items. |
||
335 | * |
||
336 | * @return int|float |
||
337 | */ |
||
338 | public function getSubtotalItems() |
||
344 | |||
345 | /** |
||
346 | * Ger structure item information request. |
||
347 | * |
||
348 | * @return \ArrayIterator |
||
349 | */ |
||
350 | public function getItemIterator() |
||
354 | |||
355 | /** |
||
356 | * Get Customer associated with the request. |
||
357 | * |
||
358 | * @return \Moip\Resource\Customer |
||
359 | */ |
||
360 | public function getCustomer() |
||
364 | |||
365 | /** |
||
366 | * Get payments associated with the request. |
||
367 | * |
||
368 | * @return ArrayIterator |
||
369 | */ |
||
370 | public function getPaymentIterator() |
||
374 | |||
375 | /** |
||
376 | * Get recipient structure of payments. |
||
377 | * |
||
378 | * @return ArrayIterator |
||
379 | */ |
||
380 | public function getReceiverIterator() |
||
384 | |||
385 | /** |
||
386 | * Get releases associated with the request. |
||
387 | * |
||
388 | * @return ArrayIterator |
||
389 | */ |
||
390 | public function getEventIterator() |
||
394 | |||
395 | /** |
||
396 | * Get repayments associated with the request. |
||
397 | * |
||
398 | * @return ArrayIterator |
||
399 | */ |
||
400 | public function getRefundIterator() |
||
404 | |||
405 | /** |
||
406 | * Get order status. |
||
407 | * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED. |
||
408 | * |
||
409 | * @return string |
||
410 | */ |
||
411 | public function getStatus() |
||
415 | |||
416 | /** |
||
417 | * Get date of resource creation. |
||
418 | * |
||
419 | * @return \DateTime |
||
420 | */ |
||
421 | public function getCreatedAt() |
||
425 | |||
426 | /** |
||
427 | * Get updated resource. |
||
428 | * |
||
429 | * @return \DateTime |
||
430 | */ |
||
431 | public function getUpdatedAt() |
||
435 | |||
436 | /** |
||
437 | * Get checkout preferences of the order. |
||
438 | * |
||
439 | * @return string |
||
440 | */ |
||
441 | public function getCheckoutPreferences() |
||
445 | |||
446 | /** |
||
447 | * Create a new Orders list instance. |
||
448 | * |
||
449 | * @return \Moip\Resource\OrdersList |
||
450 | */ |
||
451 | public function getList(Pagination $pagination = null, Filters $filters = null, $qParam = '') |
||
457 | |||
458 | /** |
||
459 | * Structure of payment. |
||
460 | * |
||
461 | * @return \Moip\Resource\Payment |
||
462 | */ |
||
463 | public function payments() |
||
470 | |||
471 | /** |
||
472 | * Structure of refund. |
||
473 | * |
||
474 | * @return \Moip\Resource\Refund |
||
475 | */ |
||
476 | public function refunds() |
||
483 | |||
484 | /** |
||
485 | * Set additional value to the item will be added to the value of the items. |
||
486 | * |
||
487 | * @param int|float $value additional value to the item. |
||
488 | * |
||
489 | * @return $this |
||
490 | */ |
||
491 | public function setAddition($value) |
||
500 | |||
501 | /** |
||
502 | * Set customer associated with the order. |
||
503 | * |
||
504 | * @param \Moip\Resource\Customer $customer customer associated with the request. |
||
505 | * |
||
506 | * @return $this |
||
507 | */ |
||
508 | public function setCustomer(Customer $customer) |
||
514 | |||
515 | /** |
||
516 | * Set customer id associated with the order. |
||
517 | * |
||
518 | * @param string $id Customer's id. |
||
519 | * |
||
520 | * @return $this |
||
521 | */ |
||
522 | public function setCustomerId($id) |
||
531 | |||
532 | /** |
||
533 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
534 | * |
||
535 | * @param int|float $value discounted value. |
||
536 | * |
||
537 | * @return $this |
||
538 | */ |
||
539 | public function setDiscount($value) |
||
545 | |||
546 | /** |
||
547 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
548 | * |
||
549 | * @deprecated |
||
550 | * |
||
551 | * @param int|float $value discounted value. |
||
552 | * |
||
553 | * @return $this |
||
554 | */ |
||
555 | public function setDiscont($value) |
||
561 | |||
562 | /** |
||
563 | * Set own request id. external reference. |
||
564 | * |
||
565 | * @param string $ownId external reference. |
||
566 | * |
||
567 | * @return $this |
||
568 | */ |
||
569 | public function setOwnId($ownId) |
||
575 | |||
576 | /** |
||
577 | * Set shipping Amount. |
||
578 | * |
||
579 | * @param float $value shipping Amount. |
||
580 | * |
||
581 | * @return $this |
||
582 | */ |
||
583 | public function setShippingAmount($value) |
||
589 | |||
590 | /** |
||
591 | * Set URL for redirection in case of success. |
||
592 | * |
||
593 | * @param string $urlSuccess UrlSuccess. |
||
594 | * |
||
595 | * @return $this |
||
596 | */ |
||
597 | public function setUrlSuccess($urlSuccess = '') |
||
603 | |||
604 | /** |
||
605 | * Set URL for redirection in case of failure. |
||
606 | * |
||
607 | * @param string $urlFailure UrlFailure. |
||
608 | * |
||
609 | * @return $this |
||
610 | */ |
||
611 | public function setUrlFailure($urlFailure = '') |
||
617 | |||
618 | /** |
||
619 | * Set installment settings for checkout preferences. |
||
620 | * |
||
621 | * @param array $quantity |
||
622 | * @param int $discountValue |
||
623 | * @param int $additionalValue |
||
624 | * |
||
625 | * @return $this |
||
626 | */ |
||
627 | public function addInstallmentCheckoutPreferences($quantity, $discountValue = 0, $additionalValue = 0) |
||
638 | } |
||
639 |