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
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\Action;
13
14
use BitBag\SyliusMolliePlugin\Action\Api\BaseApiAwareAction;
15
use BitBag\SyliusMolliePlugin\Payments\PaymentTerms\Options;
16
use BitBag\SyliusMolliePlugin\Request\Api\CreateOrder;
17
use BitBag\SyliusMolliePlugin\Request\Api\CreatePayment;
18
use BitBag\SyliusMolliePlugin\Request\Api\CreateRecurringSubscription;
19
use BitBag\SyliusMolliePlugin\Request\Api\CreateSepaMandate;
20
use Payum\Core\Bridge\Spl\ArrayObject;
21
use Payum\Core\Exception\RequestNotSupportedException;
22
use Payum\Core\Exception\RuntimeException;
23
use Payum\Core\GatewayAwareTrait;
24
use Payum\Core\Request\Capture;
25
use Payum\Core\Security\GenericTokenFactoryInterface;
26
use Payum\Core\Security\TokenInterface;
27
28
final class CaptureAction extends BaseApiAwareAction implements CaptureActionInterface
29
{
30
    use GatewayAwareTrait;
31
32
    /** @var GenericTokenFactoryInterface|null */
33
    private $tokenFactory;
34
35
    public function setGenericTokenFactory(GenericTokenFactoryInterface $genericTokenFactory = null): void
36
    {
37
        $this->tokenFactory = $genericTokenFactory;
38
    }
39
40
    /** @param Capture $request */
41
    public function execute($request): void
42
    {
43
        RequestNotSupportedException::assertSupports($this, $request);
44
45
        $details = ArrayObject::ensureArrayObject($request->getModel());
46
47
        if (true === isset($details['payment_mollie_id']) ||
48
            true === isset($details['subscription_mollie_id']) ||
49
            true === isset($details['order_mollie_id'])) {
50
            return;
51
        }
52
53
        /** @var TokenInterface $token */
54
        $token = $request->getToken();
55
56
        if (null === $this->tokenFactory) {
57
            throw new RuntimeException();
58
        }
59
60
        $notifyToken = $this->tokenFactory->createNotifyToken($token->getGatewayName(), $token->getDetails());
61
        $refundToken = $this->tokenFactory->createRefundToken($token->getGatewayName(), $token->getDetails());
62
63
        $details['webhookUrl'] = $notifyToken->getTargetUrl();
64
        $details['backurl'] = $token->getTargetUrl();
65
66
        if (true === $this->mollieApiClient->isRecurringSubscription()) {
67
            $cancelToken = $this->tokenFactory->createToken(
68
                $token->getGatewayName(),
69
                $token->getDetails(),
70
                'bitbag_sylius_mollie_plugin_cancel_subscription_mollie',
71
                ['orderId' => $details['metadata']['order_id']]
72
            );
73
74
            $details['cancel_token'] = $cancelToken->getHash();
75
76
            $this->gateway->execute(new CreateSepaMandate($details));
77
            $this->gateway->execute(new CreateRecurringSubscription($details));
78
        }
79
80
        if (false === $this->mollieApiClient->isRecurringSubscription()) {
81
            $metadata = $details['metadata'];
82
            $metadata['refund_token'] = $refundToken->getHash();
83
            $details['metadata'] = $metadata;
84
85
            if (isset($details['metadata']['methodType']) && $details['metadata']['methodType'] === Options::PAYMENT_API) {
86
                if (in_array($details['metadata']['molliePaymentMethods'], Options::getOnlyOrderAPIMethods())) {
87
                    throw new \sprintf(
0 ignored issues
show
Bug introduced by
The type sprintf was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
88
                        'Method %s is not allowed to use %s',
89
                        $details['metadata']['molliePaymentMethods'],
90
                        Options::PAYMENT_API
91
                    );
92
                }
93
94
                $this->gateway->execute(new CreatePayment($details));
95
            }
96
97
            if (isset($details['metadata']['methodType']) && $details['metadata']['methodType'] === Options::ORDER_API) {
98
                $this->gateway->execute(new CreateOrder($details));
99
            }
100
        }
101
    }
102
103
    public function supports($request): bool
104
    {
105
        return
106
            $request instanceof Capture &&
107
            $request->getModel() instanceof \ArrayAccess
108
            ;
109
    }
110
}
111