Passed
Branch master (ee6374)
by Tomasz
04:42
created

ContainerAwareProcessorFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 72
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A registerProcessorIdForCommand() 0 8 2
A hasProcessor() 0 4 2
A getProcessor() 0 14 3
1
<?php
2
3
namespace Gendoria\CommandQueueBundle\ProcessorFactory;
4
5
use Gendoria\CommandQueue\Command\CommandInterface;
6
use Gendoria\CommandQueue\Exception\MultipleProcessorsException;
7
use Gendoria\CommandQueue\ProcessorFactory\ProcessorFactory;
8
use Gendoria\CommandQueue\ProcessorFactory\ProcessorNotFoundException;
9
use InvalidArgumentException;
10
use Psr\Log\LoggerInterface;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
/**
14
 * Container aware command queue processor factory.
15
 *
16
 * @author Tomasz Struczyński <[email protected]>
17
 */
18
class ContainerAwareProcessorFactory extends ProcessorFactory
19
{
20
    /**
21
     * Service container.
22
     *
23
     * @var ContainerInterface
24
     */
25
    private $container;
26
27
    /**
28
     * Registered services IDs.
29
     *
30
     * @var int[]
31
     */
32
    private $serviceIds = array();
33
34
    /**
35
     * Class constructor.
36
     *
37
     * @param ContainerInterface              $container A ContainerInterface instance
38
     * @param LoggerInterface Logger instance
39
     */
40 5
    public function __construct(ContainerInterface $container, LoggerInterface $logger = null)
41
    {
42 5
        parent::__construct($logger);
43 5
        $this->container = $container;
44 5
    }
45
46
    /**
47
     * Register processor service for command class.
48
     *
49
     * @param string $commandClassName
50
     * @param string $serviceId
51
     * @throws MultipleProcessorsException Thrown, when several processors are registered for given class.
52
     */
53 3
    public function registerProcessorIdForCommand($commandClassName, $serviceId)
54
    {
55
        //Maybe the processor is registered in parent
56 3
        if ($this->hasProcessor($commandClassName)) {
57 1
            throw new MultipleProcessorsException($commandClassName);
58
        }
59 3
        $this->serviceIds[$commandClassName] = $serviceId;
60 3
    }
61
    
62
    /**
63
     * {@inheritdoc}
64
     */
65 4
    public function hasProcessor($commandClassName)
66
    {
67 4
        return parent::hasProcessor($commandClassName) || array_key_exists($commandClassName, $this->serviceIds) ;
68 1
    }
69
    
70
    /**
71
     * Get processor for class name, if any is registered.
72
     *
73
     * {@inheritdoc}
74
     */
75 2
    public function getProcessor(CommandInterface $command)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
76
    {
77 2
        $className = get_class($command);
78
        try {
79 2
            return parent::getProcessor($command);
80 2
        } catch (ProcessorNotFoundException $ex) {
0 ignored issues
show
Bug introduced by
The class Gendoria\CommandQueue\Pr...cessorNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
81 2
            if (!array_key_exists($className, $this->serviceIds)) {
82 1
                throw new InvalidArgumentException('No processor registered for given type: '.$className.'.', 500, $ex);
83
            }
84 1
            $this->registerProcessorForCommand($className, $this->container->get($this->serviceIds[$className]));
85
        }
86
87 1
        return parent::getProcessor($command);
88
    }
89
}
90