ObtainBankAccountAction   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 11
dl 0
loc 98
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setRequestStack() 0 4 1
B execute() 0 42 6
A supports() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PH\Bundle\PayumBundle\Action;
6
7
use Payum\Core\Action\ActionInterface;
8
use Payum\Core\Bridge\Symfony\Reply\HttpResponse;
9
use Payum\Core\Exception\LogicException;
10
use Payum\Core\Exception\RequestNotSupportedException;
11
use Payum\Core\GatewayAwareInterface;
12
use Payum\Core\GatewayAwareTrait;
13
use Payum\Core\Model\BankAccountInterface;
14
use Payum\Core\Request\RenderTemplate;
15
use PH\Bundle\PayumBundle\Factory\BankAccountFormFactoryInterface;
16
use PH\Bundle\PayumBundle\Request\ObtainBankAccount;
17
use Symfony\Component\HttpFoundation\RequestStack;
18
use Symfony\Component\HttpFoundation\Response;
19
20
class ObtainBankAccountAction implements ActionInterface, GatewayAwareInterface
21
{
22
    use GatewayAwareTrait;
23
24
    /**
25
     * @var BankAccountFormFactoryInterface
26
     */
27
    protected $bankAccountFormFactory;
28
29
    /**
30
     * @var RequestStack
31
     */
32
    protected $httpRequestStack;
33
34
    /**
35
     * @var string
36
     */
37
    protected $templateName;
38
39
    /**
40
     * ObtainBankAccountAction constructor.
41
     *
42
     * @param BankAccountFormFactoryInterface $bankAccountFormFactory
43
     * @param string                          $templateName
44
     */
45
    public function __construct(BankAccountFormFactoryInterface $bankAccountFormFactory, string $templateName)
46
    {
47
        $this->bankAccountFormFactory = $bankAccountFormFactory;
48
        $this->templateName = $templateName;
49
    }
50
51
    /**
52
     * @param RequestStack|null $requestStack
53
     */
54
    public function setRequestStack(?RequestStack $requestStack)
55
    {
56
        $this->httpRequestStack = $requestStack;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     *
62
     * @param ObtainBankAccount $request
63
     *
64
     * @throws \Payum\Core\Bridge\Symfony\Reply\HttpResponse
65
     * @throws \Payum\Core\Exception\LogicException
66
     */
67
    public function execute($request)
68
    {
69
        RequestNotSupportedException::assertSupports($this, $request);
70
        $httpRequest = $this->httpRequestStack->getMasterRequest();
71
72
        if (false == $httpRequest) {
73
            throw new LogicException('The action can be run only when http request is set.');
74
        }
75
76
        if (!isset($request->getModel()['type'])) {
77
            throw new LogicException('The type is not defined.');
78
        }
79
80
        $form = $this->bankAccountFormFactory->createBankAccountForm($request->getModel()['type']);
81
82
        $form->handleRequest($httpRequest);
83
        if ($form->isSubmitted()) {
84
            /** @var BankAccountInterface $bankAccount */
85
            $bankAccount = $form->getData();
86
87
            if ($form->isValid()) {
88
                $request->set($bankAccount);
89
90
                return;
91
            }
92
        }
93
94
        $renderTemplate = new RenderTemplate($this->templateName, [
95
            'model' => $request->getModel(),
96
            'firstModel' => $request->getFirstModel(),
97
            'form' => $form->createView(),
98
            'actionUrl' => $request->getToken() ? $request->getToken()->getTargetUrl() : null,
99
        ]);
100
101
        $this->gateway->execute($renderTemplate);
102
103
        throw new HttpResponse(new Response($renderTemplate->getResult(), 200, [
104
            'Cache-Control' => 'no-store, no-cache, max-age=0, post-check=0, pre-check=0',
105
            'X-Status-Code' => 200,
106
            'Pragma' => 'no-cache',
107
        ]));
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function supports($request)
114
    {
115
        return $request instanceof ObtainBankAccount;
116
    }
117
}
118