ValidatorMiddleware::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace League\Tactician\Bundle\Middleware;
4
5
use League\Tactician\Middleware;
6
use Symfony\Component\Validator\Validator\ValidatorInterface;
7
8
class ValidatorMiddleware implements Middleware
9
{
10
    /**
11
     * @var ValidatorInterface
12
     */
13
    protected $validator;
14
15
    /**
16
     * @param ValidatorInterface $validator
17
     */
18 12
    public function __construct(ValidatorInterface $validator)
19
    {
20 12
        $this->validator = $validator;
21 12
    }
22
23
    /**
24
     * @param object $command
25
     * @param callable $next
26
     *
27
     * @return mixed
28
     *
29
     * @throws InvalidCommandException
30
     */
31 12
    public function execute($command, callable $next)
32
    {
33 12
        $constraintViolations = $this->validator->validate($command);
34
35 12
        if (count($constraintViolations) > 0) {
36 3
            throw InvalidCommandException::onCommand($command, $constraintViolations);
37
        }
38
39 9
        return $next($command);
40
    }
41
}
42
43