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

FormInjectionMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 23 2
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\Domain\Middleware\FormInjection;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Middleware\Element\OnBeginMiddleware;
18
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface;
19
use Romm\Formz\Middleware\Processor\RemoveFromSingleFieldValidationContext;
20
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal;
21
22
/**
23
 * This middleware takes care of creating new form instances, based on the
24
 * arguments of the current request.
25
 *
26
 * It is based on the controller action of the current context: each argument
27
 * that has a form type (class that implements `FormInterface`) and is not found
28
 * in the request arguments is injected with an empty form instance.
29
 *
30
 * The goal is to provide a form instance to the controller in every case, so
31
 * the developer can manipulate it easily, for instance by pre-setting values.
32
 */
33
class FormInjectionMiddleware extends OnBeginMiddleware implements PresetMiddlewareInterface, SendsMiddlewareSignal, RemoveFromSingleFieldValidationContext
34
{
35
    /**
36
     * @var int
37
     */
38
    protected $priority = self::PRIORITY_INJECT_FORM;
39
40
    /**
41
     * @see FormInjectionMiddleware
42
     */
43
    protected function process()
44
    {
45
        $formObject = $this->getFormObject();
46
47
        $this->beforeSignal()->dispatch();
48
49
        if (false === $formObject->hasForm()) {
50
            /*
51
             * Creating an empty instance of the form.
52
             */
53
            $formClassName = $formObject->getClassName();
54
            /** @var \Romm\Formz\Form\FormInterface $form */
55
            $form = Core::get()->getObjectManager()->getEmptyObject($formClassName);
56
57
            /*
58
             * The form instance is injected in the form object, for further
59
             * usage.
60
             */
61
            $formObject->setForm($form);
62
        }
63
64
        $this->afterSignal()->dispatch();
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function getAllowedSignals()
71
    {
72
        return [FormInjectionSignal::class];
73
    }
74
}
75