| Conditions | 12 |
| Paths | 9 |
| Total Lines | 64 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 38 | public function result(Request $request) |
||
| 39 | { |
||
| 40 | //No result code in the request |
||
| 41 | $resultCode = $request->request->get('RESULT'); |
||
| 42 | if (is_null($resultCode)) { |
||
| 43 | return new Response('Missing result code', Response::HTTP_BAD_REQUEST); |
||
| 44 | } |
||
| 45 | |||
| 46 | //Cannot find the transaction in the database |
||
| 47 | $invoiceNumber = $request->request->get('INVOICE'); |
||
| 48 | $transaction = $this->getDoctrine()->getRepository(Transaction::class)->findOneBy(['invoice_number' => $invoiceNumber]); |
||
| 49 | if (!$transaction) { |
||
| 50 | return new Response('Cannot find the transaction', Response::HTTP_BAD_REQUEST); |
||
| 51 | } |
||
| 52 | |||
| 53 | //The transaction is already paid or updated. |
||
| 54 | $status = $transaction->getStatus(); |
||
| 55 | if ($status === Transaction::STATUS_PAID || $status === Transaction::STATUS_COMPLETED) { |
||
| 56 | return new Response('The transaction is completed.', Response::HTTP_BAD_REQUEST); |
||
| 57 | } |
||
| 58 | |||
| 59 | //Amount does not match. |
||
| 60 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 61 | if ($transaction->getTotalBalance() != $request->request->get('AMOUNT')) { |
||
| 62 | $transaction->setStatus(Transaction::STATUS_ERROR); |
||
| 63 | $entityManager->persist($transaction); |
||
| 64 | $entityManager->flush(); |
||
| 65 | return new Response('Invalid amount', Response::HTTP_BAD_REQUEST); |
||
| 66 | } |
||
| 67 | |||
| 68 | //Communication error |
||
| 69 | if ($resultCode < 0) { |
||
| 70 | return new Response('Communication error', Response::HTTP_OK); |
||
| 71 | } |
||
| 72 | |||
| 73 | //The transaction is declined on Payflow. |
||
| 74 | if ($resultCode > 0) { |
||
| 75 | $transaction->setStatus(Transaction::STATUS_DECLINED); |
||
| 76 | $entityManager->persist($transaction); |
||
| 77 | $entityManager->flush(); |
||
| 78 | return new Response('Declined by Payflow', Response::HTTP_OK); |
||
| 79 | } |
||
| 80 | |||
| 81 | //The transaction is declined by PayPal due to AVS or CSC check failed. |
||
| 82 | $responseMessage = $request->request->get('RESPMSG'); |
||
| 83 | if ($resultCode == 0 && ($responseMessage == 'AVSDECLINED' || $responseMessage == 'CSCDECLINED')) { |
||
| 84 | $transaction->setStatus(Transaction::STATUS_DECLINED); |
||
| 85 | $entityManager->persist($transaction); |
||
| 86 | $entityManager->flush(); |
||
| 87 | return new Response('Declined by Payflow', Response::HTTP_OK); |
||
| 88 | } |
||
| 89 | |||
| 90 | $transaction->setStatus(Transaction::STATUS_PAID); |
||
| 91 | |||
| 92 | if ($this->updateFeesOnAlma($transaction)) { |
||
| 93 | $transaction->setStatus(Transaction::STATUS_COMPLETED); |
||
| 94 | } else { |
||
| 95 | $transaction->setStatus(Transaction::STATUS_FAILED); |
||
| 96 | } |
||
| 97 | |||
| 98 | $entityManager->persist($transaction); |
||
| 99 | $entityManager->flush(); |
||
| 100 | |||
| 101 | return new Response("Success", Response::HTTP_OK); |
||
| 102 | } |
||
| 127 |