CaptureAction   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 5 2
A __construct() 0 4 1
A setGenericTokenFactory() 0 3 1
A execute() 0 45 4
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusMultiSafepayPlugin\Action;
12
13
use BitBag\SyliusMultiSafepayPlugin\Action\Api\ApiAwareTrait;
14
use BitBag\SyliusMultiSafepayPlugin\ApiClient\MultiSafepayApiClientInterface;
15
use Payum\Core\Action\ActionInterface;
16
use Payum\Core\ApiAwareInterface;
17
use Payum\Core\Bridge\Spl\ArrayObject;
18
use Payum\Core\Exception\RequestNotSupportedException;
19
use Payum\Core\Exception\RuntimeException;
20
use Payum\Core\GatewayAwareInterface;
21
use Payum\Core\GatewayAwareTrait;
22
use Payum\Core\Reply\HttpRedirect;
23
use Payum\Core\Request\Capture;
24
use Payum\Core\Security\GenericTokenFactoryAwareInterface;
25
use Payum\Core\Security\GenericTokenFactoryInterface;
26
use Payum\Core\Security\TokenInterface;
27
use Psr\Log\LoggerInterface;
28
29
final class CaptureAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface, GenericTokenFactoryAwareInterface
30
{
31
    use GatewayAwareTrait, ApiAwareTrait;
32
33
    /** @var GenericTokenFactoryInterface|null */
34
    private $tokenFactory;
35
36
    /** @var LoggerInterface */
37
    private $logger;
38
39
    /** @var string|null */
40
    private $notificationUrlHostname;
41
42
    public function __construct(LoggerInterface $logger, string $notificationUrlHostname = null)
43
    {
44
        $this->logger = $logger;
45
        $this->notificationUrlHostname = $notificationUrlHostname;
46
    }
47
48
    public function setGenericTokenFactory(GenericTokenFactoryInterface $genericTokenFactory = null): void
49
    {
50
        $this->tokenFactory = $genericTokenFactory;
51
    }
52
53
    public function execute($request): void
54
    {
55
        /** @param Capture $request */
56
        RequestNotSupportedException::assertSupports($this, $request);
57
58
        $details = ArrayObject::ensureArrayObject($request->getModel());
59
60
        if (isset($details['orderId'])) {
61
            return;
62
        }
63
64
        /** @var TokenInterface $token */
65
        $token = $request->getToken();
66
67
        if (null === $this->tokenFactory) {
68
            throw new RuntimeException();
69
        }
70
71
        $notifyToken = $this->tokenFactory->createNotifyToken($token->getGatewayName(), $token->getDetails());
72
73
        $notificationUrl = $notifyToken->getTargetUrl();
74
75
        if (!empty($this->notificationUrlHostname)) {
76
            $parseNotificationUrl = parse_url($notificationUrl);
77
78
            $notificationUrl = sprintf('%s%s', $this->notificationUrlHostname, $parseNotificationUrl['path']);
79
        }
80
81
        $paymentData = $details['paymentData'];
82
83
        $paymentData['payment_options'] = [
84
            'notification_url' => $notificationUrl,
85
            'redirect_url' => $token->getTargetUrl(),
86
            'cancel_url' => sprintf('%s?type=%s', $token->getTargetUrl(), MultiSafepayApiClientInterface::STATUS_CANCEL),
87
        ];
88
89
        $details['paymentData'] = $paymentData;
90
91
        $order = $this->multiSafepayApiClient->createPayment($paymentData);
92
93
        $details['status'] = MultiSafepayApiClientInterface::STATUS_INITIALIZED;
94
        $details['paymentLink'] = $order->getPaymentLink();
95
        $details['orderId'] = $order->data->order_id;
96
97
        throw new HttpRedirect($order->getPaymentLink());
98
    }
99
100
    public function supports($request): bool
101
    {
102
        return
103
            $request instanceof Capture &&
104
            $request->getModel() instanceof \ArrayAccess
105
        ;
106
    }
107
}
108