CommandDispatcherWithValidator::dispatchCommand()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
4
namespace Gica\Cqrs\Command\CommandDispatcher;
5
6
7
use Gica\Cqrs\Command;
8
use Gica\Cqrs\Command\CommandDispatcher;
9
use Gica\Cqrs\Command\CommandMetadata;
10
use Gica\Cqrs\Command\CommandValidator;
11
use Gica\Cqrs\Command\Exception\CommandValidationFailed;
12
13
class CommandDispatcherWithValidator implements CommandDispatcher
14
{
15
16
    /**
17
     * @var CommandDispatcher
18
     */
19
    private $commandDispatcher;
20
    /**
21
     * @var CommandValidator
22
     */
23
    private $commandValidator;
24
25 2
    public function __construct(
26
        CommandDispatcher $commandDispatcher,
27
        CommandValidator $commandValidator
28
29
    )
30
    {
31 2
        $this->commandDispatcher = $commandDispatcher;
32 2
        $this->commandValidator = $commandValidator;
33 2
    }
34
35 2
    public function dispatchCommand(Command $command, CommandMetadata $metadata = null)
36
    {
37 2
        $errors = $this->commandValidator->validateCommand($command);
38
39 2
        if (!empty($errors)) {
40 1
            throw new CommandValidationFailed($errors);
41
        }
42
43 1
        $this->commandDispatcher->dispatchCommand($command, $metadata);
44
    }
45
}