Completed
Push — feature/middleware ( 67e214...1de75b )
by Romain
02:15
created

BeginMiddleware::requestWasSubmitted()   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 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\Domain\Middleware\Begin;
15
16
use Romm\Formz\Form\FormInterface;
17
use Romm\Formz\Form\FormObject\FormObjectFactory;
18
use Romm\Formz\Middleware\Processor\MiddlewareProcessor;
19
use Romm\Formz\Middleware\Signal\After;
20
use Romm\Formz\Middleware\Signal\Element\SignalObject;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23
final class BeginMiddleware
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
        /** @var SignalObject $signalObject */
45
        $signalObject = GeneralUtility::makeInstance(SignalObject::class, $this->processor, BeginSignal::class, After::class);
46
        $signalObject->dispatch();
47
    }
48
49
    /**
50
     * Will check if the current form was submitted by the user. If it is found,
51
     * the form instance is injected in the form object.
52
     */
53
    protected function checkFormSubmission()
54
    {
55
        $formObject = $this->processor->getFormObject();
56
57
        if ($formObject->hasForm()) {
58
            return;
59
        }
60
61
        $request = $this->processor->getRequest();
62
        $formName = $formObject->getName();
63
64
        if ($this->requestWasSubmitted()
65
            && null === $request->getOriginalRequest()
66
            && $this->processor->getRequestArguments()->hasArgument($formName)
67
        ) {
68
            if (false === $request->hasArgument('formData')) {
69
                throw new \Exception('todo'); // @todo
70
            }
71
72
            $form = $this->getFormInstance();
73
74
            $formObject->setForm($form);
75
76
            $formData = $request->getArgument('formData');
77
            $formObject->getRequestData()->fillFromHash($formData);
78
79
            $proxy = FormObjectFactory::get()->getProxy($form);
0 ignored issues
show
Bug introduced by
It seems like getProxy() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
80
            $proxy->markFormAsSubmitted();
81
82
            $this->injectFormHashInProxy();
83
        }
84
    }
85
86
    /**
87
     * Fetches the form hash from the request data that has been submitted with
88
     * the form, and injects it in the form proxy.
89
     */
90
    protected function injectFormHashInProxy()
91
    {
92
        $formObject = $this->processor->getFormObject();
93
        $hash = $formObject->getRequestData()->getFormHash();
94
95
        $proxy = FormObjectFactory::get()->getProxy($formObject->getForm());
0 ignored issues
show
Bug introduced by
It seems like getProxy() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
96
        $proxy->setFormHash($hash);
97
    }
98
99
    /**
100
     * @return FormInterface
101
     */
102
    protected function getFormInstance()
103
    {
104
        $formName = $this->processor->getFormObject()->getName();
105
        $formArray = $this->processor->getRequest()->getArgument($formName);
106
        $argument = $this->processor->getRequestArguments()->getArgument($formName);
107
108
        return $argument->setValue($formArray)->getValue();
109
    }
110
111
    /**
112
     * @param MiddlewareProcessor $middlewareProcessor
113
     */
114
    final public function bindMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor)
115
    {
116
        $this->processor = $middlewareProcessor;
117
    }
118
119
    /**
120
     * @return bool
121
     */
122
    protected function requestWasSubmitted()
123
    {
124
        return $this->processor->getRequest()->getMethod() === 'POST';
125
    }
126
127
}
128