Completed
Push — master ( 2fa681...c8ac20 )
by Constantin
03:18
created

CommandDispatcherWithValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 2
crap 1
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\CommandValidator;
10
use Gica\Cqrs\Command\Exception\CommandValidationFailed;
11
12
class CommandDispatcherWithValidator implements CommandDispatcher
13
{
14
15
    /**
16
     * @var CommandDispatcher
17
     */
18
    private $commandDispatcher;
19
    /**
20
     * @var CommandValidator
21
     */
22
    private $commandValidator;
23
24 2
    public function __construct(
25
        CommandDispatcher $commandDispatcher,
26
        CommandValidator $commandValidator
27
28
    )
29
    {
30 2
        $this->commandDispatcher = $commandDispatcher;
31 2
        $this->commandValidator = $commandValidator;
32 2
    }
33
34 2
    public function dispatchCommand(Command $command, $commandMetadata = null)
35
    {
36 2
        $errors = $this->commandValidator->validateCommand($command);
37
38 2
        if (!empty($errors)) {
39 1
            throw new CommandValidationFailed($errors);
40
        }
41
42 1
        $this->commandDispatcher->dispatchCommand($command);
43 1
    }
44
45 2
    public function canExecuteCommand(Command $command): bool
46
    {
47 2
        $errors = $this->commandValidator->validateCommand($command);
48
49 2
        if (!empty($errors)) {
50 1
            return false;
51
        }
52
53 1
        return $this->commandDispatcher->canExecuteCommand($command);
54
    }
55
}