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

FormValidationMiddleware   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 20 3
A injectFieldValidationCallback() 0 10 1
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\FormValidation;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Form\Definition\Field\Field;
18
use Romm\Formz\Domain\Middleware\FieldValidation\FieldValidationArguments;
19
use Romm\Formz\Domain\Middleware\FieldValidation\FieldValidationSignal;
20
use Romm\Formz\Middleware\Element\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\Domain\Middleware\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
                ['form' => $formObject->getForm()]
65
            );
66
            $this->injectFieldValidationCallback();
67
68
            $this->validator->validate($formObject->getForm());
69
70
            $this->afterSignal(FormValidationSignal::class)->dispatch();
71
        }
72
    }
73
74
    /**
75
     * Injects a callback in the form validator data object. The callback will
76
     * be called each time a field is validated by the validator, and will allow
77
     * to dispatch a field validation signal to other middlewares.
78
     */
79
    protected function injectFieldValidationCallback()
80
    {
81
        $callback = function (Field $field) {
82
            $this->afterSignal(FieldValidationSignal::class)
83
                ->withArguments(new FieldValidationArguments($field))
84
                ->dispatch();
85
        };
86
87
        $this->validator->getDataObject()->addFieldValidationCallback($callback);
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function getAllowedSignals()
94
    {
95
        return [FormValidationSignal::class, FieldValidationSignal::class];
96
    }
97
}
98