ProcessorFactory   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 64
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A registerProcessorForCommand() 0 14 4
A getProcessor() 0 8 3
A hasProcessor() 0 4 1
1
<?php
2
3
namespace Gendoria\CommandQueue\ProcessorFactory;
4
5
use Gendoria\CommandQueue\Command\CommandInterface;
6
use Gendoria\CommandQueue\CommandProcessor\CommandProcessorInterface;
7
use Gendoria\CommandQueue\ProcessorFactory\Exception\MultipleProcessorsException;
8
use Gendoria\CommandQueue\ProcessorFactory\Exception\ProcessorNotFoundException;
9
use InvalidArgumentException;
10
use Psr\Log\LoggerInterface;
11
use Psr\Log\NullLogger;
12
use ReflectionClass;
13
14
/**
15
 * Command Processor factory - used for registering and retrieving command processor.
16
 *
17
 * @author Tomasz Struczyński <[email protected]>
18
 */
19
class ProcessorFactory implements ProcessorFactoryInterface
20
{
21
    /**
22
     * Array of registered command processors.
23
     *
24
     * @var CommandProcessorInterface[]
25
     */
26
    private $commandTypes = array();
27
28
    /**
29
     * Logger instance.
30
     *
31
     * @var LoggerInterface
32
     */
33
    private $logger;
34
35
    /**
36
     * Class constructor.
37
     *
38
     * @param LoggerInterface $logger
39
     */
40 12
    public function __construct(LoggerInterface $logger = null)
41
    {
42 12
        $this->logger = $logger ? $logger : new NullLogger();
43 12
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 7
    public function registerProcessorForCommand($commandClassName, CommandProcessorInterface $service)
49
    {
50 7
        if (!class_exists($commandClassName)) {
51 1
            throw new InvalidArgumentException('Registering service for non existing command class.');
52
        }
53 6
        $classData = new ReflectionClass($commandClassName);
54 6
        if (!$classData->implementsInterface('Gendoria\CommandQueue\Command\CommandInterface')) {
55 1
            throw new InvalidArgumentException('Command class has to implement CommandInterface interface.');
56
        }
57 5
        if (array_key_exists($commandClassName, $this->commandTypes)) {
58 1
            throw new MultipleProcessorsException($commandClassName);
59
        }
60 5
        $this->commandTypes[$commandClassName] = $service;
61 5
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 7
    public function getProcessor(CommandInterface $command)
67
    {
68 7
        if (!array_key_exists(get_class($command), $this->commandTypes) || !$this->commandTypes[get_class($command)]->supports($command)) {
69 4
            throw new ProcessorNotFoundException('No processor registered for given command class: '.get_class($command));
70
        }
71
72 3
        return $this->commandTypes[get_class($command)];
73
    }
74
    
75
    /**
76
     * {@inheritdoc}
77
     */
78 1
    public function hasProcessor($commandClassName)
79
    {
80 1
        return array_key_exists($commandClassName, $this->commandTypes);
81
    }
82
}
83