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) |
||
55 | { |
||
56 | if (!is_int($price)) { |
||
57 | throw new \UnexpectedValueException('Informe o valor do item como inteiro'); |
||
58 | } |
||
59 | |||
60 | if (!is_int($quantity) || $quantity < 1) { |
||
61 | throw new \UnexpectedValueException('A quantidade do item deve ser um valor inteiro maior que 0'); |
||
62 | } |
||
63 | |||
64 | $item = new stdClass(); |
||
65 | $item->product = $product; |
||
66 | $item->quantity = $quantity; |
||
67 | $item->detail = $detail; |
||
68 | $item->price = $price; |
||
69 | |||
70 | $this->data->items[] = $item; |
||
71 | |||
72 | return $this; |
||
73 | } |
||
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) |
||
85 | { |
||
86 | $receiver = new stdClass(); |
||
87 | $receiver->moipAccount = new stdClass(); |
||
88 | $receiver->moipAccount->id = $moipAccount; |
||
89 | if (!empty($fixed)) { |
||
90 | $receiver->amount = new stdClass(); |
||
91 | $receiver->amount->fixed = $fixed; |
||
92 | } |
||
93 | $receiver->type = $type; |
||
94 | |||
95 | $this->data->receivers[] = $receiver; |
||
96 | |||
97 | return $this; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Initialize necessary used in some functions. |
||
102 | */ |
||
103 | protected function initialize() |
||
104 | { |
||
105 | $this->data = new stdClass(); |
||
106 | $this->data->ownId = null; |
||
107 | $this->data->amount = new stdClass(); |
||
108 | $this->data->amount->currency = self::AMOUNT_CURRENCY; |
||
109 | $this->data->amount->subtotals = new stdClass(); |
||
110 | $this->data->items = []; |
||
111 | $this->data->receivers = []; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Initialize necessary used in some functions. |
||
116 | */ |
||
117 | private function initializeSubtotals() |
||
118 | { |
||
119 | if (!isset($this->data->subtotals)) { |
||
120 | $this->data->subtotals = new stdClass(); |
||
121 | } |
||
122 | } |
||
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) |
||
132 | { |
||
133 | $this->orders = clone $this; |
||
134 | $this->orders->data->id = $response->id; |
||
135 | $this->orders->data->ownId = $response->ownId; |
||
136 | $this->orders->data->amount->total = $response->amount->total; |
||
137 | $this->orders->data->amount->fees = $response->amount->fees; |
||
138 | $this->orders->data->amount->refunds = $response->amount->refunds; |
||
139 | $this->orders->data->amount->liquid = $response->amount->liquid; |
||
140 | $this->orders->data->amount->otherReceivers = $response->amount->otherReceivers; |
||
141 | $this->orders->data->amount->subtotals = $response->amount->subtotals; |
||
142 | |||
143 | $customer = new Customer($this->moip); |
||
144 | $this->orders->data->customer = $customer->populate($response->customer); |
||
145 | |||
146 | $this->orders->data->payments = $this->structure($response, Payment::PATH, Payment::class); |
||
147 | $this->orders->data->refunds = $this->structure($response, Refund::PATH, Refund::class); |
||
148 | $this->orders->data->entries = $this->structure($response, Entry::PATH, Entry::class); |
||
149 | $this->orders->data->events = $this->structure($response, Event::PATH, Event::class); |
||
150 | |||
151 | $this->orders->data->items = $response->items; |
||
152 | $this->orders->data->receivers = $response->receivers; |
||
153 | $this->orders->data->createdAt = $response->createdAt; |
||
154 | $this->orders->data->status = $response->status; |
||
155 | $this->orders->data->_links = $response->_links; |
||
156 | |||
157 | return $this->orders; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Structure resource. |
||
162 | * |
||
163 | * @param stdClass $response |
||
164 | * @param string $resource |
||
165 | * @param \Moip\Resource\Payment|\Moip\Resource\Refund|\Moip\Resource\Entry|\Moip\Resource\Event $class |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | private function structure(stdClass $response, $resource, $class) |
||
182 | |||
183 | /** |
||
184 | * Create a new order in MoIP. |
||
185 | * |
||
186 | * @return \Moip\Resource\Orders|stdClass |
||
187 | */ |
||
188 | public function create() |
||
192 | |||
193 | /** |
||
194 | * Get an order in MoIP. |
||
195 | * |
||
196 | * @param string $id_moip Id MoIP order id |
||
197 | * |
||
198 | * @return stdClass |
||
199 | */ |
||
200 | public function get($id_moip) |
||
204 | |||
205 | /** |
||
206 | * Get MoIP order id. |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | public function getId() |
||
214 | |||
215 | /** |
||
216 | * Get own request id. external reference. |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | public function getOwnId() |
||
224 | |||
225 | /** |
||
226 | * Get total value of order. |
||
227 | * |
||
228 | * @return int|float |
||
229 | */ |
||
230 | public function getAmountTotal() |
||
234 | |||
235 | /** |
||
236 | * Get total value of MoIP rate. |
||
237 | * |
||
238 | * @return int|float |
||
239 | */ |
||
240 | public function getAmountFees() |
||
244 | |||
245 | /** |
||
246 | * Get total amount of refunds. |
||
247 | * |
||
248 | * @return int|float |
||
249 | */ |
||
250 | public function getAmountRefunds() |
||
254 | |||
255 | /** |
||
256 | * Get net total value. |
||
257 | * |
||
258 | * @return int|float |
||
259 | */ |
||
260 | public function getAmountLiquid() |
||
264 | |||
265 | /** |
||
266 | * Get sum of amounts received by other recipients. Used in Marketplaces. |
||
267 | * |
||
268 | * @return int|float |
||
269 | */ |
||
270 | public function getAmountOtherReceivers() |
||
274 | |||
275 | /** |
||
276 | * Get currency used in the application. Possible values: BRL. |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | public function getCurrenty() |
||
284 | |||
285 | /** |
||
286 | * Get greight value of the item will be added to the value of the items. |
||
287 | * |
||
288 | * @return int|float |
||
289 | */ |
||
290 | public function getSubtotalShipping() |
||
296 | |||
297 | /** |
||
298 | * Get Additional value to the item will be added to the value of the items. |
||
299 | * |
||
300 | * @return int|float |
||
301 | */ |
||
302 | public function getSubtotalAddition() |
||
308 | |||
309 | /** |
||
310 | * Get discounted value of the item will be subtracted from the total value of the items. |
||
311 | * |
||
312 | * @return int|float |
||
313 | */ |
||
314 | public function getSubtotalDiscount() |
||
320 | |||
321 | /** |
||
322 | * Get summing the values of all items. |
||
323 | * |
||
324 | * @return int|float |
||
325 | */ |
||
326 | public function getSubtotalItems() |
||
332 | |||
333 | /** |
||
334 | * Ger structure item information request. |
||
335 | * |
||
336 | * @return \ArrayIterator |
||
337 | */ |
||
338 | public function getItemIterator() |
||
342 | |||
343 | /** |
||
344 | * Get Customer associated with the request. |
||
345 | * |
||
346 | * @return \Moip\Resource\Customer |
||
347 | */ |
||
348 | public function getCustomer() |
||
352 | |||
353 | /** |
||
354 | * Get payments associated with the request. |
||
355 | * |
||
356 | * @return ArrayIterator |
||
357 | */ |
||
358 | public function getPaymentIterator() |
||
362 | |||
363 | /** |
||
364 | * Get recipient structure of payments. |
||
365 | * |
||
366 | * @return ArrayIterator |
||
367 | */ |
||
368 | public function getReceiverIterator() |
||
372 | |||
373 | /** |
||
374 | * Get releases associated with the request. |
||
375 | * |
||
376 | * @return ArrayIterator |
||
377 | */ |
||
378 | public function getEventIterator() |
||
382 | |||
383 | /** |
||
384 | * Get repayments associated with the request. |
||
385 | * |
||
386 | * @return ArrayIterator |
||
387 | */ |
||
388 | public function getRefundIterator() |
||
392 | |||
393 | /** |
||
394 | * Get order status. |
||
395 | * Possible values: CREATED, WAITING, PAID, NOT_PAID, REVERTED. |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | public function getStatus() |
||
403 | |||
404 | /** |
||
405 | * Get date of resource creation. |
||
406 | * |
||
407 | * @return \DateTime |
||
408 | */ |
||
409 | public function getCreatedAt() |
||
413 | |||
414 | /** |
||
415 | * Get updated resource. |
||
416 | * |
||
417 | * @return \DateTime |
||
418 | */ |
||
419 | public function getUpdatedAt() |
||
423 | |||
424 | /** |
||
425 | * Structure of payment. |
||
426 | * |
||
427 | * @return \Moip\Resource\Payment |
||
428 | */ |
||
429 | public function payments() |
||
436 | |||
437 | /** |
||
438 | * Structure of refund. |
||
439 | * |
||
440 | * @return \Moip\Resource\Refund |
||
441 | */ |
||
442 | public function refunds() |
||
449 | |||
450 | /** |
||
451 | * Set additional value to the item will be added to the value of the items. |
||
452 | * |
||
453 | * @param int|float $value additional value to the item. |
||
454 | * |
||
455 | * @return $this |
||
456 | */ |
||
457 | public function setAddition($value) |
||
458 | { |
||
459 | if (!isset($this->data->amount->subtotals)) { |
||
460 | $this->data->amount->subtotals = new stdClass(); |
||
461 | } |
||
462 | $this->data->amount->subtotals->addition = (float) $value; |
||
463 | |||
464 | return $this; |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Set customer associated with the order. |
||
469 | * |
||
470 | * @param \Moip\Resource\Customer $customer customer associated with the request. |
||
471 | * |
||
472 | * @return $this |
||
473 | */ |
||
474 | public function setCustomer(Customer $customer) |
||
480 | |||
481 | /** |
||
482 | * Set customer id associated with the order. |
||
483 | * |
||
484 | * @param string $id Customer's id. |
||
485 | * |
||
486 | * @return $this |
||
487 | */ |
||
488 | public function setCustomerId($id) |
||
497 | |||
498 | /** |
||
499 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
500 | * |
||
501 | * @param int|float $value discounted value. |
||
502 | * |
||
503 | * @return $this |
||
504 | */ |
||
505 | public function setDiscount($value) |
||
511 | |||
512 | /** |
||
513 | * Set discounted value of the item will be subtracted from the total value of the items. |
||
514 | * |
||
515 | * @deprecated |
||
516 | * |
||
517 | * @param int|float $value discounted value. |
||
518 | * |
||
519 | * @return $this |
||
520 | */ |
||
521 | public function setDiscont($value) |
||
527 | |||
528 | /** |
||
529 | * Set own request id. external reference. |
||
530 | * |
||
531 | * @param string $ownId external reference. |
||
532 | * |
||
533 | * @return $this |
||
534 | */ |
||
535 | public function setOwnId($ownId) |
||
541 | |||
542 | /** |
||
543 | * Set shipping Amount. |
||
544 | * |
||
545 | * @param float $value shipping Amount. |
||
546 | * |
||
547 | * @return $this |
||
548 | */ |
||
549 | public function setShippingAmount($value) |
||
555 | } |
||
556 |