This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
BeginMiddleware.php
160 lines (135 loc) 路 4.68 KB
/
BeginMiddleware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/*
* 2017 Romain CANON <romain.hydrocanon@gmail.com>
*
* This file is part of the TYPO3 FormZ project.
* It is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, either
* version 3 of the License, or any later version.
*
* For the full copyright and license information, see:
* http://www.gnu.org/licenses/gpl-3.0.html
*/
namespace Romm\Formz\Middleware\Item\Begin;
use Romm\Formz\Core\Core;
use Romm\Formz\Form\FormObject\FormObjectFactory;
use Romm\Formz\Middleware\BasicMiddlewareInterface;
use Romm\Formz\Middleware\Item\Begin\Service\FormService;
use Romm\Formz\Middleware\Processor\MiddlewareProcessor;
use Romm\Formz\Middleware\Signal\After;
use Romm\Formz\Middleware\Signal\SignalObject;
use Romm\Formz\ViewHelpers\Step\PreviousLinkViewHelper;
final class BeginMiddleware implements BasicMiddlewareInterface
{
/**
* @var MiddlewareProcessor
*/
private $processor;
/**
* @var FormService
*/
protected $formService;
/**
* Initialization of this middleware.
*/
public function initialize()
{
$this->formService = Core::instantiate(FormService::class, $this->processor->getRequest(), $this->processor->getRequestArguments());
$this->checkFormSubmission();
$this->fetchCurrentStep();
$this->fetchSubstepsLevel();
}
/**
* This is the first middleware being called, it will send a signal to all
* middlewares that depend on it.
*/
public function execute()
{
$signalObject = new SignalObject($this->processor, BeginSignal::class, After::class);
$signalObject->dispatch();
}
/**
* Will check if the current form was submitted by the user. If it is found,
* the form instance is injected in the form object.
*/
protected function checkFormSubmission()
{
$formObject = $this->processor->getFormObject();
if ($formObject->hasForm()) {
return;
}
$request = $this->processor->getRequest();
$formName = $formObject->getName();
if ($this->requestWasSubmitted()
&& null === $request->getOriginalRequest()
&& $this->processor->getRequestArguments()->hasArgument($formName)
) {
if (false === $request->hasArgument('fz-hash')) {
throw new \Exception('todo fz-hash'); // @todo
}
if (false === $request->hasArgument('formzData')) {
throw new \Exception('todo formzData'); // @todo
}
$form = $this->formService->getFormInstance($formName);
$formObject->setForm($form);
$formzData = $request->getArgument('formzData');
$formObject->getRequestData()->fillFromHash($formzData);
if (!$request->hasArgument(PreviousLinkViewHelper::PREVIOUS_LINK_PARAMETER)) {
$proxy = FormObjectFactory::get()->getProxy($form);
$proxy->markFormAsSubmitted();
}
$this->injectFormHashInProxy();
}
}
/**
* @todo
*/
protected function fetchCurrentStep()
{
$formObject = $this->processor->getFormObject();
$request = ($formObject->formWasSubmitted())
? $this->processor->getRequest()->getReferringRequest()
: $this->processor->getRequest();
$formObject->fetchCurrentStep($request);
}
/**
* @todo
*/
protected function fetchSubstepsLevel()
{
$request = $this->processor->getRequest();
if ($this->requestWasSubmitted()
&& $request->hasArgument('substepsLevel')
) {
$substepLevel = $request->getArgument('substepsLevel');
FormObjectFactory::get()
->getStepService($this->processor->getFormObject())
->setSubstepsLevel($substepLevel);
}
}
/**
* Fetches the form hash from the request data that has been submitted with
* the form, and injects it in the form proxy.
*/
protected function injectFormHashInProxy()
{
$formObject = $this->processor->getFormObject();
$hash = $formObject->getRequestData()->getFormHash();
$proxy = FormObjectFactory::get()->getProxy($formObject->getForm());
$proxy->setFormHash($hash);
}
/**
* @param MiddlewareProcessor $middlewareProcessor
*/
final public function bindMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor)
{
$this->processor = $middlewareProcessor;
}
/**
* @return bool
*/
protected function requestWasSubmitted()
{
return $this->processor->getRequest()->getMethod() === 'POST';
}
}