Completed
Push — master ( 636a3e...0d9cfe )
by Joachim
12:23
created

PaymentController::newAction()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 62
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 8.6652
c 0
b 0
f 0
cc 5
eloc 36
nc 6
nop 2

How to fix   Long Method   

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
namespace Loevgaard\DandomainAltapayBundle\Controller;
3
4
use Loevgaard\AltaPay\Payload\PaymentRequest as PaymentRequestPayload;
5
use Loevgaard\AltaPay\Payload\OrderLine as OrderLinePayload;
6
use Loevgaard\AltaPay\Payload\PaymentRequest\Config as ConfigPayload;
7
use Loevgaard\Dandomain\Pay\Handler;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
14
15
class PaymentController extends Controller {
16
    /**
17
     * @Method("POST")
18
     * @Route("/{terminal}")
19
     *
20
     * @param string $terminal
21
     * @param Request $request
22
     * @return RedirectResponse
23
     */
24
    public function newAction($terminal, Request $request)
25
    {
26
        $terminalManager = $this->container->get('loevgaard_dandomain_altapay.terminal_manager');
27
        $paymentManager = $this->container->get('loevgaard_dandomain_altapay.payment_manager');
28
        $terminalEntity = $terminalManager->findTerminalBySlug($terminal);
29
30
        if(!$terminalEntity) {
31
            throw new \RuntimeException('Terminal `'.$terminal.'` not found');
32
        }
33
34
        $handler = new Handler(
35
            $request,
36
            $this->container->getParameter('loevgaard_dandomain_altapay.shared_key_1'),
37
            $this->container->getParameter('loevgaard_dandomain_altapay.shared_key_2')
38
        );
39
40
        if(!$handler->checksumMatches()) {
41
            // @todo what should happen here?
42
            throw new \RuntimeException('Checksum mismatch. Try again');
43
        }
44
45
        $paymentRequest = $handler->getPaymentRequest();
46
47
        $paymentEntity = $paymentManager->createPaymentFromDandomainPaymentRequest($paymentRequest);
48
        $paymentManager->updatePayment($paymentEntity);
49
50
        $paymentRequestPayload = new PaymentRequestPayload(
51
            $terminalEntity->getTitle(),
52
            $paymentRequest->getOrderId(),
53
            $paymentRequest->getTotalAmount(),
54
            $paymentRequest->getCurrencySymbol()
55
        );
56
57
        foreach ($paymentRequest->getOrderLines() as $orderLine) {
58
            $orderLinePayload = new OrderLinePayload(
59
                $orderLine->getName(),
60
                $orderLine->getProductNumber(),
61
                $orderLine->getQuantity(),
62
                $orderLine->getPrice(),
63
                $orderLine->getVat()
64
            );
65
66
            $paymentRequestPayload->addOrderLine($orderLinePayload);
67
        }
68
69
        $configPayload = new ConfigPayload($this->generateUrl('loevgaard_dandomain_altapay_callback_form', [], UrlGeneratorInterface::ABSOLUTE_URL)); // @todo set the callback urls
70
        $paymentRequestPayload->setConfig($configPayload);
71
72
        //$paymentRequestPayload->setCookie(); @todo set payment request entity id in the cookie so we can use this in callbacks
73
74
75
        $altapay = $this->container->get('loevgaard_dandomain_altapay.altapay_client');
76
        $response = $altapay->createPaymentRequest($paymentRequestPayload);
77
78
        if(!$response->isSuccessful()) {
79
            // @todo fix this
80
            throw new \RuntimeException('An error occurred during payment request.');
81
        }
82
83
        echo $response->getUrl();exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method newAction() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
84
        return new RedirectResponse($response->getUrl());
0 ignored issues
show
Unused Code introduced by
return new \Symfony\Comp...e($response->getUrl()); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
85
    }
86
}