1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DMT\CommandBus\Validator; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationException; |
6
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
7
|
|
|
use Doctrine\Common\Annotations\CachedReader; |
8
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
|
|
|
|
9
|
|
|
use League\Tactician\Middleware; |
10
|
|
|
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory; |
11
|
|
|
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader; |
12
|
|
|
use Symfony\Component\Validator\Mapping\Loader\LoaderChain; |
13
|
|
|
use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader; |
14
|
|
|
use Symfony\Component\Validator\Validator\RecursiveValidator; |
15
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
16
|
|
|
use Symfony\Component\Validator\ValidatorBuilder; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ValidationMiddleware |
20
|
|
|
* |
21
|
|
|
* @package DMT\Validation |
22
|
|
|
*/ |
23
|
|
|
class ValidationMiddleware implements Middleware |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ValidatorInterface |
27
|
|
|
*/ |
28
|
|
|
protected $validator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* ValidationMiddleware constructor. |
32
|
|
|
* |
33
|
|
|
* @param ValidatorInterface|null $validator |
34
|
|
|
* @throws AnnotationException |
35
|
|
|
*/ |
36
|
5 |
|
public function __construct(ValidatorInterface $validator = null) |
37
|
|
|
{ |
38
|
5 |
|
$this->validator = $validator ?? $this->getDefaultValidator(); |
39
|
5 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param object $command |
43
|
|
|
* @param callable $next |
44
|
|
|
* |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
5 |
|
public function execute($command, callable $next) |
48
|
|
|
{ |
49
|
5 |
|
$violations = $this->validator->validate($command); |
50
|
|
|
|
51
|
5 |
|
if ($violations->count() > 0) { |
52
|
4 |
|
throw new ValidationException( |
53
|
4 |
|
sprintf('Invalid command %s given', get_class($command)), |
54
|
4 |
|
0, |
55
|
4 |
|
null, |
56
|
4 |
|
$violations |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return $next($command); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get a default validator. |
65
|
|
|
* |
66
|
|
|
* By default the usage of annotations to validate object is off. To enable annotation configuration install |
67
|
|
|
* `doctrine/annotations`. |
68
|
|
|
* |
69
|
|
|
* @return RecursiveValidator|ValidatorInterface |
70
|
|
|
* @throws AnnotationException |
71
|
|
|
*/ |
72
|
2 |
|
protected function getDefaultValidator(): ValidatorInterface |
73
|
|
|
{ |
74
|
2 |
|
$loaders = [new StaticMethodLoader()]; |
75
|
|
|
|
76
|
2 |
|
if (class_exists(AnnotationReader::class)) { |
77
|
2 |
|
if (class_exists(ArrayCache::class)) { |
78
|
|
|
$loaders[] = new AnnotationLoader(new CachedReader(new AnnotationReader(), new ArrayCache())); |
|
|
|
|
79
|
|
|
} else { |
80
|
2 |
|
$loaders[] = new AnnotationLoader(new AnnotationReader()); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
return (new ValidatorBuilder()) |
85
|
2 |
|
->setMetadataFactory( |
86
|
2 |
|
new LazyLoadingMetadataFactory( |
87
|
2 |
|
new LoaderChain($loaders) |
88
|
|
|
) |
89
|
|
|
) |
90
|
2 |
|
->getValidator(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths