Completed
Push — middleware-wip ( 357c0d...3d734d )
by Romain
02:39
created

BeginMiddleware   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 50
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 6 1
B checkFormSubmission() 0 22 4
A getAllowedSignals() 0 4 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\SendsMiddlewareSignal;
18
use Romm\Formz\Middleware\State\StateMiddlewareDependencyTrait;
19
20
final class BeginMiddleware implements BasicMiddlewareInterface, SendsMiddlewareSignal
21
{
22
    use StateMiddlewareDependencyTrait;
23
24
    /**
25
     * This is the first middleware being called, it will send a signal to all
26
     * middlewares that depend on it.
27
     */
28
    public function execute()
29
    {
30
        $this->checkFormSubmission();
31
32
        $this->afterSignal()->dispatch();
33
    }
34
35
    /**
36
     * Will check if the given form was submitted by the user. If it is found,
37
     * the form instance is injected in the form object.
38
     */
39
    protected function checkFormSubmission()
40
    {
41
        $request = $this->state->getRequest();
42
        $formObject = $this->state->getFormObject();
43
        $formName = $formObject->getName();
44
45
        if ($request->getMethod() === 'POST'
46
            && $request->hasArgument($formName)
47
        ) {
48
            $formArray = $request->getArgument($formName);
49
            $argument = $this->state->getArguments()->getArgument($formName);
50
            $form = $argument->setValue($formArray)->getValue();
51
52
            $formObject->setForm($form);
53
            $formObject->markFormAsSubmitted();
54
            $request->setArgument($formName, $form);
55
56
            if ($request->hasArgument('formzData')) {
57
                $formObject->getRequestData()->fillFromHash($request->getArgument('formzData'));
58
            }
59
        }
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getAllowedSignals()
66
    {
67
        return [BeginSignal::class];
68
    }
69
}
70