Passed
Push — master ( 44a14e...a34e41 )
by Will
04:40
created

ResultController::result()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 64
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 37
nc 9
nop 1
dl 0
loc 64
rs 6.9666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\Transaction;
6
use App\Service\AlmaApi;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\Routing\Annotation\Route;
11
12
class ResultController extends Controller
13
{
14
15
    private $almaApi;
16
17
    public function __construct(AlmaApi $api)
18
    {
19
        $this->almaApi = $api;
20
    }
21
22
    /**
23
     * @Route("/result", name="result")
24
     * @param Request $request
25
     * @return Response
26
     */
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
    }
92
93
    private function updateFeesOnAlma(Transaction $transaction)
94
    {
95
        $result = false;
96
97
        $fees = $transaction->getFees();
98
        foreach ($fees as $fee) {
99
            try {
100
                $this->almaApi->payUserFee($transaction->getUserId(), $fee->getFeeId(), $fee->getBalance());
101
                $result = true;
102
            } catch (\GuzzleHttp\Exception\GuzzleException $e) {
103
                echo $e->getCode() . $e->getMessage();
104
            }
105
        }
106
107
        return $result;
108
    }
109
}
110