Completed
Push — middleware-wip ( 844477...d1e0ab )
by Romain
02:47
created

ControllerState::getRequestArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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\Form\FormInterface;
17
use Romm\Formz\Form\FormObject;
18
use Romm\Formz\Form\FormObjectFactory;
19
use Romm\Formz\Service\Traits\ExtendedSelfInstantiateTrait;
20
use TYPO3\CMS\Core\SingletonInterface;
21
use TYPO3\CMS\Extbase\Mvc\Controller\Argument;
22
use TYPO3\CMS\Extbase\Mvc\Controller\Arguments;
23
use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
24
use TYPO3\CMS\Extbase\Mvc\Request as MvcRequest;
25
use TYPO3\CMS\Extbase\Mvc\Web\Request;
26
27
class ControllerState implements SingletonInterface
28
{
29
    use ExtendedSelfInstantiateTrait;
30
31
    /**
32
     * @var FormObjectFactory
33
     */
34
    protected $formObjectFactory;
35
36
    /**
37
     * @var FormObject[]
38
     */
39
    protected $formArguments = [];
40
41
    /**
42
     * @var bool
43
     */
44
    protected $dispatched = false;
45
46
    /**
47
     * @var Request
48
     */
49
    protected $request;
50
51
    /**
52
     * @var Request
53
     */
54
    protected $realRequest;
55
56
    /**
57
     * @var Request
58
     */
59
    protected $originalRequest;
60
61
    /**
62
     * @var Arguments
63
     */
64
    protected $requestArguments;
65
66
    /**
67
     * @var array
68
     */
69
    protected $settings = [];
70
71
    /**
72
     * @todo
73
     *
74
     * @param MvcRequest $request
75
     * @param Arguments  $requestArguments
76
     * @param array      $settings
77
     * @return $this
78
     */
79
    public function setData(MvcRequest $request, Arguments $requestArguments, array $settings)
80
    {
81
        /** @var Request $request */
82
        $this->realRequest = $request;
83
        $this->request = clone $request;
84
        $this->originalRequest = clone $request;
85
        $this->requestArguments = $requestArguments;
86
        $this->settings = $settings;
87
88
        return $this;
89
    }
90
91
    /**
92
     * @todo
93
     *
94
     * @throws StopActionException
95
     */
96
    public function dispatch()
97
    {
98
        if (false === $this->dispatched) {
99
            $this->dispatched = true;
100
101
            $this->realRequest->setDispatched(false);
102
            $this->realRequest->setControllerVendorName('Romm');
103
            $this->realRequest->setControllerName('Form');
104
            $this->realRequest->setControllerExtensionName('Formz');
105
            $this->realRequest->setControllerActionName('processForm');
106
            $this->realRequest->setArguments([
107
                'originalRequest' => $this->originalRequest
108
            ]);
109
110
            $this->checkFormObjectsErrors();
111
112
            throw new StopActionException;
113
        }
114
    }
115
116
    /**
117
     * Will check if the form objects found in the request arguments contain
118
     * configuration errors. If they do, we dispatch the request to the error
119
     * view, where all errors will be explained properly to the user.
120
     */
121
    protected function checkFormObjectsErrors()
122
    {
123
        foreach ($this->getRequestForms() as $formObject) {
124
            if ($formObject->getConfigurationValidationResult()->hasErrors()) {
125
                $this->realRequest->setControllerActionName('formObjectError');
126
                $this->realRequest->setArguments(['formObject' => $formObject]);
127
128
                break;
129
            }
130
        }
131
    }
132
133
    /**
134
     * Loops on the request arguments, and pick up each one that is a form
135
     * instance (it implements `FormInterface`).
136
     *
137
     * @return FormObject[]
138
     */
139
    public function getRequestForms()
140
    {
141
        if (empty($this->formArguments)) {
142
            /** @var Argument $argument */
143
            foreach ($this->requestArguments as $argument) {
144
                $type = $argument->getDataType();
145
146
                if (class_exists($type)
147
                    && in_array(FormInterface::class, class_implements($type))
148
                ) {
149
                    $formClassName = $argument->getDataType();
150
                    $formName = $argument->getName();
151
152
                    $formObject = $this->formObjectFactory->getInstanceFromClassName($formClassName, $formName);
153
                    $this->formArguments[$formName] = $formObject;
154
                }
155
            }
156
        }
157
158
        return $this->formArguments;
159
    }
160
161
    /**
162
     * @return Request
163
     */
164
    public function getRequest()
165
    {
166
        return $this->request;
167
    }
168
169
    /**
170
     * @return Arguments
171
     */
172
    public function getRequestArguments()
173
    {
174
        return $this->requestArguments;
175
    }
176
177
    /**
178
     * @return array
179
     */
180
    public function getSettings()
181
    {
182
        return $this->settings;
183
    }
184
185
    /**
186
     * @param FormObjectFactory $formObjectFactory
187
     */
188
    public function injectFormObjectFactory(FormObjectFactory $formObjectFactory)
189
    {
190
        $this->formObjectFactory = $formObjectFactory;
191
    }
192
}
193