Complex classes like Payment 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 Payment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Payment extends MoipResource |
||
12 | { |
||
13 | /** |
||
14 | * @const string |
||
15 | */ |
||
16 | const PATH = 'payments'; |
||
17 | |||
18 | /** |
||
19 | * @const string |
||
20 | */ |
||
21 | const MULTI_PAYMENTS_PATH = 'multipayments'; |
||
22 | |||
23 | /** |
||
24 | * @const string |
||
25 | */ |
||
26 | const SIMULATOR_PATH = 'simulador'; |
||
27 | |||
28 | /** |
||
29 | * Payment means. |
||
30 | * |
||
31 | * @const string |
||
32 | */ |
||
33 | const METHOD_CREDIT_CARD = 'CREDIT_CARD'; |
||
34 | |||
35 | /** |
||
36 | * Payment means. |
||
37 | * |
||
38 | * @const string |
||
39 | */ |
||
40 | const METHOD_BOLETO = 'BOLETO'; |
||
41 | |||
42 | /** |
||
43 | * Payment means. |
||
44 | * |
||
45 | * @const string |
||
46 | */ |
||
47 | const METHOD_ONLINE_DEBIT = 'ONLINE_DEBIT'; |
||
48 | |||
49 | /** |
||
50 | * Payment means. |
||
51 | * |
||
52 | * @const string |
||
53 | */ |
||
54 | const METHOD_WALLET = 'WALLET'; |
||
55 | |||
56 | /** |
||
57 | * Payment means. |
||
58 | * |
||
59 | * @const string |
||
60 | */ |
||
61 | const METHOD_ONLINE_BANK_DEBIT = 'ONLINE_BANK_DEBIT'; |
||
62 | |||
63 | /** |
||
64 | * @var \Moip\Resource\Orders |
||
65 | */ |
||
66 | private $order; |
||
67 | |||
68 | /** |
||
69 | * Just created, but not initialized yet. |
||
70 | */ |
||
71 | const STATUS_CREATED = 'CREATED'; |
||
72 | |||
73 | /** |
||
74 | * Waiting for the payment. |
||
75 | */ |
||
76 | const STATUS_WAITING = 'WAITING'; |
||
77 | |||
78 | /** |
||
79 | * On risk analysis, it may be automatic or manual. |
||
80 | */ |
||
81 | const STATUS_IN_ANALYSIS = 'IN_ANALYSIS'; |
||
82 | |||
83 | /** |
||
84 | * The amount was reserved on client credit card, it may be caught or discarded until 5 days. |
||
85 | */ |
||
86 | const STATUS_PRE_AUTHORIZED = 'PRE_AUTHORIZED'; |
||
87 | |||
88 | /** |
||
89 | * Payment confirmed by the bank institution. |
||
90 | */ |
||
91 | const STATUS_AUTHORIZED = 'AUTHORIZED'; |
||
92 | |||
93 | /** |
||
94 | * Payment cancelled. |
||
95 | */ |
||
96 | const STATUS_CANCELLED = 'CANCELLED'; |
||
97 | |||
98 | /** |
||
99 | * Payment refunded. |
||
100 | */ |
||
101 | const STATUS_REFUNDED = 'REFUNDED'; |
||
102 | |||
103 | /** |
||
104 | * Paymend reversed (it means that the payment may was not recognized by the client). |
||
105 | */ |
||
106 | const STATUS_REVERSED = 'REVERSED'; |
||
107 | |||
108 | /** |
||
109 | * Payment finalized, the amout is on your account. |
||
110 | */ |
||
111 | const STATUS_SETTLED = 'SETTLED'; |
||
112 | |||
113 | /** |
||
114 | * @var \Moip\Resource\Multiorders |
||
115 | */ |
||
116 | private $multiorder; |
||
117 | |||
118 | /** |
||
119 | * Initializes new instances. |
||
120 | */ |
||
121 | protected function initialize() |
||
127 | |||
128 | /** |
||
129 | * Create a new payment in api MoIP. |
||
130 | * |
||
131 | * @return $this |
||
132 | */ |
||
133 | public function execute() |
||
134 | { |
||
135 | if ($this->order !== null) { |
||
136 | $path = sprintf('/%s/%s/%s/%s', MoipResource::VERSION, Orders::PATH, $this->order->getId(), self::PATH); |
||
137 | } else { |
||
138 | $path = sprintf('/%s/%s/%s/%s', MoipResource::VERSION, Multiorders::PATH, $this->multiorder->getId(), self::MULTI_PAYMENTS_PATH); |
||
139 | } |
||
140 | |||
141 | $response = $this->httpRequest($path, Requests::POST, $this); |
||
142 | |||
143 | return $this->populate($response); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Get an payment and multipayment in MoIP. |
||
148 | * |
||
149 | * @param string $id_moip Id MoIP payment |
||
150 | * |
||
151 | * @return stdClass |
||
152 | */ |
||
153 | public function get($id_moip) |
||
154 | { |
||
155 | if ($this->order !== null) { |
||
156 | return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::PATH, $id_moip)); |
||
157 | } |
||
158 | |||
159 | return $this->getByPath(sprintf('/%s/%s/%s', MoipResource::VERSION, self::MULTI_PAYMENTS_PATH, $id_moip)); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Get id MoIP payment. |
||
164 | * |
||
165 | * |
||
166 | * @return \Moip\Resource\Payment |
||
167 | */ |
||
168 | public function getId() |
||
169 | { |
||
170 | return $this->getIfSet('id'); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Mount payment structure. |
||
175 | * |
||
176 | * @param \stdClass $response |
||
177 | * |
||
178 | * @return Payment |
||
179 | */ |
||
180 | protected function populate(stdClass $response) |
||
181 | { |
||
182 | $payment = clone $this; |
||
183 | |||
184 | $payment->data->id = $this->getIfSet('id', $response); |
||
185 | $payment->data->status = $this->getIfSet('status', $response); |
||
186 | $payment->data->delayCapture = $this->getIfSet('delayCapture', $response); |
||
187 | $payment->data->amount = new stdClass(); |
||
188 | $payment->data->amount->total = $this->getIfSet('total', $response->amount); |
||
189 | $payment->data->amount->currency = $this->getIfSet('currency', $response->amount); |
||
190 | $payment->data->installmentCount = $this->getIfSet('installmentCount', $response); |
||
191 | $payment->data->fundingInstrument = $this->getIfSet('fundingInstrument', $response); |
||
192 | $payment->data->escrows = $this->getIfSet('escrows', $response); |
||
193 | $payment->data->fees = $this->getIfSet('fees', $response); |
||
194 | $payment->data->refunds = $this->getIfSet('refunds', $response); |
||
195 | $payment->data->_links = $this->getIfSet('_links', $response); |
||
196 | $payment->data->createdAt = $this->getIfSetDateTime('createdAt', $response); |
||
197 | $payment->data->updatedAt = $this->getIfSetDateTime('updatedAt', $response); |
||
198 | |||
199 | return $payment; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Refunds. |
||
204 | * |
||
205 | * @return Refund |
||
206 | */ |
||
207 | public function refunds() |
||
214 | |||
215 | /** |
||
216 | * Escrows. |
||
217 | * |
||
218 | * @return Escrow |
||
219 | */ |
||
220 | public function escrows() |
||
227 | |||
228 | /** |
||
229 | * Get payment status. |
||
230 | * |
||
231 | * @return string Payment status. Possible values CREATED, WAITING, IN_ANALYSIS, PRE_AUTHORIZED, AUTHORIZED, CANCELLED, REFUNDED, REVERSED, SETTLED |
||
232 | */ |
||
233 | public function getStatus() |
||
237 | |||
238 | /** |
||
239 | * get creation time. |
||
240 | * |
||
241 | * @return \DateTime |
||
242 | */ |
||
243 | public function getCreatedAt() |
||
247 | |||
248 | /** |
||
249 | * Returns when the last update occurred. |
||
250 | * |
||
251 | * @return \DateTime |
||
252 | */ |
||
253 | public function getUpdatedAt() |
||
257 | |||
258 | /** |
||
259 | * Returns the funding instrument. |
||
260 | * |
||
261 | * @return stdClass |
||
262 | */ |
||
263 | public function getFundingInstrument() |
||
268 | |||
269 | /** |
||
270 | * Get href to Boleto |
||
271 | * *. |
||
272 | * |
||
273 | * @return stdClass |
||
274 | */ |
||
275 | public function getHrefBoleto() |
||
279 | |||
280 | /** |
||
281 | * Get LineCode to Boleto |
||
282 | * *. |
||
283 | * |
||
284 | * @return stdClass |
||
285 | */ |
||
286 | public function getLineCodeBoleto() |
||
290 | |||
291 | /** |
||
292 | * Get href from print to Boleto |
||
293 | * *. |
||
294 | * |
||
295 | * @return stdClass |
||
296 | */ |
||
297 | public function getHrefPrintBoleto() |
||
301 | |||
302 | /** |
||
303 | * Get Expirate Date to Boleto |
||
304 | * *. |
||
305 | * |
||
306 | * @return stdClass |
||
307 | */ |
||
308 | public function getExpirationDateBoleto() |
||
312 | |||
313 | /** |
||
314 | * Returns payment amount. |
||
315 | * |
||
316 | * @return stdClass |
||
317 | */ |
||
318 | public function getAmount() |
||
322 | |||
323 | /** |
||
324 | * Returns escrow. |
||
325 | * |
||
326 | * @return stdClass |
||
327 | */ |
||
328 | public function getEscrow() |
||
332 | |||
333 | /** |
||
334 | * Returns installment count. |
||
335 | * |
||
336 | * @return stdClass |
||
337 | */ |
||
338 | public function getInstallmentCount() |
||
342 | |||
343 | /** |
||
344 | * Set means of payment. |
||
345 | * |
||
346 | * @param \stdClass $fundingInstrument |
||
347 | * |
||
348 | * @return $this |
||
349 | */ |
||
350 | public function setFundingInstrument(stdClass $fundingInstrument) |
||
356 | |||
357 | /** |
||
358 | * Set billet. |
||
359 | * |
||
360 | * @param \DateTime|string $expirationDate Expiration date of a billet. |
||
361 | * @param string $logoUri Logo of billet. |
||
362 | * @param array $instructionLines Instructions billet. |
||
363 | * |
||
364 | * @return $this |
||
365 | */ |
||
366 | public function setBoleto($expirationDate, $logoUri, array $instructionLines = []) |
||
387 | |||
388 | /** |
||
389 | * Set credit card holder. |
||
390 | * |
||
391 | * @param \Moip\Resource\Customer $holder |
||
392 | */ |
||
393 | private function setCreditCardHolder(Customer $holder) |
||
411 | |||
412 | /** |
||
413 | * Set credit cardHash. |
||
414 | * |
||
415 | * @param string $hash Credit card hash encripted using Moip.js |
||
416 | * @param \Moip\Resource\Customer $holder |
||
417 | * @param bool $store Flag to know if credit card should be saved. |
||
418 | * |
||
419 | * @return $this |
||
420 | */ |
||
421 | public function setCreditCardHash($hash, Customer $holder, $store = true) |
||
431 | |||
432 | /** |
||
433 | * Set credit card |
||
434 | * Credit card used in a payment. |
||
435 | * The card when returned within a parent resource is presented in its minimum representation. |
||
436 | * |
||
437 | * @param int $expirationMonth Card expiration month |
||
438 | * @param int $expirationYear Year of card expiration. |
||
439 | * @param string $number Card number. |
||
440 | * @param int $cvc Card Security Code. |
||
441 | * @param \Moip\Resource\Customer $holder |
||
442 | * @param bool $store Flag to know if credit card should be saved. |
||
443 | * |
||
444 | * @return $this |
||
445 | */ |
||
446 | public function setCreditCard($expirationMonth, $expirationYear, $number, $cvc, Customer $holder, $store = true) |
||
459 | |||
460 | /** |
||
461 | * Sets data from a previously saved credit card |
||
462 | * Credit card used in a payment. |
||
463 | * Used when the credit card was saved with the customer and the payment made in a future date. |
||
464 | * |
||
465 | * @param string $creditCardId MoIP's Credit Card Id. |
||
466 | * @param int $cvc Card Security Code. |
||
467 | * |
||
468 | * @return $this |
||
469 | */ |
||
470 | public function setCreditCardSaved($creditCardId, $cvc) |
||
480 | |||
481 | /** |
||
482 | * Set installment count. |
||
483 | * |
||
484 | * @param int $installmentCount |
||
485 | * |
||
486 | * @return $this |
||
487 | */ |
||
488 | public function setInstallmentCount($installmentCount) |
||
494 | |||
495 | /** |
||
496 | * Set statement descriptor. |
||
497 | * |
||
498 | * @param string $statementDescriptor |
||
499 | * |
||
500 | * @return $this |
||
501 | */ |
||
502 | public function setStatementDescriptor($statementDescriptor) |
||
508 | |||
509 | /** |
||
510 | * Set payment means made available by banks. |
||
511 | * |
||
512 | * @param string $bankNumber Bank number. Possible values: 001, 237, 341, 041. |
||
513 | * @param \DateTime|string $expirationDate Date of expiration debit. |
||
514 | * @param string $returnUri Return Uri. |
||
515 | * |
||
516 | * @return $this |
||
517 | */ |
||
518 | public function setOnlineBankDebit($bankNumber, $expirationDate, $returnUri) |
||
531 | |||
532 | /** |
||
533 | * Set Multiorders. |
||
534 | * |
||
535 | * @param \Moip\Resource\Multiorders $multiorder |
||
536 | * |
||
537 | * @return $this |
||
538 | */ |
||
539 | public function setMultiorder(Multiorders $multiorder) |
||
545 | |||
546 | /** |
||
547 | * Set order. |
||
548 | * |
||
549 | * @param \Moip\Resource\Orders $order |
||
550 | * |
||
551 | * @return $this |
||
552 | */ |
||
553 | public function setOrder(Orders $order) |
||
559 | |||
560 | /** |
||
561 | * Turns on a delay on credit card payment capture (pre-authorization). |
||
562 | * |
||
563 | * @return $this |
||
564 | */ |
||
565 | public function setDelayCapture() |
||
571 | |||
572 | /** |
||
573 | * Set escrow to a payment. |
||
574 | * |
||
575 | * @param string $description |
||
576 | * |
||
577 | * @return $this |
||
578 | */ |
||
579 | public function setEscrow($description) |
||
586 | |||
587 | /** |
||
588 | * Capture a pre-authorized amount on a credit card payment. |
||
589 | * |
||
590 | * @throws \Exception |
||
591 | * |
||
592 | * @return Payment |
||
593 | */ |
||
594 | public function capture() |
||
605 | |||
606 | /** |
||
607 | * Avoid a pre-authorized amount on a credit card payment. |
||
608 | * |
||
609 | * @throws \Exception |
||
610 | * |
||
611 | * @return Payment |
||
612 | */ |
||
613 | public function avoid() |
||
624 | |||
625 | /** |
||
626 | * Authorize a payment (Available only in sandbox to credit card payment with status IN_ANALYSIS and billet payment with status WAITING). |
||
627 | * |
||
628 | * @return bool |
||
629 | */ |
||
630 | public function authorize($amount = null) |
||
644 | } |
||
645 |