Completed
Push — middleware-wip ( f76eb7...e5756f )
by Romain
07:43
created

BeginMiddleware::getFormInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
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->injectFormHashInProxy();
84
        }
85
    }
86
87
    /**
88
     * Fetches the form hash from the request data that has been submitted with
89
     * the form, and injects it in the form proxy.
90
     */
91
    protected function injectFormHashInProxy()
92
    {
93
        $formObject = $this->processor->getFormObject();
94
        $hash = $formObject->getRequestData()->getFormHash();
95
96
        $proxy = FormObjectFactory::get()->getProxy($formObject->getForm());
97
98
        $proxy->setFormHash($hash);
99
    }
100
101
    /**
102
     * @return FormInterface
103
     */
104
    protected function getFormInstance()
105
    {
106
        $formName = $this->processor->getFormObject()->getName();
107
        $formArray = $this->processor->getRequest()->getArgument($formName);
108
        $argument = $this->processor->getRequestArguments()->getArgument($formName);
109
110
        return $argument->setValue($formArray)->getValue();
111
    }
112
113
    /**
114
     * @param MiddlewareProcessor $middlewareProcessor
115
     */
116
    final public function bindMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor)
117
    {
118
        $this->processor = $middlewareProcessor;
119
    }
120
}
121