Completed
Push — feature/middleware ( 864c18 )
by Romain
03:21
created

EndMiddleware   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 8 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\Middleware\Item\End;
15
16
use Romm\Formz\Middleware\BasicMiddlewareInterface;
17
use Romm\Formz\Middleware\Processor\MiddlewareProcessor;
18
use Romm\Formz\Middleware\Signal\Before;
19
use Romm\Formz\Middleware\Signal\SignalObject;
20
21
final class EndMiddleware implements BasicMiddlewareInterface
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
        $signalObject = new SignalObject($this->processor, EndSignal::class, Before::class);
35
        $signalObject->dispatch();
36
37
        $this->injectFormInRequest();
38
        $this->injectFormResultInRequest();
39
    }
40
41
    /**
42
     * The form instance is injected in the request argument.
43
     */
44
    protected function injectFormInRequest()
45
    {
46
        $formObject = $this->processor->getFormObject();
47
48
        if ($formObject->hasForm()) {
49
            $this->processor->getRequest()->setArgument(
50
                $formObject->getName(),
51
                $formObject->getForm()
52
            );
53
        }
54
    }
55
56
    /**
57
     * Will inject the form validation result that was manipulated by other
58
     * middlewares in the current request.
59
     */
60
    protected function injectFormResultInRequest()
61
    {
62
        if ($this->processor->inSingleFieldValidationContext()) {
63
            /*
64
             * In "single field validation context", there is no need to inject
65
             * the form result in the request.
66
             */
67
            return;
68
        }
69
70
        $request = $this->processor->getRequest();
71
        $result = $this->processor->getFormObject()->getFormResult();
72
        $formName = $this->processor->getFormObject()->getName();
73
74
        $requestResult = $request->getOriginalRequestMappingResults();
75
        $requestResult->forProperty($formName)->merge($result);
76
        $request->setOriginalRequestMappingResults($requestResult);
77
    }
78
79
    /**
80
     * @param MiddlewareProcessor $middlewareProcessor
81
     */
82
    final public function bindMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor)
83
    {
84
        $this->processor = $middlewareProcessor;
85
    }
86
}
87