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

EndMiddleware   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 9 1
A injectFormInRequest() 0 11 2
A injectFormResultInRequest() 0 18 2
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\End;
15
16
use Romm\Formz\Middleware\Processor\MiddlewareProcessor;
17
use Romm\Formz\Middleware\Signal\Before;
18
use Romm\Formz\Middleware\Signal\SignalObject;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
final class EndMiddleware
22
{
23
    /**
24
     * @var MiddlewareProcessor
25
     */
26
    private $processor;
27
28
    /**
29
     * This is the last middleware being called, it will send a signal to all
30
     * middlewares that depend on it.
31
     */
32
    public function execute()
33
    {
34
        /** @var SignalObject $signalObject */
35
        $signalObject = GeneralUtility::makeInstance(SignalObject::class,$this->processor, EndSignal::class, Before::class);
36
        $signalObject->dispatch();
37
38
        $this->injectFormInRequest();
39
        $this->injectFormResultInRequest();
40
    }
41
42
    /**
43
     * The form instance is injected in the request argument.
44
     */
45
    protected function injectFormInRequest()
46
    {
47
        $formObject = $this->processor->getFormObject();
48
49
        if ($formObject->hasForm()) {
50
            $this->processor->getRequest()->setArgument(
51
                $formObject->getName(),
52
                $formObject->getForm()
53
            );
54
        }
55
    }
56
57
    /**
58
     * Will inject the form validation result that was manipulated by other
59
     * middlewares in the current request.
60
     */
61
    protected function injectFormResultInRequest()
62
    {
63
        if ($this->processor->inSingleFieldValidationContext()) {
64
            /*
65
             * In "single field validation context", there is no need to inject
66
             * the form result in the request.
67
             */
68
            return;
69
        }
70
71
        $request = $this->processor->getRequest();
72
        $result = $this->processor->getFormObject()->getFormResult();
73
        $formName = $this->processor->getFormObject()->getName();
74
75
        $requestResult = $request->getOriginalRequestMappingResults();
76
        $requestResult->forProperty($formName)->merge($result);
77
        $request->setOriginalRequestMappingResults($requestResult);
78
    }
79
80
    /**
81
     * @param MiddlewareProcessor $middlewareProcessor
82
     */
83
    final public function bindMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor)
84
    {
85
        $this->processor = $middlewareProcessor;
86
    }
87
}
88