Completed
Push — middleware-wip ( 17e892...bf36ad )
by Romain
02:41
created

InjectFormMiddleware::process()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 9
nc 2
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\Middleware\Item\InjectForm;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Middleware\Item\OnBeginMiddleware;
18
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal;
19
20
/**
21
 * This middleware takes care of creating new form instances, based on the
22
 * arguments of the current request.
23
 *
24
 * It is based on the controller action of the current context: each argument
25
 * that is not found in the request, and has a form type (class that implements
26
 * `FormInterface`) gets injected with an empty form instance.
27
 *
28
 * The goal is to provide a form instance to the controller in every case, so
29
 * the developer can manipulate it easily, for instance by pre-setting values.
30
 */
31
class InjectFormMiddleware extends OnBeginMiddleware implements SendsMiddlewareSignal
32
{
33
    /**
34
     * @see InjectFormMiddleware
35
     */
36
    protected function process()
37
    {
38
        $this->beforeSignal()->dispatch();
39
40
        $formClassName = $this->getFormObject()->getClassName();
41
        $formName = $this->getFormObject()->getName();
42
43
        if (false === $this->getRequest()->hasArgument($formName)) {
44
            /*
45
             * Creating an empty instance of the form.
46
             */
47
            $form = Core::get()->getObjectManager()->getEmptyObject($formClassName);
48
49
            /*
50
             * The request argument with the name of the form is filled with the
51
             * new form instance.
52
             */
53
            $this->getRequest()->setArgument($formName, $form);
54
55
            /*
56
             * The form instance is injected in the form object, for further
57
             * usage.
58
             */
59
            $this->getFormObject()->setForm($form);
60
61
            /*
62
             * The after-signal should be dispatched only if the form was
63
             * actually injected in the request, otherwise the other middlewares
64
             * should not be called.
65
             */
66
            $this->afterSignal()->dispatch();
67
        }
68
    }
69
70
    /**
71
     * @return int
72
     */
73
    public function getPriority()
74
    {
75
        return self::PRIORITY_INJECT_FORM;
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    public function getAllowedSignals()
82
    {
83
        return [InjectFormSignal::class];
84
    }
85
}
86