CaptureAction::supports()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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\Action;
14
15
use BitBag\SyliusQuadPayPlugin\Action\Api\ApiAwareTrait;
16
use BitBag\SyliusQuadPayPlugin\Client\QuadPayApiClientInterface;
17
use Payum\Core\Action\ActionInterface;
18
use Payum\Core\ApiAwareInterface;
19
use Payum\Core\Bridge\Spl\ArrayObject;
20
use Payum\Core\Exception\RequestNotSupportedException;
21
use Payum\Core\GatewayAwareInterface;
22
use Payum\Core\GatewayAwareTrait;
23
use Payum\Core\Reply\HttpRedirect;
24
use Payum\Core\Request\Capture;
25
use Payum\Core\Security\TokenInterface;
26
27
final class CaptureAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
28
{
29
    use GatewayAwareTrait;
30
    use ApiAwareTrait;
31
32
    /**
33
     * {@inheritdoc}
34
     *
35
     * @param Capture $request
36
     */
37
    public function execute($request): void
38
    {
39
        RequestNotSupportedException::assertSupports($this, $request);
40
41
        $details = ArrayObject::ensureArrayObject($request->getModel());
42
43
        if (true === isset($details['orderToken'])) {
44
            return;
45
        }
46
47
        /** @var TokenInterface $token */
48
        $token = $request->getToken();
49
50
        $details['merchant'] = [
51
            'redirectConfirmUrl' => $token->getTargetUrl(),
52
            'redirectCancelUrl' => $token->getTargetUrl() . '?&' . http_build_query(['status' => QuadPayApiClientInterface::STATUS_ABANDONED]),
53
        ];
54
55
        $order = $this->quadPayApiClient->createOrder($details->getArrayCopy());
56
57
        $details['orderToken'] = $order['token'];
58
        $details['orderStatus'] = QuadPayApiClientInterface::STATUS_CREATED;
59
60
        if (isset($order['orderId'])) {
61
            $details['orderId'] = $order['orderId'];
62
        }
63
64
        throw new HttpRedirect($order['redirectUrl']);
65
    }
66
67
    public function supports($request): bool
68
    {
69
        return
70
            $request instanceof Capture &&
71
            $request->getModel() instanceof \ArrayAccess
72
        ;
73
    }
74
}
75