Completed
Push — master ( 2da130...d85d50 )
by Tomasz
04:10
created

ProcessorFactory   A

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