Completed
Push — feature/middleware ( f1bcd3...4630a4 )
by Romain
11:52
created

FormController::commitMetadata()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
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 Exception;
17
use Romm\Formz\Controller\Processor\ControllerProcessor;
18
use Romm\Formz\Core\Core;
19
use Romm\Formz\Form\FormObject\FormObject;
20
use Romm\Formz\Middleware\Processor\MiddlewareProcessor;
21
use Romm\Formz\Middleware\Request\Exception\ForwardException;
22
use Romm\Formz\Middleware\Request\Exception\RedirectException;
23
use Romm\Formz\Middleware\Request\Exception\StopPropagationException;
24
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
25
use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
26
use TYPO3\CMS\Extbase\Mvc\Web\Request;
27
28
/**
29
 * This is the main form controller, which is called before a controller action
30
 * with at least form argument is called.
31
 *
32
 * It will process the argument form(s), for instance by calling all its
33
 * middlewares. It allows manipulating the request, and information about the
34
 * form, before the actual action is called.
35
 */
36
class FormController extends ActionController
37
{
38
    /**
39
     * @var ControllerProcessor
40
     */
41
    protected $processor;
42
43
    /**
44
     * @var Request
45
     */
46
    protected $originalRequest;
47
48
    /**
49
     * Main action used to dispatch the request properly, depending on FormZ
50
     * configuration.
51
     *
52
     * The request is based on the previously called controller action, and is
53
     * used to list which forms are handled (every argument of the action method
54
     * that implements the interface `FormInterface`).
55
     *
56
     * Middlewares will be called for each form argument, and may modify the
57
     * request, which is then dispatched again with modified data.
58
     *
59
     * @param Request $originalRequest
60
     * @throws Exception
61
     */
62
    public function processFormAction(Request $originalRequest)
63
    {
64
        $exception = null;
65
        $this->originalRequest = $originalRequest;
66
67
        try {
68
            $this->invokeMiddlewares();
69
            $this->manageRequestResult();
70
        } catch (Exception $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
71
        }
72
73
        if ($exception instanceof Exception) {
74
            if ($exception instanceof StopPropagationException) {
75
                if ($exception instanceof RedirectException) {
76
                    $this->redirectFromException($exception);
77
                } elseif (false === $exception instanceof ForwardException) {
78
                    $this->forwardToReferrer();
79
                }
80
            } else {
81
                throw $exception;
82
            }
83
        }
84
85
        $this->continueRequest();
86
    }
87
88
    /**
89
     * @param FormObject $formObject
90
     */
91
    public function formObjectErrorAction(FormObject $formObject)
92
    {
93
        $this->view->assign('formObject', $formObject);
94
    }
95
96
    /**
97
     * Will fetch every form argument for this request, and dispatch every
98
     * middleware that was registered in its TypoScript configuration.
99
     */
100
    protected function invokeMiddlewares()
101
    {
102
        foreach ($this->processor->getRequestForms() as $formObject) {
103
            /** @var MiddlewareProcessor $middlewareProcessor */
104
            $middlewareProcessor = Core::instantiate(MiddlewareProcessor::class, $formObject, $this->processor);
105
106
            $middlewareProcessor->run();
107
        }
108
    }
109
110
    /**
111
     * Will check if the request result contains error; if errors are found, the
112
     * request is forwarded to the referring request, with the arguments of the
113
     * current request.
114
     */
115
    protected function manageRequestResult()
116
    {
117
        $result = $this->processor->getRequest()->getOriginalRequestMappingResults();
118
        $this->request->setOriginalRequestMappingResults($result);
119
120
        if ($result->hasErrors()) {
121
            $this->forwardToReferrer();
122
        }
123
    }
124
125
    /**
126
     * Forwards the request to the original request that led to this controller.
127
     *
128
     * @throws StopActionException
129
     */
130
    protected function continueRequest()
131
    {
132
        $this->request->setDispatched(false);
133
        $request = $this->processor->getRequest();
134
135
        $this->request->setPluginName($request->getPluginName());
136
        $this->request->setControllerVendorName($request->getControllerVendorName());
137
        $this->request->setControllerExtensionName($request->getControllerExtensionName());
138
        $this->request->setControllerName($request->getControllerName());
139
        $this->request->setControllerActionName($request->getControllerActionName());
140
        $this->request->setArguments($this->processor->getRequest()->getArguments());
141
142
        throw new StopActionException;
143
    }
144
145
    /**
146
     * Forwards to the referrer request. It will also fill the arguments of the
147
     * action with the ones from the source request.
148
     *
149
     * @throws StopActionException
150
     */
151
    protected function forwardToReferrer()
152
    {
153
        $referringRequest = $this->originalRequest->getReferringRequest();
154
155
        if ($referringRequest) {
156
            $this->request->setDispatched(false);
157
            $this->request->setOriginalRequest($this->originalRequest);
158
159
            $this->request->setControllerVendorName($referringRequest->getControllerVendorName());
160
            $this->request->setControllerExtensionName($referringRequest->getControllerExtensionName());
161
            $this->request->setControllerName($referringRequest->getControllerName());
162
            $this->request->setControllerActionName($referringRequest->getControllerActionName());
163
            $this->request->setArguments($this->processor->getRequest()->getArguments());
164
        } else {
165
            // @todo ?
166
        }
167
168
        throw new StopActionException;
169
    }
170
171
    /**
172
     * @param RedirectException $redirectException
173
     */
174
    protected function redirectFromException(RedirectException $redirectException)
175
    {
176
        $this->uriBuilder->setRequest($this->processor->getRequest());
177
178
        $this->redirect(
179
            $redirectException->getActionName(),
180
            $redirectException->getControllerName(),
181
            $redirectException->getExtensionName(),
182
            $redirectException->getArguments(),
183
            $redirectException->getPageUid(),
184
            $redirectException->getDelay(),
185
            $redirectException->getStatusCode()
186
        );
187
    }
188
189
    /**
190
     * @param ControllerProcessor $processor
191
     */
192
    public function injectProcessor(ControllerProcessor $processor)
193
    {
194
        $this->processor = $processor;
195
    }
196
}
197