ObtainBankAccountAction::execute()   B
last analyzed

Complexity

Conditions 9
Paths 39

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 7.6444
c 0
b 0
f 0
cc 9
nc 39
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PH\Bundle\PayumBundle\Action\Mollie\SepaDirectDebit;
6
7
use PayHelper\Payum\Mollie\Request\Api\CreateSepaMandate;
8
use Payum\Core\Action\ActionInterface;
9
use Payum\Core\Bridge\Spl\ArrayObject;
10
use Payum\Core\Exception\LogicException;
11
use Payum\Core\Exception\RequestNotSupportedException;
12
use Payum\Core\GatewayAwareInterface;
13
use Payum\Core\GatewayAwareTrait;
14
use Payum\Core\Model\BankAccountInterface;
15
use Payum\Core\Request\GetHttpRequest;
16
use Payum\Core\Security\SensitiveValue;
17
use PH\Bundle\PayumBundle\Model\EmailAwareInterface;
18
use PH\Bundle\PayumBundle\Request\ObtainBankAccount;
19
20
class ObtainBankAccountAction implements ActionInterface, GatewayAwareInterface
21
{
22
    use GatewayAwareTrait;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function execute($request)
28
    {
29
        RequestNotSupportedException::assertSupports($this, $request);
30
31
        $model = ArrayObject::ensureArrayObject($request->getModel());
32
33
        // process confirm form if submitted
34
        $this->gateway->execute($httpRequest = new GetHttpRequest());
35
36
        if ('POST' === $httpRequest->method) {
37
            $postParams = [];
38
            parse_str($httpRequest->content, $postParams);
39
40
            if (array_key_exists('mandate_id', $postParams) && null !== $postParams['mandate_id'] && $model['mandate']['id'] === $postParams['mandate_id']) {
41
                // mandate has been confirmed by the user
42
                return;
43
            }
44
        }
45
46
        if (!$model->validateNotEmpty(['sepaIban', 'sepaHolder'], false)) {
47
            try {
48
                $obtainBankAccount = new ObtainBankAccount($request->getToken());
49
                $obtainBankAccount->setModel($request->getModel());
50
                $this->gateway->execute($obtainBankAccount);
51
52
                /** @var BankAccountInterface $bankAccount */
53
                $bankAccount = $obtainBankAccount->obtain();
54
55
                $model['sepaIban'] = SensitiveValue::ensureSensitive($bankAccount->getIban());
56
                $model['sepaHolder'] = SensitiveValue::ensureSensitive($bankAccount->getHolder());
57
58
                if (null !== $bankAccount->getBic()) {
59
                    $model['sepaBic'] = SensitiveValue::ensureSensitive($bankAccount->getBic());
60
                }
61
62
                if ($bankAccount instanceof EmailAwareInterface) {
63
                    $model['email'] = $bankAccount->getEmail();
64
                }
65
66
                $this->gateway->execute(new CreateSepaMandate($model));
67
            } catch (RequestNotSupportedException $e) {
68
                throw new LogicException('Bank account details has to be set explicitly or there has to be an action that supports ObtainBankAccount request.');
69
            }
70
        }
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 View Code Duplication
    public function supports($request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        return
79
            $request instanceof CreateSepaMandate &&
80
            $request->getModel() instanceof \ArrayAccess &&
81
            \Mollie_API_Object_Method::DIRECTDEBIT === $request->getModel()['method'] &&
82
            !isset($request->getModel()['sepaIban']) &&
83
            !isset($request->getModel()['sepaHolder']);
84
    }
85
}
86