Completed
Push — middleware-wip ( 106169...2ebda9 )
by Romain
02:43
created

FormController::dispatchMiddlewares()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 10
nc 4
nop 0
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Controller;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Form\FormInterface;
18
use Romm\Formz\Form\FormObjectFactory;
19
use Romm\Formz\Middleware\State\MiddlewareState;
20
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
21
use TYPO3\CMS\Extbase\Mvc\Controller\Argument;
22
use TYPO3\CMS\Extbase\Property\PropertyMapper;
23
use TYPO3\CMS\Extbase\Security\Cryptography\HashService;
24
25
class FormController extends ActionController
26
{
27
    /**
28
     * @var FormzControllerContext
29
     */
30
    protected $formzControllerContext;
31
32
    /**
33
     * @var HashService
34
     */
35
    protected $hashService;
36
37
    /**
38
     * Main action used to dispatch the request properly, depending on FormZ
39
     * configuration.
40
     */
41
    public function processFormAction()
42
    {
43
        $tt = new \TYPO3\CMS\Core\TimeTracker\TimeTracker();
44
        $tt->start();
45
46
        $this->dispatchMiddlewares();
47
48
        $this->formzControllerContext->setDispatched(true);
49
50
        $request = $this->formzControllerContext->getRequest();
51
52
        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($tt->getDifferenceToStarttime(), 'TIME');
53
        $this->forward(
54
            $request->getControllerActionName(),
55
            $request->getControllerName(),
56
            $request->getControllerExtensionName(),
57
            $request->getArguments()
58
        );
59
    }
60
61
    protected function dispatchMiddlewares()
62
    {
63
        /** @var FormObjectFactory $formObjectFactory */
64
        $formObjectFactory = Core::instantiate(FormObjectFactory::class);
65
66
        /** @var Argument $argument */
67
        foreach ($this->formzControllerContext->getArguments() as $argument) {
68
            $type = $argument->getDataType();
69
70
            if (class_exists($type)
71
                && in_array(FormInterface::class, class_implements($type))
72
            ) {
73
                $formObject = $formObjectFactory->getInstanceFromClassName($type, $argument->getName());
74
75
                if (false === $formObject->getConfigurationValidationResult()->hasErrors()) {
76
                    /** @var MiddlewareState $middlewareFactory */
77
                    $middlewareFactory = Core::instantiate(MiddlewareState::class, $formObject, $this->formzControllerContext);
78
79
                    $middlewareFactory->run();
80
                }
81
            }
82
        }
83
    }
84
85
    protected function aze()
86
    {
87
        $arguments = $this->formzControllerContext->getArguments();
88
89
        foreach ($arguments as $argument) {
90
            /** @var Argument $argument */
91
            if ($this->request->hasArgument($argument->getName())
92
                && true === class_exists($argument->getDataType())
93
                && true === in_array(FormInterface::class, class_implements($argument->getDataType()))
94
            ) {
95
                /** @var PropertyMapper $propertyMapper */
96
                $propertyMapper = Core::instantiate(PropertyMapper::class);
97
                $form = $propertyMapper->convert(
98
                    $this->request->getArgument($argument->getName()),
99
                    $argument->getDataType()
100
                );
101
                break;
102
            }
103
        }
104
105
        return $form;
0 ignored issues
show
Bug introduced by
The variable $form does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
106
    }
107
108
    /**
109
     * @todo
110
     *
111
     * @return array
112
     */
113
    protected function getRequestData()
114
    {
115
        $requestData = [];
116
117
        if ($this->request->hasArgument('formz')) {
118
            $requestData = $this->request->getArgument('formz');
119
            $requestData = $this->hashService->validateAndStripHmac($requestData);
120
            $requestData = unserialize(base64_decode($requestData));
121
        }
122
123
        return $requestData;
124
    }
125
126
    /**
127
     * @param FormzControllerContext $formzControllerContext
128
     */
129
    public function injectFormzControllerContext(FormzControllerContext $formzControllerContext)
130
    {
131
        $this->formzControllerContext = $formzControllerContext;
132
    }
133
134
    /**
135
     * @param HashService $hashService
136
     */
137
    public function injectHashService(HashService $hashService)
138
    {
139
        $this->hashService = $hashService;
140
    }
141
}
142