Completed
Push — middleware-wip ( 4ea7b3...161931 )
by Romain
03:37
created

BeginMiddleware::bindMiddlewareState()   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\Middleware\BasicMiddlewareInterface;
17
use Romm\Formz\Middleware\Signal\After;
18
use Romm\Formz\Middleware\Signal\SignalObject;
19
use Romm\Formz\Middleware\State\MiddlewareState;
20
21
final class BeginMiddleware implements BasicMiddlewareInterface
22
{
23
    /**
24
     * @var MiddlewareState
25
     */
26
    private $state;
27
28
    /**
29
     * This is the first middleware being called, it will send a signal to all
30
     * middlewares that depend on it.
31
     */
32
    public function execute()
33
    {
34
        $this->checkFormSubmission();
35
36
        $signalObject = new SignalObject($this->state, BeginSignal::class, After::class);
37
        $signalObject->dispatch();
38
    }
39
40
    /**
41
     * Will check if the current form was submitted by the user. If it is found,
42
     * the form instance is injected in the form object.
43
     */
44
    protected function checkFormSubmission()
45
    {
46
        if ($this->state->inSingleFieldValidationContext()) {
47
            /*
48
             * In "single field validation context", there is no need to check
49
             * for the form submission.
50
             */
51
            return;
52
        }
53
54
        $request = $this->state->getRequest();
55
        $formObject = $this->state->getFormObject();
56
        $formName = $formObject->getName();
57
58
        if ($request->getMethod() === 'POST'
59
            && $request->hasArgument($formName)
60
        ) {
61
            $formArray = $request->getArgument($formName);
62
            $argument = $this->state->getRequestArguments()->getArgument($formName);
63
            $form = $argument->setValue($formArray)->getValue();
64
65
            $formObject->setForm($form);
66
            $formObject->markFormAsSubmitted();
67
            $request->setArgument($formName, $form);
68
69
            if ($request->hasArgument('formzData')) {
70
                $formObject->getRequestData()->fillFromHash($request->getArgument('formzData'));
71
            }
72
        }
73
    }
74
75
    /**
76
     * @param MiddlewareState $middlewareState
77
     */
78
    final public function bindMiddlewareState(MiddlewareState $middlewareState)
79
    {
80
        $this->state = $middlewareState;
81
    }
82
}
83