CommandValidator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 41
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A validateCommand() 0 19 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
}