Completed
Push — master ( 0d9cfe...073f2f )
by Joachim
12:30
created

CallbackController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 0
loc 150
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A formAction() 0 8 1
A okAction() 0 4 1
A failAction() 0 4 1
A redirectAction() 0 4 1
A openAction() 0 4 1
A notificationAction() 0 5 1
A verifyOrderAction() 0 5 1
A handleCallback() 0 15 3
A getPaymentFromRequest() 0 12 2
A logCallback() 0 8 1
A getPaymentManager() 0 3 1
1
<?php
2
namespace Loevgaard\DandomainAltapayBundle\Controller;
3
4
use Loevgaard\DandomainAltapayBundle\Entity\PaymentInterface;
5
use Loevgaard\DandomainAltapayBundle\Manager\PaymentManager;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
12
/**
13
 * @Route("/callback")
14
 */
15
class CallbackController extends Controller {
16
    /**
17
     * @Method("POST")
18
     * @Route("/form", name="loevgaard_dandomain_altapay_callback_form")
19
     *
20
     * @param Request $request
21
     * @return Response
22
     */
23
    public function formAction(Request $request)
24
    {
25
        $payment = $this->handleCallback($request);
26
27
        return $this->render('@LoevgaardDandomainAltapay/callback/form.html.twig', [
28
            'payment' => $payment
29
        ]);
30
    }
31
32
    /**
33
     * @Method("POST")
34
     * @Route("/ok", name="loevgaard_dandomain_altapay_callback_ok")
35
     *
36
     * @param Request $request
37
     * @return Response
38
     */
39
    public function okAction(Request $request)
40
    {
41
        $payment = $this->handleCallback($request);
0 ignored issues
show
Unused Code introduced by
$payment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
42
    }
43
44
    /**
45
     * @Method("POST")
46
     * @Route("/fail", name="loevgaard_dandomain_altapay_callback_fail")
47
     *
48
     * @param Request $request
49
     * @return Response
50
     */
51
    public function failAction(Request $request)
52
    {
53
        $payment = $this->handleCallback($request);
0 ignored issues
show
Unused Code introduced by
$payment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
54
    }
55
56
    /**
57
     * @Method("POST")
58
     * @Route("/redirect", name="loevgaard_dandomain_altapay_callback_redirect")
59
     *
60
     * @param Request $request
61
     * @return Response
62
     */
63
    public function redirectAction(Request $request)
64
    {
65
        $payment = $this->handleCallback($request);
0 ignored issues
show
Unused Code introduced by
$payment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
    }
67
68
    /**
69
     * @Method("POST")
70
     * @Route("/open", name="loevgaard_dandomain_altapay_callback_open")
71
     *
72
     * @param Request $request
73
     * @return Response
74
     */
75
    public function openAction(Request $request)
76
    {
77
        $payment = $this->handleCallback($request);
0 ignored issues
show
Unused Code introduced by
$payment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
    }
79
80
    /**
81
     * @Method("POST")
82
     * @Route("/notification", name="loevgaard_dandomain_altapay_callback_notification")
83
     *
84
     * @param Request $request
85
     * @return Response
86
     */
87
    public function notificationAction(Request $request)
88
    {
89
        $payment = $this->handleCallback($request);
0 ignored issues
show
Unused Code introduced by
$payment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
        return new Response();
91
    }
92
93
    /**
94
     * @Method("POST")
95
     * @Route("/verify-order", name="loevgaard_dandomain_altapay_callback_verify_order")
96
     *
97
     * @param Request $request
98
     * @return Response
99
     */
100
    public function verifyOrderAction(Request $request)
101
    {
102
        $payment = $this->handleCallback($request);
0 ignored issues
show
Unused Code introduced by
$payment is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
103
        return new Response();
104
    }
105
106
    /**
107
     * @param Request $request
108
     * @return PaymentInterface
109
     */
110
    protected function handleCallback(Request $request) {
111
        $payment = $this->getPaymentFromRequest($request);
112
        $callbackManager = $this->container->get('loevgaard_dandomain_altapay.callback_manager');
113
        $callback = $callbackManager->createCallbackFromRequest($request);
114
        $callback->setPayment($payment);
115
116
        $callbackManager->updateCallback($callback);
117
118
        $allowedIps = $this->container->getParameter('loevgaard_dandomain_altapay.altapay_ips');
119
        if($this->container->get('kernel')->getEnvironment() === 'prod' && !in_array($request->getClientIp(), $allowedIps)) {
120
            throw new \RuntimeException('IP `'.$request->getClientIp().'` is not an allowed IP.');
121
        }
122
123
        return $payment;
124
    }
125
126
    /**
127
     * @param Request $request
128
     * @return PaymentInterface
129
     */
130
    protected function getPaymentFromRequest(Request $request) {
131
        $paymentId = $request->cookies->getInt('payment_id');
132
        $paymentManager = $this->getPaymentManager();
133
        $payment = $paymentManager->findPaymentById($paymentId);
134
135
        if(!$payment) {
136
            // @todo fix exception
137
            throw new \RuntimeException('Payment '.$paymentId.' does not exist');
138
        }
139
140
        return $payment;
141
    }
142
143
    /**
144
     * Add a callback request to the payment for logging purposes
145
     *
146
     * @param PaymentInterface $payment
147
     * @param Request $request
148
     */
149
    protected function logCallback($payment, Request $request) {
150
        $callbackManager = $this->container->get('loevgaard_dandomain_altapay.callback_manager');
151
        $callback = $callbackManager->createCallback();
152
        $callback->setPayment($payment)
153
            ->setRequest((string)$request);
154
155
        $callbackManager->updateCallback($callback);
156
    }
157
158
    /**
159
     * @return PaymentManager
160
     */
161
    protected function getPaymentManager() {
162
        return $this->container->get('loevgaard_dandomain_altapay.payment_manager');
163
    }
164
}