Completed
Push — wip/steps ( 107e99...47b00f )
by Romain
02:27
created

BeginMiddleware   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 8
dl 0
loc 136
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A bindMiddlewareProcessor() 0 4 1
A requestWasSubmitted() 0 4 1
A initialize() 0 8 1
A execute() 0 5 1
B checkFormSubmission() 0 38 6
A fetchCurrentStep() 0 9 2
A fetchSubstepsLevel() 0 13 3
A injectFormHashInProxy() 0 8 1
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\Middleware\Item\Begin;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Form\FormObject\FormObjectFactory;
18
use Romm\Formz\Middleware\BasicMiddlewareInterface;
19
use Romm\Formz\Middleware\Item\Begin\Service\FormService;
20
use Romm\Formz\Middleware\Processor\MiddlewareProcessor;
21
use Romm\Formz\Middleware\Signal\After;
22
use Romm\Formz\Middleware\Signal\SignalObject;
23
24
final class BeginMiddleware implements BasicMiddlewareInterface
25
{
26
    /**
27
     * @var MiddlewareProcessor
28
     */
29
    private $processor;
30
31
    /**
32
     * @var FormService
33
     */
34
    protected $formService;
35
36
    /**
37
     * Initialization of this middleware.
38
     */
39
    public function initialize()
40
    {
41
        $this->formService = Core::instantiate(FormService::class, $this->processor);
42
43
        $this->checkFormSubmission();
44
        $this->fetchCurrentStep();
45
        $this->fetchSubstepsLevel();
46
    }
47
48
    /**
49
     * This is the first middleware being called, it will send a signal to all
50
     * middlewares that depend on it.
51
     */
52
    public function execute()
53
    {
54
        $signalObject = new SignalObject($this->processor, BeginSignal::class, After::class);
55
        $signalObject->dispatch();
56
    }
57
58
    /**
59
     * Will check if the current form was submitted by the user. If it is found,
60
     * the form instance is injected in the form object.
61
     */
62
    protected function checkFormSubmission()
63
    {
64
        if ($this->processor->inSingleFieldValidationContext()) {
65
            /*
66
             * In "single field validation context", there is no need to check
67
             * for the form submission.
68
             */
69
            return;
70
        }
71
72
        $request = $this->processor->getRequest();
73
        $formObject = $this->processor->getFormObject();
74
        $formName = $formObject->getName();
75
76
        if ($this->requestWasSubmitted()
77
            && $this->processor->getRequestArguments()->hasArgument($formName)
78
        ) {
79
            if (false === $request->hasArgument('fz-hash')) {
80
                throw new \Exception('todo fz-hash'); // @todo
81
            }
82
83
            if (false === $request->hasArgument('formzData')) {
84
                throw new \Exception('todo formzData'); // @todo
85
            }
86
87
            $form = $this->formService->getFormInstance();
88
89
            $formObject->setForm($form);
90
91
            $formzData = $request->getArgument('formzData');
92
            $formObject->getRequestData()->fillFromHash($formzData);
93
94
            $proxy = FormObjectFactory::get()->getProxy($form);
95
            $proxy->markFormAsSubmitted();
96
97
            $this->injectFormHashInProxy();
98
        }
99
    }
100
101
    /**
102
     * @todo
103
     */
104
    protected function fetchCurrentStep()
105
    {
106
        $formObject = $this->processor->getFormObject();
107
        $request = ($formObject->formWasSubmitted())
108
            ? $this->processor->getRequest()->getReferringRequest()
109
            : $this->processor->getRequest();
110
111
        $formObject->fetchCurrentStep($request);
112
    }
113
114
    /**
115
     * @todo
116
     */
117
    protected function fetchSubstepsLevel()
118
    {
119
        $request = $this->processor->getRequest();
120
121
        if ($this->requestWasSubmitted()
122
            && $request->hasArgument('substepsLevel')
123
        ) {
124
            $substepLevel = $request->getArgument('substepsLevel');
125
            FormObjectFactory::get()
126
                ->getStepService($this->processor->getFormObject())
127
                ->setSubstepsLevel($substepLevel);
128
        }
129
    }
130
131
    /**
132
     * Fetches the form hash from the request data that has been submitted with
133
     * the form, and injects it in the form proxy.
134
     */
135
    protected function injectFormHashInProxy()
136
    {
137
        $formObject = $this->processor->getFormObject();
138
        $hash = $formObject->getRequestData()->getFormHash();
139
140
        $proxy = FormObjectFactory::get()->getProxy($formObject->getForm());
141
        $proxy->setFormHash($hash);
142
    }
143
144
    /**
145
     * @param MiddlewareProcessor $middlewareProcessor
146
     */
147
    final public function bindMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor)
148
    {
149
        $this->processor = $middlewareProcessor;
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    protected function requestWasSubmitted()
156
    {
157
        return $this->processor->getRequest()->getMethod() === 'POST';
158
    }
159
}
160