Completed
Push — wip/steps ( 47ee47 )
by Romain
03:13
created

FormValidationMiddleware   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 71
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 19 3
A injectFieldValidationCallback() 0 10 1
A getAllowedSignals() 0 4 1
A injectCurrentStep() 0 8 2
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\Core\Core;
17
use Romm\Formz\Form\Definition\Field\Field;
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\Processor\PresetMiddlewareInterface;
22
use Romm\Formz\Middleware\Processor\RemoveFromSingleFieldValidationContext;
23
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal;
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\Processor\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($this->options->getFormValidatorClassName());
63
64
            $this->injectFieldValidationCallback();
65
            $this->injectCurrentStep();
66
67
            $this->validator->validate($formObject->getForm());
68
69
            $this->afterSignal(FormValidationSignal::class)->dispatch();
70
        }
71
    }
72
73
    /**
74
     * Injects a callback in the form validator data object. The callback will
75
     * be called each time a field is validated by the validator, and will allow
76
     * to dispatch a field validation signal to other middlewares.
77
     */
78
    protected function injectFieldValidationCallback()
79
    {
80
        $callback = function (Field $field) {
81
            $this->afterSignal(FieldValidationSignal::class)
82
                ->withArguments(new FieldValidationArguments($field))
83
                ->dispatch();
84
        };
85
86
        $this->validator->getDataObject()->addFieldValidationCallback($callback);
87
    }
88
89
    /**
90
     * Injects the current step in the form validator.
91
     */
92
    protected function injectCurrentStep()
93
    {
94
        $currentStep = $this->getCurrentStep();
95
96
        if ($currentStep) {
97
            $this->validator->getDataObject()->setValidatedStep($currentStep);
98
        }
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function getAllowedSignals()
105
    {
106
        return [FormValidationSignal::class, FieldValidationSignal::class];
107
    }
108
}
109