| Conditions | 12 |
| Paths | 9 |
| Total Lines | 64 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 27 | public function result(Request $request) |
||
| 28 | { |
||
| 29 | //No result code in the request |
||
| 30 | $resultCode = $request->request->get('RESULT'); |
||
| 31 | if (is_null($resultCode)) { |
||
| 32 | return new Response('Missing result code', Response::HTTP_BAD_REQUEST); |
||
| 33 | } |
||
| 34 | |||
| 35 | //Cannot find the transaction in the database |
||
| 36 | $invoiceNumber = $request->request->get('INVOICE'); |
||
| 37 | $transaction = $this->getDoctrine()->getRepository(Transaction::class)->findOneBy(['invoice_number' => $invoiceNumber]); |
||
| 38 | if (!$transaction) { |
||
| 39 | return new Response('Cannot find the transaction', Response::HTTP_BAD_REQUEST); |
||
| 40 | } |
||
| 41 | |||
| 42 | //The transaction is already paid or updated. |
||
| 43 | $status = $transaction->getStatus(); |
||
| 44 | if ($status === Transaction::STATUS_PAID || $status === Transaction::STATUS_COMPLETED) { |
||
| 45 | return new Response('The transaction is completed.', Response::HTTP_BAD_REQUEST); |
||
| 46 | } |
||
| 47 | |||
| 48 | //Amount does not match. |
||
| 49 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 50 | if ($transaction->getTotalBalance() != $request->request->get('AMOUNT')) { |
||
| 51 | $transaction->setStatus(Transaction::STATUS_ERROR); |
||
| 52 | $entityManager->persist($transaction); |
||
| 53 | $entityManager->flush(); |
||
| 54 | return new Response('Invalid amount', Response::HTTP_BAD_REQUEST); |
||
| 55 | } |
||
| 56 | |||
| 57 | //Communication error |
||
| 58 | if ($resultCode < 0) { |
||
| 59 | return new Response('Communication error', Response::HTTP_OK); |
||
| 60 | } |
||
| 61 | |||
| 62 | //The transaction is declined on Payflow. |
||
| 63 | if ($resultCode > 0) { |
||
| 64 | $transaction->setStatus(Transaction::STATUS_DECLINED); |
||
| 65 | $entityManager->persist($transaction); |
||
| 66 | $entityManager->flush(); |
||
| 67 | return new Response('Declined by Payflow', Response::HTTP_OK); |
||
| 68 | } |
||
| 69 | |||
| 70 | //The transaction is declined by PayPal due to AVS or CSC check failed. |
||
| 71 | $responseMessage = $request->request->get('RESPMSG'); |
||
| 72 | if ($resultCode == 0 && ($responseMessage == 'AVSDECLINED' || $responseMessage == 'CSCDECLINED')) { |
||
| 73 | $transaction->setStatus(Transaction::STATUS_DECLINED); |
||
| 74 | $entityManager->persist($transaction); |
||
| 75 | $entityManager->flush(); |
||
| 76 | return new Response('Declined by Payflow', Response::HTTP_OK); |
||
| 77 | } |
||
| 78 | |||
| 79 | $transaction->setStatus(Transaction::STATUS_PAID); |
||
| 80 | |||
| 81 | if ($this->updateFeesOnAlma($transaction)) { |
||
| 82 | $transaction->setStatus(Transaction::STATUS_COMPLETED); |
||
| 83 | } else { |
||
| 84 | $transaction->setStatus(Transaction::STATUS_FAILED); |
||
| 85 | } |
||
| 86 | |||
| 87 | $entityManager->persist($transaction); |
||
| 88 | $entityManager->flush(); |
||
| 89 | |||
| 90 | return new Response("Success", Response::HTTP_OK); |
||
| 91 | } |
||
| 110 |