-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathValidatorTask.php
More file actions
142 lines (124 loc) · 4.33 KB
/
Copy pathValidatorTask.php
File metadata and controls
142 lines (124 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php declare(strict_types=1);
/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (C) 2017-2021 Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\ProcessBundle\Task\Validation;
use CleverAge\ProcessBundle\Model\AbstractConfigurableTask;
use CleverAge\ProcessBundle\Model\ProcessState;
use CleverAge\ProcessBundle\Validator\ConstraintLoader;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Validate the input and pass it to the output
*
* @author Valentin Clavreul <vclavreul@clever-age.com>
* @author Vincent Chalnot <vchalnot@clever-age.com>
*/
class ValidatorTask extends AbstractConfigurableTask
{
/** @var LoggerInterface */
protected $logger;
/** @var ValidatorInterface */
protected $validator;
/**
* @param LoggerInterface $logger
* @param ValidatorInterface $validator
*/
public function __construct(LoggerInterface $logger, ValidatorInterface $validator)
{
$this->logger = $logger;
$this->validator = $validator;
}
/**
* @param ProcessState $state
*
* @throws ExceptionInterface
* @throws \UnexpectedValueException
*/
public function execute(ProcessState $state)
{
$options = $this->getOptions($state);
$violations = $this->validator->validate(
$state->getInput(),
$options['constraints'],
$options['groups']
);
if (0 < $violations->count()) {
/** @var $violation ConstraintViolationInterface */
foreach ($violations as $violation) {
$invalidValue = $violation->getInvalidValue();
$logContext = [
'property' => $violation->getPropertyPath(),
'violation_code' => $violation->getCode(),
'invalid_value' => $invalidValue,
];
if ($options['log_errors']) {
$this->logger->log($options['log_errors'], $violation->getMessage(), $logContext);
}
}
if ($options['error_output_violations']) {
$state->setErrorOutput($violations);
$state->setSkipped(true);
return;
}
throw new \UnexpectedValueException("{$violations->count()} constraint violations detected on validation");
}
$state->setOutput($state->getInput());
}
/**
* {@inheritDoc}
*/
protected function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('log_errors', LogLevel::CRITICAL);
$resolver->setAllowedValues(
'log_errors',
[
LogLevel::ALERT,
LogLevel::CRITICAL,
LogLevel::DEBUG,
LogLevel::EMERGENCY,
LogLevel::ERROR,
LogLevel::INFO,
LogLevel::NOTICE,
LogLevel::WARNING,
true,
false,
]
);
$resolver->setNormalizer(
'log_errors',
static function (Options $options, $value) {
if (true === $value) {
return LogLevel::CRITICAL;
}
return $value;
}
);
$resolver->setDefault('groups', null);
$resolver->setAllowedTypes('groups', ['null', 'array']);
$resolver->setDefault('constraints', null);
$resolver->setAllowedTypes('constraints', ['null', 'array']);
$resolver->setNormalizer(
'constraints',
static function (Options $options, $constraints) {
if (null === $constraints) {
return null;
}
return (new ConstraintLoader())->buildConstraints($constraints);
}
);
$resolver->setDefault('error_output_violations', false);
$resolver->setAllowedTypes('error_output_violations', ['bool']);
}
}