Completed
Push — middleware-wip ( 17e892...bf36ad )
by Romain
02:41
created

BeginMiddleware   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 46
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 6 1
A checkFormSubmission() 0 18 3
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
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function getAllowedSignals()
62
    {
63
        return [BeginSignal::class];
64
    }
65
}
66