Passed
Push — master ( a33092...6248c3 )
by US
04:42
created

NotifyAction::supports()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
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
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusMultiSafepayPlugin\Action;
14
15
use BitBag\SyliusMultiSafepayPlugin\Action\Api\ApiAwareTrait;
16
use BitBag\SyliusMultiSafepayPlugin\MultiSafepayGatewayFactory;
17
use Payum\Core\Action\ActionInterface;
18
use Payum\Core\ApiAwareInterface;
19
use Payum\Core\Bridge\Spl\ArrayObject;
20
use Payum\Core\GatewayAwareInterface;
21
use Payum\Core\GatewayAwareTrait;
22
use Payum\Core\Reply\HttpResponse;
23
use Payum\Core\Request\GetHttpRequest;
24
use Payum\Core\Request\Notify;
25
use Psr\Log\LoggerInterface;
26
use SM\Factory\FactoryInterface;
27
use Sylius\Component\Core\Model\OrderInterface;
28
use Sylius\Component\Core\Model\PaymentInterface;
29
use Sylius\Component\Core\Model\PaymentMethodInterface;
30
use Sylius\Component\Payment\PaymentTransitions;
31
32
final class NotifyAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
33
{
34
    use GatewayAwareTrait, ApiAwareTrait;
35
36
    /** @var LoggerInterface */
37
    private $logger;
38
39
    /** @var FactoryInterface */
40
    private $stateMachineFactory;
41
42
    public function __construct(LoggerInterface $logger, FactoryInterface $stateMachineFactory)
43
    {
44
        $this->logger = $logger;
45
        $this->stateMachineFactory = $stateMachineFactory;
46
    }
47
48
    public function execute($request): void
49
    {
50
        /** @var $request Notify */
51
        $details = ArrayObject::ensureArrayObject($request->getModel());
52
53
        $this->gateway->execute($httpRequest = new GetHttpRequest());
54
55
        if (!isset($httpRequest->query['transactionid'])) {
56
            throw new HttpResponse(null, 400);
57
        }
58
59
        /** @var PaymentInterface $payment */
60
        $payment = $request->getFirstModel();
61
62
        /** @var OrderInterface $order */
63
        $order = $payment->getOrder();
64
65
        /** @var PaymentInterface $item */
66
        foreach ($order->getPayments() as $item) {
67
            /** @var PaymentMethodInterface $method */
68
            $method = $item->getMethod();
69
70
            if (
71
                PaymentInterface::STATE_NEW === $item->getState() &&
72
                MultiSafepayGatewayFactory::FACTORY_NAME === $method->getGatewayConfig()->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

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