ValidateCommandMiddleware   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 25
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 15 2
1
<?php declare(strict_types=1);
2
3
namespace hiapi\middlewares;
4
5
use hiapi\commands\BaseCommand;
6
use hiapi\exceptions\domain\ValidationException;
7
use League\Tactician\Middleware;
8
9
/**
10
 * Class ValidateCommandMiddleware
11
 *
12
 * @author Dmytro Naumenko <[email protected]>
13
 */
14
class ValidateCommandMiddleware implements Middleware
15
{
16
    /**
17
     * @param object|BaseCommand $command
18
     * @param callable $next
19
     *
20
     * @return mixed
21
     * @throws ValidationException when data is not valid
22
     */
23
    public function execute($command, callable $next)
24
    {
25
        if (!$command->validate()) {
26
            // TODO: enhance handling of multiple errors in the same command.
27
            $errors = $command->getFirstErrors();
28
29
            throw new ValidationException(sprintf(
30
                '%s: %s',
31
                array_key_first($errors),
32
                reset($errors)
33
            ));
34
        }
35
36
        return $next($command);
37
    }
38
}
39