CreateSepaMandateAction::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
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\Api;
13
14
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface;
15
use BitBag\SyliusMolliePlugin\Request\Api\CreateCustomer;
16
use BitBag\SyliusMolliePlugin\Request\Api\CreateSepaMandate;
17
use Mollie\Api\Exceptions\ApiException;
18
use Mollie\Api\Resources\Customer;
19
use Mollie\Api\Resources\Mandate;
20
use Mollie\Api\Types\MandateMethod;
21
use Payum\Core\Action\ActionInterface;
22
use Payum\Core\ApiAwareInterface;
23
use Payum\Core\Bridge\Spl\ArrayObject;
24
use Payum\Core\Exception\RequestNotSupportedException;
25
use Payum\Core\GatewayAwareInterface;
26
use Payum\Core\GatewayAwareTrait;
27
use Symfony\Component\HttpFoundation\Session\SessionInterface;
28
29
final class CreateSepaMandateAction extends BaseApiAwareAction implements ActionInterface, GatewayAwareInterface, ApiAwareInterface
30
{
31
    use GatewayAwareTrait;
32
33
    /** @var SessionInterface */
34
    private $session;
35
36
    /** @var MollieLoggerActionInterface */
37
    private $loggerAction;
38
39
    public function __construct(SessionInterface $session, MollieLoggerActionInterface $loggerAction)
40
    {
41
        $this->session = $session;
42
        $this->loggerAction = $loggerAction;
43
    }
44
45
    /** @param CreateSepaMandate $request */
46
    public function execute($request): void
47
    {
48
        RequestNotSupportedException::assertSupports($this, $request);
49
50
        if (null === $directDebitData = $this->session->get('mollie_direct_debit_data', null)) {
51
            return;
52
        }
53
54
        $model = ArrayObject::ensureArrayObject($request->getModel());
55
56
        $this->gateway->execute(new CreateCustomer($model));
57
58
        try {
59
            /** @var Customer $customer */
60
            $customer = $this->mollieApiClient->customers->get($model['customer_mollie_id']);
61
        } catch (\Exception $e) {
62
            $this->loggerAction->addNegativeLog(sprintf('Error with get customer from mollie with: %s', $e->getMessage()));
63
64
            throw new ApiException(sprintf('Error with get customer from mollie with: %s', $e->getMessage()));
65
        }
66
67
        /** @var Mandate $mandate */
68
        $mandate = $customer->createMandate([
69
            'consumerAccount' => $directDebitData['iban'] ?? null,
70
            'consumerName' => $directDebitData['consumerName'] ?? null,
71
            'method' => MandateMethod::DIRECTDEBIT,
72
        ]);
73
74
        $this->loggerAction->addLog(sprintf('Create mandate with id %s', $mandate->id));
75
76
        $model['mandate_mollie_id'] = $mandate->id;
77
    }
78
79
    public function supports($request): bool
80
    {
81
        return
82
            $request instanceof CreateSepaMandate &&
83
            $request->getModel() instanceof \ArrayAccess;
84
    }
85
}
86