Completed
Push — middleware-wip ( a42479 )
by Romain
02:55
created

FormController::aze()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 12
nc 3
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
        /** @var MiddlewareState $middlewareFactory */
57
        $middlewareFactory = Core::instantiate(MiddlewareState::class);
58
        $middlewareFactory->run($formObject, $originalRequest);
59
60
61
//        $tt = new \TYPO3\CMS\Core\TimeTracker\TimeTracker();
62
//        $tt->start();
63
//        /** @var FormObjectFactory $formObjectFactory */
64
//        $formObjectFactory = Core::instantiate(FormObjectFactory::class);
65
//        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($tt->getDifferenceToStarttime(), 'TIME 1');
66
//        $qsd = $formObjectFactory->getInstanceFromClassName(\Romm\FormzExample\Form\ExampleForm::class, 'exForm');
67
//        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($tt->getDifferenceToStarttime(), 'TIME 2');
68
//
69
//        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($qsd, '$qsd');
70
//
71
//
72
////        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($originalRequest, '$originalRequest');
73
//
74
//        $aze = $this->controllerService->getControllerActionParameters(
75
//            $originalRequest->getControllerObjectName(),
76
//            $originalRequest->getControllerActionName()
77
//        );
78
////        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($aze, '$aze');
79
//
80
//        $form = $this->aze();
81
//        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($tt->getDifferenceToStarttime(), 'TIME 3');
82
//        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($form, '$form');
83
84
        $this->formzControllerContext->setDispatched(true);
85
86
        $this->forward(
87
            $originalRequest->getControllerActionName(),
88
            $originalRequest->getControllerName(),
89
            $originalRequest->getControllerExtensionName()
90
        );
91
    }
92
93
    protected function aze()
94
    {
95
        $arguments = $this->formzControllerContext->getArguments();
96
97
        foreach ($arguments as $argument) {
98
            /** @var Argument $argument */
99
            if ($this->request->hasArgument($argument->getName())
100
                && true === class_exists($argument->getDataType())
101
                && true === in_array(FormInterface::class, class_implements($argument->getDataType()))
102
            ) {
103
                /** @var PropertyMapper $propertyMapper */
104
                $propertyMapper = Core::instantiate(PropertyMapper::class);
105
                $form = $propertyMapper->convert(
106
                    $this->request->getArgument($argument->getName()),
107
                    $argument->getDataType()
108
                );
109
                break;
110
            }
111
        }
112
113
        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...
114
    }
115
116
    /**
117
     * @todo
118
     *
119
     * @return array
120
     */
121
    protected function getRequestData()
122
    {
123
        $requestData = [];
124
125
        if ($this->request->hasArgument('formz')) {
126
            $requestData = $this->request->getArgument('formz');
127
            $requestData = $this->hashService->validateAndStripHmac($requestData);
128
            $requestData = unserialize(base64_decode($requestData));
129
        }
130
131
        return $requestData;
132
    }
133
134
    /**
135
     * @param FormzControllerContext $formzControllerContext
136
     */
137
    public function injectFormzControllerContext(FormzControllerContext $formzControllerContext)
138
    {
139
        $this->formzControllerContext = $formzControllerContext;
140
    }
141
142
    /**
143
     * @param ControllerService $controllerService
144
     */
145
    public function injectControllerService(ControllerService $controllerService)
146
    {
147
        $this->controllerService = $controllerService;
148
    }
149
150
    /**
151
     * @param HashService $hashService
152
     */
153
    public function injectHashService(HashService $hashService)
154
    {
155
        $this->hashService = $hashService;
156
    }
157
}
158