1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gendoria\CommandQueueBundle\ProcessorFactory; |
4
|
|
|
|
5
|
|
|
use Gendoria\CommandQueue\Command\CommandInterface; |
6
|
|
|
use Gendoria\CommandQueue\ProcessorFactory\Exception\MultipleProcessorsException; |
7
|
|
|
use Gendoria\CommandQueue\ProcessorFactory\Exception\ProcessorNotFoundException; |
8
|
|
|
use Gendoria\CommandQueue\ProcessorFactory\ProcessorFactory; |
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
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get processor for class name, if any is registered. |
72
|
|
|
* |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
2 |
|
public function getProcessor(CommandInterface $command) |
76
|
|
|
{ |
77
|
2 |
|
$className = get_class($command); |
78
|
|
|
try { |
79
|
2 |
|
return parent::getProcessor($command); |
80
|
2 |
|
} catch (ProcessorNotFoundException $ex) { |
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
|
|
|
|