NotifyAction::execute()   B
last analyzed

Complexity

Conditions 10
Paths 7

Size

Total Lines 49
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 23
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 49
rs 7.6666

How to fix   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
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusMultiSafepayPlugin\Action;
12
13
use BitBag\SyliusMultiSafepayPlugin\Action\Api\ApiAwareTrait;
14
use BitBag\SyliusMultiSafepayPlugin\MultiSafepayGatewayFactory;
15
use Payum\Core\Action\ActionInterface;
16
use Payum\Core\ApiAwareInterface;
17
use Payum\Core\Bridge\Spl\ArrayObject;
18
use Payum\Core\GatewayAwareInterface;
19
use Payum\Core\GatewayAwareTrait;
20
use Payum\Core\Reply\HttpResponse;
21
use Payum\Core\Request\GetHttpRequest;
22
use Payum\Core\Request\Notify;
23
use Psr\Log\LoggerInterface;
24
use SM\Factory\FactoryInterface;
25
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
26
use Sylius\Component\Core\Model\OrderInterface;
27
use Sylius\Component\Core\Model\PaymentInterface;
28
use Sylius\Component\Core\Model\PaymentMethodInterface;
29
use Sylius\Component\Payment\PaymentTransitions;
30
31
final class NotifyAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
32
{
33
    use GatewayAwareTrait, ApiAwareTrait;
34
35
    /** @var LoggerInterface */
36
    private $logger;
37
38
    /** @var FactoryInterface */
39
    private $stateMachineFactory;
40
41
    public function __construct(LoggerInterface $logger, FactoryInterface $stateMachineFactory)
42
    {
43
        $this->logger = $logger;
44
        $this->stateMachineFactory = $stateMachineFactory;
45
    }
46
47
    public function execute($request): void
48
    {
49
        /** @param Notify $request */
50
        $details = ArrayObject::ensureArrayObject($request->getModel());
51
52
        $this->gateway->execute($httpRequest = new GetHttpRequest());
53
54
        if (!isset($httpRequest->query['transactionid'])) {
55
            throw new HttpResponse('', 400);
56
        }
57
58
        /** @var PaymentInterface $payment */
59
        $payment = $request->getFirstModel();
60
61
        /** @var OrderInterface $order */
62
        $order = $payment->getOrder();
63
64
        /** @var PaymentInterface $item */
65
        foreach ($order->getPayments() as $item) {
66
            /** @var PaymentMethodInterface $method */
67
            $method = $item->getMethod();
68
69
            /** @var GatewayConfigInterface $gatewayConfig */
70
            $gatewayConfig = $method->getGatewayConfig();
71
72
            if (
73
                PaymentInterface::STATE_NEW === $item->getState() &&
74
                MultiSafepayGatewayFactory::FACTORY_NAME === $gatewayConfig->getFactoryName() &&
0 ignored issues
show
Deprecated Code introduced by
The function Payum\Core\Model\Gateway...rface::getFactoryName() has been deprecated: since 1.3.3 will be removed in 2.0. set factory option inside the config ( Ignorable by Annotation )

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

74
                MultiSafepayGatewayFactory::FACTORY_NAME === /** @scrutinizer ignore-deprecated */ $gatewayConfig->getFactoryName() &&

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
75
                $payment !== $item
76
            ) {
77
                $order->removePayment($item);
78
            }
79
        }
80
81
        $paymentStateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH);
82
83
        $orderData = $this->multiSafepayApiClient->getOrderById($details['orderId']);
84
85
        if (
86
            (PaymentInterface::STATE_FAILED === $payment->getState() || PaymentInterface::STATE_CANCELLED === $payment->getState()) &&
87
            $paymentStateMachine->can(PaymentTransitions::TRANSITION_PROCESS) &&
88
            $this->multiSafepayApiClient->isPaymentActive($orderData->status)
89
        ) {
90
            $paymentStateMachine->apply(PaymentTransitions::TRANSITION_PROCESS);
91
        }
92
93
        $details['status'] = $orderData->status;
94
95
        throw new HttpResponse('OK', 200);
96
    }
97
98
    public function supports($request): bool
99
    {
100
        return
101
            $request instanceof Notify &&
102
            $request->getModel() instanceof \ArrayAccess
103
        ;
104
    }
105
}
106