PaymentStateResolver::resolve()   B
last analyzed

Complexity

Conditions 10
Paths 24

Size

Total Lines 67
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 40
nc 24
nop 1
dl 0
loc 67
rs 7.6666
c 0
b 0
f 0

How to fix   Long Method    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 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\SyliusQuadPayPlugin\Resolver;
14
15
use BitBag\SyliusQuadPayPlugin\Client\QuadPayApiClientInterface;
16
use BitBag\SyliusQuadPayPlugin\QuadPayGatewayFactory;
17
use Doctrine\ORM\EntityManagerInterface;
18
use GuzzleHttp\Exception\ClientException;
19
use SM\Factory\FactoryInterface;
20
use SM\StateMachine\StateMachineInterface;
21
use Sylius\Component\Core\Model\PaymentInterface;
22
use Sylius\Component\Core\Model\PaymentMethodInterface;
23
use Sylius\Component\Payment\PaymentTransitions;
24
use Symfony\Component\HttpFoundation\Response;
25
26
final class PaymentStateResolver implements PaymentStateResolverInterface
27
{
28
    /** @var FactoryInterface */
29
    private $stateMachineFactory;
30
31
    /** @var QuadPayApiClientInterface */
32
    private $quadPayApiClient;
33
34
    /** @var EntityManagerInterface */
35
    private $paymentEntityManager;
36
37
    public function __construct(
38
        FactoryInterface $stateMachineFactory,
39
        QuadPayApiClientInterface $quadPayApiClient,
40
        EntityManagerInterface $paymentEntityManager
41
    ) {
42
        $this->stateMachineFactory = $stateMachineFactory;
43
        $this->quadPayApiClient = $quadPayApiClient;
44
        $this->paymentEntityManager = $paymentEntityManager;
45
    }
46
47
    public function resolve(PaymentInterface $payment): void
48
    {
49
        /** @var PaymentMethodInterface $paymentMethod */
50
        $paymentMethod = $payment->getMethod();
51
52
        if (QuadPayGatewayFactory::FACTORY_NAME !== $paymentMethod->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

52
        if (QuadPayGatewayFactory::FACTORY_NAME !== /** @scrutinizer ignore-deprecated */ $paymentMethod->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...
53
            return;
54
        }
55
56
        $details = $payment->getDetails();
57
58
        if (false === isset($details['orderToken']) && false === isset($details['orderId'])) {
59
            return;
60
        }
61
62
        $gatewayConfig = $paymentMethod->getGatewayConfig()->getConfig();
63
64
        $this->quadPayApiClient->setConfig(
65
            $gatewayConfig['clientId'],
66
            $gatewayConfig['clientSecret'],
67
            $gatewayConfig['apiEndpoint'],
68
            $gatewayConfig['authTokenEndpoint'],
69
            $gatewayConfig['apiAudience']
70
        );
71
72
        $paymentStateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH);
73
74
        try {
75
            if (isset($details['orderId'])) {
76
                $order = $this->quadPayApiClient->getOrderById($details['orderId']);
77
            } else {
78
                $order = $this->quadPayApiClient->getOrderByToken($details['orderToken']);
79
            }
80
81
            $details['orderStatus'] = strtolower($order['orderStatus']);
82
83
            $payment->setDetails($details);
84
        } catch (ClientException $clientException) {
85
            if (Response::HTTP_NOT_FOUND === $clientException->getCode()) {
86
                $this->applyFail($paymentStateMachine);
87
88
                $this->paymentEntityManager->flush();
89
            }
90
91
            throw $clientException;
92
        }
93
94
        switch ($details['orderStatus']) {
95
            case QuadPayApiClientInterface::STATUS_CREATED:
96
                $this->applyProcess($paymentStateMachine);
97
98
                break;
99
            case QuadPayApiClientInterface::STATUS_ABANDONED:
100
                $this->applyCancel($paymentStateMachine);
101
102
                break;
103
            case QuadPayApiClientInterface::STATUS_APPROVED:
104
                $this->applyComplete($paymentStateMachine);
105
106
                break;
107
            default:
108
                $this->applyFail($paymentStateMachine);
109
110
                break;
111
        }
112
113
        $this->paymentEntityManager->flush();
114
    }
115
116
    private function applyProcess(StateMachineInterface $paymentStateMachine): void
117
    {
118
        if ($paymentStateMachine->can(PaymentTransitions::TRANSITION_PROCESS)) {
119
            $paymentStateMachine->apply(PaymentTransitions::TRANSITION_PROCESS);
120
        }
121
    }
122
123
    private function applyCancel(StateMachineInterface $paymentStateMachine): void
124
    {
125
        if ($paymentStateMachine->can(PaymentTransitions::TRANSITION_CANCEL)) {
126
            $paymentStateMachine->apply(PaymentTransitions::TRANSITION_CANCEL);
127
        }
128
    }
129
130
    private function applyComplete(StateMachineInterface $paymentStateMachine): void
131
    {
132
        if ($paymentStateMachine->can(PaymentTransitions::TRANSITION_COMPLETE)) {
133
            $paymentStateMachine->apply(PaymentTransitions::TRANSITION_COMPLETE);
134
        }
135
    }
136
137
    private function applyFail(StateMachineInterface $paymentStateMachine): void
138
    {
139
        if ($paymentStateMachine->can(PaymentTransitions::TRANSITION_FAIL)) {
140
            $paymentStateMachine->apply(PaymentTransitions::TRANSITION_FAIL);
141
        }
142
    }
143
}
144