Completed
Push — wip/steps ( 551eb2...b236f9 )
by Romain
03:12
created

BeginMiddleware::bindMiddlewareProcessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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