StatusAction   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 8
dl 0
loc 76
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setApi() 0 8 2
B execute() 0 46 7
A supports() 0 7 2
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\SyliusPrzelewy24Plugin\Action;
14
15
use BitBag\SyliusPrzelewy24Plugin\Bridge\Przelewy24BridgeInterface;
16
use Payum\Core\Action\ActionInterface;
17
use Payum\Core\ApiAwareInterface;
18
use Payum\Core\Exception\RequestNotSupportedException;
19
use Payum\Core\Exception\UnsupportedApiException;
20
use Payum\Core\GatewayAwareInterface;
21
use Payum\Core\GatewayAwareTrait;
22
use Payum\Core\Request\GetHttpRequest;
23
use Payum\Core\Request\GetStatusInterface;
24
use Sylius\Component\Core\Model\PaymentInterface;
25
26
final class StatusAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
27
{
28
    use GatewayAwareTrait;
29
30
    /** @var Przelewy24BridgeInterface */
31
    private $przelewy24Bridge;
32
33
    public function __construct(Przelewy24BridgeInterface $przelewy24Bridge)
34
    {
35
        $this->przelewy24Bridge = $przelewy24Bridge;
36
    }
37
38
    public function setApi($api): void
39
    {
40
        if (false === is_array($api)) {
41
            throw new UnsupportedApiException('Not supported.Expected to be set as array.');
42
        }
43
44
        $this->przelewy24Bridge->setAuthorizationData($api['merchant_id'], $api['crc_key'], $api['environment']);
45
    }
46
47
    public function execute($request): void
48
    {
49
        RequestNotSupportedException::assertSupports($this, $request);
50
51
        /** @var PaymentInterface $payment */
52
        $payment = $request->getModel();
53
54
        $details = $payment->getDetails();
55
56
        $this->gateway->execute($httpRequest = new GetHttpRequest());
57
58
        if (isset($httpRequest->query['status']) &&
59
            $httpRequest->query['status'] === Przelewy24BridgeInterface::CANCELLED_STATUS
60
        ) {
61
            $details['p24_status'] = Przelewy24BridgeInterface::CANCELLED_STATUS;
62
            $request->markCanceled();
63
64
            return;
65
        }
66
67
        if (false === isset($details['p24_status'])) {
68
            $request->markNew();
69
70
            return;
71
        }
72
73
        if (Przelewy24BridgeInterface::COMPLETED_STATUS === $details['p24_status']) {
74
            $request->markCaptured();
75
76
            return;
77
        }
78
79
        if (Przelewy24BridgeInterface::CREATED_STATUS === $details['p24_status']) {
80
            $request->markPending();
81
82
            return;
83
        }
84
85
        if (Przelewy24BridgeInterface::FAILED_STATUS === $details['p24_status']) {
86
            $request->markFailed();
87
88
            return;
89
        }
90
91
        $request->markUnknown();
92
    }
93
94
    public function supports($request): bool
95
    {
96
        return
97
            $request instanceof GetStatusInterface &&
98
            $request->getModel() instanceof PaymentInterface
99
        ;
100
    }
101
}
102