Completed
Push — middleware-wip ( 9b4a78...106169 )
by Romain
03:02
created

FormController::getRequestData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
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 Romm\Formz\Service\ControllerService;
21
use Romm\FormzExample\Form\ExampleForm;
22
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
23
use TYPO3\CMS\Extbase\Mvc\Controller\Argument;
24
use TYPO3\CMS\Extbase\Property\PropertyMapper;
25
use TYPO3\CMS\Extbase\Security\Cryptography\HashService;
26
27
class FormController extends ActionController
28
{
29
    /**
30
     * @var FormzControllerContext
31
     */
32
    protected $formzControllerContext;
33
34
    /**
35
     * @var HashService
36
     */
37
    protected $hashService;
38
39
    /**
40
     * @var ControllerService
41
     */
42
    protected $controllerService;
43
44
    /**
45
     * Main action used to dispatch the request properly, depending on FormZ
46
     * configuration.
47
     */
48
    public function processFormAction()
49
    {
50
        /** @var FormObjectFactory $formObjectFactory */
51
        $formObjectFactory = Core::instantiate(FormObjectFactory::class);
52
53
        $formObject = $formObjectFactory->getInstanceFromClassName(ExampleForm::class, 'foo');
54
        $originalRequest = $this->formzControllerContext->getOriginalRequest();
55
56
        if (false === $formObject->getConfigurationValidationResult()->hasErrors()) {
57
58
            /** @var MiddlewareState $middlewareFactory */
59
            $middlewareFactory = Core::instantiate(MiddlewareState::class);
60
            $middlewareFactory->run($formObject, $originalRequest);
61
        }
62
63
64
//        $tt = new \TYPO3\CMS\Core\TimeTracker\TimeTracker();
65
//        $tt->start();
66
//        /** @var FormObjectFactory $formObjectFactory */
67
//        $formObjectFactory = Core::instantiate(FormObjectFactory::class);
68
//        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($tt->getDifferenceToStarttime(), 'TIME 1');
69
//        $qsd = $formObjectFactory->getInstanceFromClassName(\Romm\FormzExample\Form\ExampleForm::class, 'exForm');
70
//        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($tt->getDifferenceToStarttime(), 'TIME 2');
71
//
72
//        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($qsd, '$qsd');
73
//
74
//
75
////        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($originalRequest, '$originalRequest');
76
//
77
//        $aze = $this->controllerService->getControllerActionParameters(
78
//            $originalRequest->getControllerObjectName(),
79
//            $originalRequest->getControllerActionName()
80
//        );
81
////        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($aze, '$aze');
82
//
83
//        $form = $this->aze();
84
//        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($tt->getDifferenceToStarttime(), 'TIME 3');
85
//        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($form, '$form');
86
87
        $this->formzControllerContext->setDispatched(true);
88
89
        $this->forward(
90
            $originalRequest->getControllerActionName(),
91
            $originalRequest->getControllerName(),
92
            $originalRequest->getControllerExtensionName()
93
        );
94
    }
95
96
    protected function aze()
97
    {
98
        $arguments = $this->formzControllerContext->getArguments();
99
100
        foreach ($arguments as $argument) {
101
            /** @var Argument $argument */
102
            if ($this->request->hasArgument($argument->getName())
103
                && true === class_exists($argument->getDataType())
104
                && true === in_array(FormInterface::class, class_implements($argument->getDataType()))
105
            ) {
106
                /** @var PropertyMapper $propertyMapper */
107
                $propertyMapper = Core::instantiate(PropertyMapper::class);
108
                $form = $propertyMapper->convert(
109
                    $this->request->getArgument($argument->getName()),
110
                    $argument->getDataType()
111
                );
112
                break;
113
            }
114
        }
115
116
        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...
117
    }
118
119
    /**
120
     * @todo
121
     *
122
     * @return array
123
     */
124
    protected function getRequestData()
125
    {
126
        $requestData = [];
127
128
        if ($this->request->hasArgument('formz')) {
129
            $requestData = $this->request->getArgument('formz');
130
            $requestData = $this->hashService->validateAndStripHmac($requestData);
131
            $requestData = unserialize(base64_decode($requestData));
132
        }
133
134
        return $requestData;
135
    }
136
137
    /**
138
     * @param FormzControllerContext $formzControllerContext
139
     */
140
    public function injectFormzControllerContext(FormzControllerContext $formzControllerContext)
141
    {
142
        $this->formzControllerContext = $formzControllerContext;
143
    }
144
145
    /**
146
     * @param ControllerService $controllerService
147
     */
148
    public function injectControllerService(ControllerService $controllerService)
149
    {
150
        $this->controllerService = $controllerService;
151
    }
152
153
    /**
154
     * @param HashService $hashService
155
     */
156
    public function injectHashService(HashService $hashService)
157
    {
158
        $this->hashService = $hashService;
159
    }
160
}
161