CaptureAction   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 11
dl 0
loc 100
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setApi() 0 8 2
A setGenericTokenFactory() 0 4 1
B execute() 0 47 9
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\SyliusAdyenPlugin\Action;
14
15
use BitBag\SyliusAdyenPlugin\Bridge\AdyenBridgeInterface;
16
use Payum\Core\Action\ActionInterface;
17
use Payum\Core\ApiAwareInterface;
18
use Payum\Core\Bridge\Spl\ArrayObject;
19
use Payum\Core\Exception\RequestNotSupportedException;
20
use Payum\Core\Exception\UnsupportedApiException;
21
use Payum\Core\GatewayAwareInterface;
22
use Payum\Core\GatewayAwareTrait;
23
use Payum\Core\Reply\HttpPostRedirect;
24
use Payum\Core\Request\Capture;
25
use Payum\Core\Request\GetHttpRequest;
26
use Payum\Core\Security\GenericTokenFactoryAwareInterface;
27
use Payum\Core\Security\GenericTokenFactoryInterface;
28
use Payum\Core\Security\TokenInterface;
29
30
final class CaptureAction implements ActionInterface, ApiAwareInterface, GenericTokenFactoryAwareInterface, GatewayAwareInterface
31
{
32
    use GatewayAwareTrait;
33
34
    /**
35
     * @var AdyenBridgeInterface
36
     */
37
    protected $api;
38
39
    /**
40
     * @var GenericTokenFactoryInterface
41
     */
42
    protected $tokenFactory;
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function setApi($api): void
48
    {
49
        if (false === $api instanceof AdyenBridgeInterface) {
50
            throw new UnsupportedApiException(sprintf('Not supported. Expected %s instance to be set as api.', AdyenBridgeInterface::class));
51
        }
52
53
        $this->api = $api;
54
    }
55
56
    /**
57
     * @param GenericTokenFactoryInterface $genericTokenFactory
58
     *
59
     * @return void
60
     */
61
    public function setGenericTokenFactory(GenericTokenFactoryInterface $genericTokenFactory = null): void
62
    {
63
        $this->tokenFactory = $genericTokenFactory;
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     *
69
     * @param Capture $request
70
     */
71
    public function execute($request): void
72
    {
73
        RequestNotSupportedException::assertSupports($this, $request);
74
75
        /** @var TokenInterface $token */
76
        $token = $request->getToken();
77
78
        $model = ArrayObject::ensureArrayObject($request->getModel());
79
80
        $this->gateway->execute($httpRequest = new GetHttpRequest());
81
82
        if (true === $this->api->verifyRequest($httpRequest->query, $model->toUnsafeArray())) {
83
            $model['authResult'] = $httpRequest->query['authResult'];
84
85
            if (true === isset($httpRequest->query['pspReference'])) {
86
                $model['pspReference'] = $httpRequest->query['pspReference'];
87
            }
88
89
            $model->toUnsafeArray();
90
            return;
91
        }
92
93
        $extraData = $model['extraData'] ? json_decode($model['extraData'], true) : [];
94
95
        if (false === isset($extraData['capture_token']) && $token) {
96
            $extraData['captureToken'] = $token->getHash();
97
            $extraData['refundToken'] = $this->tokenFactory->createRefundToken($token->getGatewayName(), $token->getDetails() ?? $model)->getHash();
98
            $model['resURL'] = $token->getTargetUrl();
99
        }
100
101
        if (false === isset($extraData['notify_token']) && $token && $this->tokenFactory) {
102
            $notifyToken = $this->tokenFactory->createNotifyToken(
103
                $token->getGatewayName(),
104
                $token->getDetails()
105
            );
106
107
            $extraData['notifyToken'] = $notifyToken->getHash();
108
            $model['notifyURL'] = $notifyToken->getTargetUrl();
109
        }
110
111
        $model['extraData'] = json_encode($extraData);
112
113
        throw new HttpPostRedirect(
114
            $this->api->getApiEndpoint(),
115
            $this->api->prepareFields($model->toUnsafeArray())
116
        );
117
    }
118
119
    /**
120
     * {@inheritDoc}
121
     */
122
    public function supports($request): bool
123
    {
124
        return
125
            $request instanceof Capture &&
126
            $request->getModel() instanceof \ArrayAccess
127
        ;
128
    }
129
}
130