Completed
Push — middleware-wip ( a395c5...d52214 )
by Romain
03:54
created

ControllerProcessor::checkFormObjectsErrors()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
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\Processor;
15
16
use Romm\Formz\Form\FormInterface;
17
use Romm\Formz\Form\FormObject\FormObject;
18
use Romm\Formz\Form\FormObject\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 ControllerProcessor 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
     * @param MvcRequest $request
73
     * @param Arguments  $requestArguments
74
     * @param array      $settings
75
     * @return $this
76
     */
77
    public static function prepare(MvcRequest $request, Arguments $requestArguments, array $settings)
78
    {
79
        return self::get()->setData($request, $requestArguments, $settings);
80
    }
81
82
    /**
83
     * Injects the data needed for this class to work properly. This method must
84
     * be called before the dispatch is called.
85
     *
86
     * @param MvcRequest $request
87
     * @param Arguments  $requestArguments
88
     * @param array      $settings
89
     * @return $this
90
     */
91
    public function setData(MvcRequest $request, Arguments $requestArguments, array $settings)
92
    {
93
        /** @var Request $request */
94
        $this->realRequest = $request;
95
        $this->request = clone $request;
96
        $this->originalRequest = clone $request;
97
        $this->requestArguments = $requestArguments;
98
        $this->settings = $settings;
99
100
        return $this;
101
    }
102
103
    /**
104
     * Will dispatch the current request to the form controller, which will take
105
     * care of processing everything properly.
106
     *
107
     * In case no form is found in the controller action parameters, the current
108
     * request is not killed.
109
     *
110
     * @throws StopActionException
111
     */
112
    public function dispatch()
113
    {
114
        if (false === $this->dispatched) {
115
            $this->dispatched = true;
116
117
            if (false === empty($this->getRequestForms())) {
118
                $this->realRequest->setDispatched(false);
119
                $this->realRequest->setControllerVendorName('Romm');
120
                $this->realRequest->setControllerName('Form');
121
                $this->realRequest->setControllerExtensionName('Formz');
122
                $this->realRequest->setControllerActionName('processForm');
123
                $this->realRequest->setArguments([
124
                    'originalRequest' => $this->originalRequest
125
                ]);
126
127
                $this->checkFormObjectsErrors();
128
129
                throw new StopActionException;
130
            }
131
        }
132
    }
133
134
    /**
135
     * Will check if the form objects found in the request arguments contain
136
     * configuration errors. If they do, we dispatch the request to the error
137
     * view, where all errors will be explained properly to the user.
138
     */
139
    protected function checkFormObjectsErrors()
140
    {
141
        foreach ($this->getRequestForms() as $formObject) {
142
            if ($formObject->getConfigurationValidationResult()->hasErrors()) {
143
                $this->realRequest->setControllerActionName('formObjectError');
144
                $this->realRequest->setArguments(['formObject' => $formObject]);
145
146
                break;
147
            }
148
        }
149
    }
150
151
    /**
152
     * Loops on the request arguments, and pick up each one that is a form
153
     * instance (it implements `FormInterface`).
154
     *
155
     * @return FormObject[]
156
     */
157
    public function getRequestForms()
158
    {
159
        if (null === $this->formArguments) {
160
            $this->formArguments = [];
161
162
            /** @var Argument $argument */
163
            foreach ($this->requestArguments as $argument) {
164
                $type = $argument->getDataType();
165
166
                if (class_exists($type)
167
                    && in_array(FormInterface::class, class_implements($type))
168
                ) {
169
                    $formClassName = $argument->getDataType();
170
                    $formName = $argument->getName();
171
172
                    $formObject = $this->formObjectFactory->getInstanceWithClassName($formClassName, $formName);
173
                    $this->formArguments[$formName] = $formObject;
174
                }
175
            }
176
        }
177
178
        return $this->formArguments;
179
    }
180
181
    /**
182
     * @return Request
183
     */
184
    public function getRequest()
185
    {
186
        return $this->request;
187
    }
188
189
    /**
190
     * @return Arguments
191
     */
192
    public function getRequestArguments()
193
    {
194
        return $this->requestArguments;
195
    }
196
197
    /**
198
     * @return array
199
     */
200
    public function getSettings()
201
    {
202
        return $this->settings;
203
    }
204
205
    /**
206
     * @param FormObjectFactory $formObjectFactory
207
     */
208
    public function injectFormObjectFactory(FormObjectFactory $formObjectFactory)
209
    {
210
        $this->formObjectFactory = $formObjectFactory;
211
    }
212
}
213