Completed
Push — wip/steps ( 0e4101...4c32fc )
by Romain
03:57
created

BeginMiddleware::fetchSubstepsLevel()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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