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 | * |
||
82 | * @return $this |
||
83 | */ |
||
84 | public function addReceiver($moipAccount, $type, $fixed) |
||
99 | |||
100 | /** |
||
101 | * Initialize necessary used in some functions. |
||
102 | */ |
||
103 | protected function initialize() |
||
113 | |||
114 | /** |
||
115 | * Initialize necessary used in some functions. |
||
116 | */ |
||
117 | private function initializeSubtotals() |
||
123 | |||
124 | /** |
||
125 | * Mount the structure of order. |
||
126 | * |
||
127 | * @param \stdClass $response |
||
128 | * |
||
129 | * @return Orders Response order. |
||
130 | */ |
||
131 | protected function populate(stdClass $response) |
||
160 | |||
161 | /** |
||
162 | * Structure resource. |
||
163 | * |
||
164 | * @param stdClass $response |
||
165 | * @param string $resource |
||
166 | * @param \Moip\Resource\Payment|\Moip\Resource\Refund|\Moip\Resource\Entry|\Moip\Resource\Event $class |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | private function structure(stdClass $response, $resource, $class) |
||
183 | |||
184 | /** |
||
185 | * Create a new order in MoIP. |
||
186 | * |
||
187 | * @return \Moip\Resource\Orders|stdClass |
||
188 | */ |
||
189 | public function create() |
||
193 | |||
194 | /** |
||
195 | * Get an order in MoIP. |
||
196 | * |
||
197 | * @param string $id_moip Id MoIP order id |
||
198 | * |
||
199 | * @return stdClass |
||
200 | */ |
||
201 | public function get($id_moip) |
||
205 | |||
206 | /** |
||
207 | * Get MoIP order id. |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public function getId() |
||
215 | |||
216 | /** |
||
217 | * Get own request id. external reference. |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | public function getOwnId() |
||
225 | |||
226 | /** |
||
227 | * Get total value of order. |
||
228 | * |
||
229 | * @return int|float |
||
230 | */ |
||
231 | public function getAmountTotal() |
||
235 | |||
236 | /** |
||
237 | * Get total value of MoIP rate. |
||
238 | * |
||
239 | * @return int|float |
||
240 | */ |
||
241 | public function getAmountFees() |
||
245 | |||
246 | /** |
||
247 | * Get total amount of refunds. |
||
248 | * |
||
249 | * @return int|float |
||
250 | */ |
||
251 | public function getAmountRefunds() |
||
255 | |||
256 | /** |
||
257 | * Get net total value. |
||
258 | * |
||
259 | * @return int|float |
||
260 | */ |
||
261 | public function getAmountLiquid() |
||
265 | |||
266 | /** |
||
267 | * Get sum of amounts received by other recipients. Used in Marketplaces. |
||
268 | * |
||
269 | * @return int|float |
||
270 | */ |
||
271 | public function getAmountOtherReceivers() |
||
275 | |||
276 | /** |
||
277 | * Get currency used in the application. Possible values: BRL. |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | public function getCurrenty() |
||
285 | |||
286 | /** |
||
287 | * Get greight value of the item will be added to the value of the items. |
||
288 | * |
||
289 | * @return int|float |
||
290 | */ |
||
291 | public function getSubtotalShipping() |
||
297 | |||
298 | /** |
||
299 | * Get Additional value to the item will be added to the value of the items. |
||
300 | * |
||
301 | * @return int|float |
||
302 | */ |
||
303 | public function getSubtotalAddition() |
||
309 | |||
310 | /** |
||
311 | * Get discounted value of the item will be subtracted from the total value of the items. |
||
312 | * |
||
313 | * @return int|float |
||
314 | */ |
||
315 | public function getSubtotalDiscount() |
||
321 | |||
322 | /** |
||
323 | * Get summing the values of all items. |
||
324 | * |
||
325 | * @return int|float |
||
326 | */ |
||
327 | public function getSubtotalItems() |
||
333 | |||
334 | /** |
||
335 | * Ger structure item information request. |
||
336 | * |
||
337 | * @return \ArrayIterator |
||
338 | */ |
||
339 | public function getItemIterator() |
||
343 | |||
344 | /** |
||
345 | * Get Customer associated with the request. |
||
346 | * |
||
347 | * @return \Moip\Resource\Customer |
||
348 | */ |
||
349 | public function getCustomer() |
||
353 | |||
354 | /** |
||
355 | * Get payments associated with the request. |
||
356 | * |
||
357 | * @return ArrayIterator |
||
358 | */ |
||
359 | public function getPaymentIterator() |
||
363 | |||
364 | /** |
||
365 | * Get recipient structure of payments. |
||
366 | * |
||
367 | * @return ArrayIterator |
||
368 | */ |
||
369 | public function getReceiverIterator() |
||
373 | |||
374 | /** |
||
375 | * Get releases associated with the request. |
||
376 | * |
||
377 | * @return ArrayIterator |
||
378 | */ |
||
379 | public function getEventIterator() |
||
383 | |||
384 | /** |
||
385 | * Get repayments associated with the request. |
||
386 | * |
||
387 | * @return ArrayIterator |
||
388 | */ |
||
389 | public function getRefundIterator() |
||
393 | |||
394 | /** |
||
395 | * Get order status. |
||
396 | * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED. |
||
397 | * |
||
398 | * @return string |
||
399 | */ |
||
400 | public function getStatus() |
||
404 | |||
405 | /** |
||
406 | * Get date of resource creation. |
||
407 | * |
||
408 | * @return \DateTime |
||
409 | */ |
||
410 | public function getCreatedAt() |
||
414 | |||
415 | /** |
||
416 | * Get updated resource. |
||
417 | * |
||
418 | * @return \DateTime |
||
419 | */ |
||
420 | public function getUpdatedAt() |
||
424 | |||
425 | /** |
||
426 | * Structure of payment. |
||
427 | * |
||
428 | * @return \Moip\Resource\Payment |
||
429 | */ |
||
430 | public function payments() |
||
437 | |||
438 | /** |
||
439 | * Structure of refund. |
||
440 | * |
||
441 | * @return \Moip\Resource\Refund |
||
442 | */ |
||
443 | public function refunds() |
||
450 | |||
451 | /** |
||
452 | * Set additional value to the item will be added to the value of the items. |
||
453 | * |
||
454 | * @param int|float $value additional value to the item. |
||
455 | * |
||
456 | * @return $this |
||
457 | */ |
||
458 | public function setAddition($value) |
||
459 | { |
||
460 | if (!isset($this->data->amount->subtotals)) { |
||
461 | $this->data->amount->subtotals = new stdClass(); |
||
462 | } |
||
463 | $this->data->amount->subtotals->addition = (float) $value; |
||
464 | |||
465 | return $this; |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * Set customer associated with the order. |
||
470 | * |
||
471 | * @param \Moip\Resource\Customer $customer customer associated with the request. |
||
472 | * |
||
473 | * @return $this |
||
474 | */ |
||
475 | public function setCustomer(Customer $customer) |
||
481 | |||
482 | /** |
||
483 | * Set customer id associated with the order. |
||
484 | * |
||
485 | * @param string $id Customer's id. |
||
486 | * |
||
487 | * @return $this |
||
488 | */ |
||
489 | public function setCustomerId($id) |
||
498 | |||
499 | /** |
||
500 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
501 | * |
||
502 | * @param int|float $value discounted value. |
||
503 | * |
||
504 | * @return $this |
||
505 | */ |
||
506 | public function setDiscount($value) |
||
512 | |||
513 | /** |
||
514 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
515 | * |
||
516 | * @deprecated |
||
517 | * |
||
518 | * @param int|float $value discounted value. |
||
519 | * |
||
520 | * @return $this |
||
521 | */ |
||
522 | public function setDiscont($value) |
||
528 | |||
529 | /** |
||
530 | * Set own request id. external reference. |
||
531 | * |
||
532 | * @param string $ownId external reference. |
||
533 | * |
||
534 | * @return $this |
||
535 | */ |
||
536 | public function setOwnId($ownId) |
||
542 | |||
543 | /** |
||
544 | * Set shipping Amount. |
||
545 | * |
||
546 | * @param float $value shipping Amount. |
||
547 | * |
||
548 | * @return $this |
||
549 | */ |
||
550 | public function setShippingAmount($value) |
||
556 | } |
||
557 |