ValidationMiddleware   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 21
c 2
b 0
f 0
dl 0
loc 68
ccs 22
cts 23
cp 0.9565
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultValidator() 0 19 3
A __construct() 0 3 1
A execute() 0 14 2
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;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Cache\ArrayCache was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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()));
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Annotations\CachedReader has been deprecated: the CachedReader is deprecated and will be removed in version 2.0.0 of doctrine/annotations. Please use the {@see \Doctrine\Common\Annotations\PsrCachedReader} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

78
                $loaders[] = new AnnotationLoader(/** @scrutinizer ignore-deprecated */ new CachedReader(new AnnotationReader(), new ArrayCache()));
Loading history...
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