CommandValidator::validateCommand()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
crap 2
1
<?php
2
/******************************************************************************
3
 * Copyright (c) 2016 Constantin Galbenu <[email protected]>             *
4
 ******************************************************************************/
5
6
namespace Gica\Cqrs\Command;
7
8
9
use Gica\Cqrs\Command;
10
use Gica\Cqrs\Command\CommandValidation\CommandValidatorSubscriber;
11
use Gica\Cqrs\Command\ValueObject\CommandHandlerDescriptor;
12
use Gica\Dependency\AbstractFactory;
13
14
class CommandValidator
15
{
16
17
    /**
18
     * @var CommandValidatorSubscriber
19
     */
20
    private $commandValidatorSubscriber;
21
    /**
22
     * @var AbstractFactory
23
     */
24
    private $abstractFactory;
25
26 1
    public function __construct(
27
        CommandValidatorSubscriber $subscriber,
28
        AbstractFactory $abstractFactory
29
    )
30
    {
31 1
        $this->commandValidatorSubscriber = $subscriber;
32 1
        $this->abstractFactory = $abstractFactory;
33 1
    }
34
35 1
    public function validateCommand(Command $command)
36
    {
37 1
        $handlerDescriptors = $this->commandValidatorSubscriber->getHandlersForCommand($command);
38
39 1
        $errors = [];
40
41
        /** @var CommandHandlerDescriptor $handlerDescriptor */
42 1
        foreach ($handlerDescriptors as $handlerDescriptor) {
43 1
            $handler = $this->abstractFactory->createObject($handlerDescriptor->getHandlerClass());
44
45 1
            $generator = call_user_func([$handler, $handlerDescriptor->getMethodName()], $command);
46
47 1
            $ownErrors = iterator_to_array($generator, false);
48
49 1
            $errors = array_merge($errors, $ownErrors);
50
        }
51
52 1
        return $errors;
53
    }
54
}