Completed
Push — feature/middleware ( 864c18 )
by Romain
03:21
created

BeginMiddleware::aze()   C

Complexity

Conditions 8
Paths 11

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 21
nc 11
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
    }
37
38
    /**
39
     * This is the first middleware being called, it will send a signal to all
40
     * middlewares that depend on it.
41
     */
42
    public function execute()
43
    {
44
        $signalObject = new SignalObject($this->processor, BeginSignal::class, After::class);
45
        $signalObject->dispatch();
46
    }
47
48
    /**
49
     * Will check if the current form was submitted by the user. If it is found,
50
     * the form instance is injected in the form object.
51
     */
52
    protected function checkFormSubmission()
53
    {
54
        if ($this->processor->inSingleFieldValidationContext()) {
55
            /*
56
             * In "single field validation context", there is no need to check
57
             * for the form submission.
58
             */
59
            return;
60
        }
61
62
        $request = $this->processor->getRequest();
63
        $formObject = $this->processor->getFormObject();
64
        $formName = $formObject->getName();
65
66
        if ($request->getMethod() === 'POST'
67
            && $this->processor->getRequestArguments()->hasArgument($formName)
68
        ) {
69
            if (false === $request->hasArgument('formzData')) {
70
                throw new \Exception('todo'); // @todo
71
            }
72
73
            $form = $this->getFormInstance();
74
75
            $formObject->setForm($form);
76
77
            $formzData = $request->getArgument('formzData');
78
            $formObject->getRequestData()->fillFromHash($formzData);
79
80
            $proxy = FormObjectFactory::get()->getProxy($form);
81
            $proxy->markFormAsSubmitted();
82
83
            $this->aze();
84
85
            $this->injectFormHashInProxy();
86
        }
87
    }
88
89
    protected function aze()
90
    {
91
        $formObject = $this->processor->getFormObject();
92
        $request = $this->processor->getRequest();
93
        $proxy = FormObjectFactory::get()->getProxy($formObject->getForm());
94
95
        $currentStep = $formObject->fetchCurrentStep($request)->getCurrentStep();
96
97
        if ($currentStep
98
            && $currentStep->hasSubsteps()
99
        ) {
100
            $currentSubstepIdentifier = $request->hasArgument('currentSubstep')
101
                ? $request->getArgument('currentSubstep')
102
                : null;
103
104
            if ($currentSubstepIdentifier) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $currentSubstepIdentifier of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
105
                $substepDefinition = $currentStep->getSubsteps()->getFirstSubstepDefinition();
106
                $currentSubstepDefinition = null;
107
108
                while ($substepDefinition) {
109
                    if ($substepDefinition->getUniqueIdentifier() === $currentSubstepIdentifier) {
110
                        $currentSubstepDefinition = $substepDefinition;
111
                        break;
112
                    }
113
114
                    $substepDefinition = $substepDefinition->hasNextSubsteps()
115
                        ? $substepDefinition->getNextSubsteps()
116
                        : null;
117
                }
118
119
                $proxy->setCurrentSubstepDefinition($currentSubstepDefinition);
120
            }
121
        }
122
    }
123
124
    /**
125
     * Fetches the form hash from the request data that has been submitted with
126
     * the form, and injects it in the form proxy.
127
     */
128
    protected function injectFormHashInProxy()
129
    {
130
        $formObject = $this->processor->getFormObject();
131
        $hash = $formObject->getRequestData()->getFormHash();
132
133
        $proxy = FormObjectFactory::get()->getProxy($formObject->getForm());
134
        $proxy->setFormHash($hash);
135
    }
136
137
    /**
138
     * @return FormInterface
139
     */
140
    protected function getFormInstance()
141
    {
142
        $formName = $this->processor->getFormObject()->getName();
143
        $formArray = $this->processor->getRequest()->getArgument($formName);
144
        $argument = $this->processor->getRequestArguments()->getArgument($formName);
145
146
        return $argument->setValue($formArray)->getValue();
147
    }
148
149
    /**
150
     * @param MiddlewareProcessor $middlewareProcessor
151
     */
152
    final public function bindMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor)
153
    {
154
        $this->processor = $middlewareProcessor;
155
    }
156
}
157