Completed
Push — middleware-wip ( ab9573...668c38 )
by Romain
10:14
created

FormValidationMiddleware::process()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 13
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\FormValidation;
15
16
use Romm\Formz\Configuration\Form\Field\Field;
17
use Romm\Formz\Core\Core;
18
use Romm\Formz\Middleware\Item\FieldValidation\FieldValidationArguments;
19
use Romm\Formz\Middleware\Item\FieldValidation\FieldValidationSignal;
20
use Romm\Formz\Middleware\Item\OnBeginMiddleware;
21
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal;
22
use Romm\Formz\Middleware\State\PresetMiddlewareInterface;
23
use Romm\Formz\Middleware\State\RemoveFromSingleFieldValidationContext;
24
use Romm\Formz\Validation\Validator\Form\AbstractFormValidator;
25
26
/**
27
 * This middleware takes care of validating the form instance, with a proper
28
 * form validator instance.
29
 *
30
 * You can bind middlewares to the signal `FormValidationSignal`, which will be
31
 * dispatched if and only if the form was submitted by the user.
32
 *
33
 * Please note that this middleware will not be called when being in a "single
34
 * field validation context".
35
 *
36
 * @see \Romm\Formz\Middleware\State\RemoveFromSingleFieldValidationContext
37
 */
38
class FormValidationMiddleware extends OnBeginMiddleware implements PresetMiddlewareInterface, SendsMiddlewareSignal, RemoveFromSingleFieldValidationContext
39
{
40
    /**
41
     * @var \Romm\Formz\Middleware\Item\FormValidation\FormValidationMiddlewareOption
42
     */
43
    protected $options;
44
45
    /**
46
     * @var AbstractFormValidator
47
     */
48
    protected $validator;
49
50
    /**
51
     * @see FormValidationMiddleware
52
     */
53
    protected function process()
54
    {
55
        $formObject = $this->getFormObject();
56
57
        $this->beforeSignal(FormValidationSignal::class)->dispatch();
58
59
        if ($formObject->hasForm()
60
            && $formObject->formWasSubmitted()
61
        ) {
62
            $this->validator = Core::instantiate(
63
                $this->options->getFormValidatorClassName(),
64
                ['name' => $formObject->getName()]
65
            );
66
67
            $this->injectFieldValidationCallback();
68
            $this->injectCurrentStep();
69
70
            $this->validator->setInitialResult($this->getResult());
71
            $this->validator->validate($formObject->getForm());
72
73
            $this->afterSignal(FormValidationSignal::class)->dispatch();
74
        }
75
    }
76
77
    /**
78
     * @todo
79
     */
80
    protected function injectFieldValidationCallback()
81
    {
82
        $callback = function (Field $field) {
83
            $this->afterSignal(FieldValidationSignal::class)
84
                ->withArguments(new FieldValidationArguments($field))
85
                ->dispatch();
86
        };
87
88
        $this->validator->getDataObject()->addFieldValidationCallback($callback);
89
    }
90
91
    /**
92
     * @todo
93
     */
94
    protected function injectCurrentStep()
95
    {
96
        $this->validator->getDataObject()->setCurrentStep($this->getCurrentStep());
0 ignored issues
show
Bug introduced by
It seems like $this->getCurrentStep() can be null; however, setCurrentStep() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function getAllowedSignals()
103
    {
104
        return [FormValidationSignal::class, FieldValidationSignal::class];
105
    }
106
}
107