Completed
Push — middleware-wip ( 106169...2ebda9 )
by Romain
02:43
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\Items\InjectForm;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Middleware\Items\OnBeginMiddleware;
18
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal;
19
20
class InjectFormMiddleware extends OnBeginMiddleware implements SendsMiddlewareSignal
21
{
22
    /**
23
     * @todo
24
     */
25
    protected function process()
26
    {
27
        $this->dispatchBeforeSignal();
28
29
        $formClassName = $this->getFormObject()->getClassName();
30
        $formName = $this->getFormObject()->getName();
31
32
        if (false === $this->getRequest()->hasArgument($formName)) {
33
            /*
34
             * Creating an empty instance of the form.
35
             */
36
            $form = Core::get()->getObjectManager()->getEmptyObject($formClassName);
37
38
            /*
39
             * The request argument with the name of the form is filled with the
40
             * new form instance.
41
             */
42
            $this->getRequest()->setArgument($formName, $form);
43
44
            /*
45
             * The form instance is injected in the form object, for further
46
             * usage.
47
             */
48
            $this->getFormObject()->setForm($form);
49
50
            /*
51
             * The after-signal should be dispatched only if the form was
52
             * actually injected in the request, otherwise the other middlewares
53
             * should not be called.
54
             */
55
            $this->dispatchAfterSignal();
56
        }
57
    }
58
59
    /**
60
     * @return int
61
     */
62
    public function getPriority()
63
    {
64
        return self::PRIORITY_INJECT_FORM;
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function getSignalName()
71
    {
72
        return InjectFormSignal::class;
73
    }
74
}
75