Completed
Push — feature/middleware ( aef20d...bf156f )
by Romain
02:16
created

BeginMiddleware   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 98
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 4 1
A execute() 0 6 1
B checkFormSubmission() 0 34 5
A injectFormHashInProxy() 0 8 1
A getFormInstance() 0 8 1
A bindMiddlewareProcessor() 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\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\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
        if ($this->processor->inSingleFieldValidationContext()) {
56
            /*
57
             * In "single field validation context", there is no need to check
58
             * for the form submission.
59
             */
60
            return;
61
        }
62
63
        $request = $this->processor->getRequest();
64
        $formObject = $this->processor->getFormObject();
65
        $formName = $formObject->getName();
66
67
        if ($request->getMethod() === 'POST'
68
            && $this->processor->getRequestArguments()->hasArgument($formName)
69
        ) {
70
            if (false === $request->hasArgument('formData')) {
71
                throw new \Exception('todo'); // @todo
72
            }
73
74
            $form = $this->getFormInstance();
75
76
            $formObject->setForm($form);
77
78
            $formData = $request->getArgument('formData');
79
            $formObject->getRequestData()->fillFromHash($formData);
80
81
            $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...
82
            $proxy->markFormAsSubmitted();
83
84
            $this->injectFormHashInProxy();
85
        }
86
    }
87
88
    /**
89
     * Fetches the form hash from the request data that has been submitted with
90
     * the form, and injects it in the form proxy.
91
     */
92
    protected function injectFormHashInProxy()
93
    {
94
        $formObject = $this->processor->getFormObject();
95
        $hash = $formObject->getRequestData()->getFormHash();
96
97
        $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...
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