OrderController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 39
c 1
b 0
f 0
dl 0
loc 76
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B updateAppleOrderAction() 0 69 9
A getApplePayProviderService() 0 3 1
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\Controller\Action\Shop;
13
14
use BitBag\SyliusMolliePlugin\Provider\Apple\ApplePayDirectProviderInterface;
15
use Sylius\Bundle\OrderBundle\Controller\OrderController as BaseOrderController;
16
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
17
use Sylius\Component\Payment\Model\PaymentInterface;
18
use Sylius\Component\Resource\Exception\UpdateHandlingException;
19
use Sylius\Component\Resource\ResourceActions;
20
use Symfony\Component\HttpFoundation\JsonResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\Exception\HttpException;
24
25
final class OrderController extends BaseOrderController
26
{
27
    public function updateAppleOrderAction(Request $request): Response
28
    {
29
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
30
31
        $this->isGrantedOr403($configuration, ResourceActions::UPDATE);
32
        $resource = $this->findOr404($configuration);
33
34
        /** @var ResourceControllerEvent $event */
35
        $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource);
36
37
        if ($event->isStopped() && !$configuration->isHtmlRequest()) {
38
            throw new HttpException($event->getErrorCode(), $event->getMessage());
39
        }
40
41
        if ($event->isStopped()) {
42
            $eventResponse = $event->getResponse();
43
            if (!empty($eventResponse)) {
44
                return $eventResponse;
45
            }
46
47
            return new JsonResponse([], Response::HTTP_BAD_REQUEST);
48
        }
49
50
        try {
51
            $this->getApplePayProviderService()->provideOrder($this->getCurrentCart(), $request);
52
53
            /** @var PaymentInterface $payment */
54
            $payment = $this->getCurrentCart()->getLastPayment();
0 ignored issues
show
Bug introduced by
The method getLastPayment() does not exist on Sylius\Component\Order\Model\OrderInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sylius\Component\Order\Model\Order. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            $payment = $this->getCurrentCart()->/** @scrutinizer ignore-call */ getLastPayment();
Loading history...
55
56
            if ($payment->getState() !== PaymentInterface::STATE_COMPLETED) {
57
                $response = [
58
                    'status' => 1,
59
                    'errors' => 'Payment not created',
60
                ];
61
62
                return new JsonResponse($response, Response::HTTP_BAD_REQUEST);
63
            }
64
65
            $this->resourceUpdateHandler->handle($resource, $configuration, $this->manager);
66
        } catch (UpdateHandlingException $exception) {
67
            return new JsonResponse([], Response::HTTP_BAD_REQUEST);
68
        }
69
70
        $postEvent = $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource);
71
72
        $postEventResponse = $postEvent->getResponse();
73
74
        if (!empty($postEventResponse)) {
75
            return $postEventResponse;
76
        }
77
78
        $initializeEvent = $this->eventDispatcher->dispatchInitializeEvent(ResourceActions::UPDATE, $configuration, $resource);
79
80
        $initializeEventResponse = $initializeEvent->getResponse();
81
82
        if (!empty($initializeEventResponse)) {
83
            return $initializeEventResponse;
84
        }
85
86
        $redirect = $this->redirectToRoute('sylius_shop_order_thank_you');
87
        $dataResponse['returnUrl'] = $redirect->getTargetUrl();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$dataResponse was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dataResponse = array(); before regardless.
Loading history...
88
        $dataResponse['responseToApple'] = ['status' => 0];
89
90
        $response = [
91
            'success' => true,
92
            'data' => $dataResponse,
93
        ];
94
95
        return new JsonResponse($response, Response::HTTP_OK);
96
    }
97
98
    private function getApplePayProviderService(): ApplePayDirectProviderInterface
99
    {
100
        return $this->get('bitbag_sylius_mollie_plugin.provider.apple.apple_pay_direct_provider');
101
    }
102
}
103