1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gendoria\CommandQueue\Worker; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Gendoria\CommandQueue\Command\CommandInterface; |
7
|
|
|
use Gendoria\CommandQueue\CommandProcessor\CommandProcessorInterface; |
8
|
|
|
use Gendoria\CommandQueue\ProcessorFactory\Exception\ProcessorNotFoundException; |
9
|
|
|
use Gendoria\CommandQueue\ProcessorFactory\ProcessorFactoryInterface; |
10
|
|
|
use Gendoria\CommandQueue\Serializer\Exception\UnserializeErrorException; |
11
|
|
|
use Gendoria\CommandQueue\Serializer\SerializedCommandData; |
12
|
|
|
use Gendoria\CommandQueue\Serializer\SerializerInterface; |
13
|
|
|
use Gendoria\CommandQueue\Worker\Exception\ProcessorErrorException; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use Psr\Log\NullLogger; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Base command queue worker |
19
|
|
|
* |
20
|
|
|
* @author Tomasz Struczyński <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
abstract class BaseWorker implements WorkerInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Processor factory instance. |
27
|
|
|
* |
28
|
|
|
* @var ProcessorFactoryInterface |
29
|
|
|
*/ |
30
|
|
|
protected $processorFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Logger instance. |
34
|
|
|
* |
35
|
|
|
* @var LoggerInterface |
36
|
|
|
*/ |
37
|
|
|
protected $logger; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Serializer. |
41
|
|
|
* |
42
|
|
|
* @var SerializerInterface |
43
|
|
|
*/ |
44
|
|
|
protected $serializer; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Class constructor. |
48
|
|
|
* |
49
|
|
|
* @param ProcessorFactoryInterface $processorFactory Command processor factory. |
50
|
|
|
* @param SerializerInterface $serializer Serializer. |
51
|
|
|
* @param LoggerInterface $logger Logger. |
52
|
|
|
*/ |
53
|
4 |
|
public function __construct(ProcessorFactoryInterface $processorFactory, SerializerInterface $serializer, LoggerInterface $logger = null) |
54
|
|
|
{ |
55
|
4 |
|
$this->processorFactory = $processorFactory; |
56
|
4 |
|
$this->serializer = $serializer; |
57
|
4 |
|
$this->logger = $logger ? $logger : new NullLogger(); |
58
|
4 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
* @param mixed $commandData |
63
|
|
|
* @return void |
64
|
|
|
* |
65
|
|
|
* @throws UnserializeErrorException Thrown, when translation process resulted in an error. |
66
|
|
|
* @throws ProcessorNotFoundException Thrown, when processor for given command has not been found. |
67
|
|
|
* @throws ProcessorErrorException Thrown, when processor resulted in an error. |
68
|
|
|
*/ |
69
|
4 |
|
public function process($commandData) |
70
|
|
|
{ |
71
|
4 |
|
$this->beforeTranslateHook($commandData); |
72
|
4 |
|
$command = $this->serializer->unserialize($this->getSerializedCommandData($commandData)); |
73
|
3 |
|
$this->beforeGetProcessorHook($command); |
74
|
3 |
|
$processor = $this->getProcessor($command); |
75
|
2 |
|
$this->beforeProcessHook($command, $processor); |
76
|
|
|
try { |
77
|
2 |
|
$processor->process($command); |
78
|
2 |
|
} catch (Exception $e) { |
79
|
1 |
|
$this->processorErrorHook($command, $processor, $e); |
80
|
1 |
|
throw new ProcessorErrorException($command, $processor, $e->getMessage(), $e->getCode(), $e); |
81
|
|
|
} |
82
|
1 |
|
$this->afterProcessHook($command, $processor); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
3 |
|
public function getProcessor(CommandInterface $command) |
89
|
|
|
{ |
90
|
3 |
|
$processor = $this->processorFactory->getProcessor($command); |
91
|
2 |
|
$processor->setLogger($this->logger); |
92
|
2 |
|
return $processor; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
1 |
|
public function setProcessorFactory(ProcessorFactoryInterface $processorFactory) |
99
|
|
|
{ |
100
|
1 |
|
$this->processorFactory = $processorFactory; |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get serialized command data to use with translator. |
105
|
|
|
* |
106
|
|
|
* @param mixed $commandData |
107
|
|
|
* @return SerializedCommandData |
108
|
|
|
* |
109
|
|
|
* @throws UnserializeErrorException Thrown, when data could not have been translated to serialized command data. |
110
|
|
|
*/ |
111
|
|
|
abstract protected function getSerializedCommandData($commandData); |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Hook called before command translation. |
115
|
|
|
* |
116
|
|
|
* @param string $commandData |
117
|
|
|
* @return void |
118
|
|
|
* |
119
|
|
|
* @codeCoverageIgnore |
120
|
|
|
*/ |
121
|
|
|
protected function beforeTranslateHook(&$commandData) |
122
|
|
|
{ |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Hook called before getting processor for command. |
127
|
|
|
* |
128
|
|
|
* @param CommandInterface $command |
129
|
|
|
* @return void |
130
|
|
|
* |
131
|
|
|
* @codeCoverageIgnore |
132
|
|
|
*/ |
133
|
|
|
protected function beforeGetProcessorHook(CommandInterface $command) |
134
|
|
|
{ |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Hook called before processing command. |
139
|
|
|
* |
140
|
|
|
* @param CommandInterface $command |
141
|
|
|
* @return void |
142
|
|
|
* |
143
|
|
|
* @codeCoverageIgnore |
144
|
|
|
*/ |
145
|
|
|
protected function beforeProcessHook(CommandInterface $command, CommandProcessorInterface $processor) |
146
|
|
|
{ |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Hook called after successfull processing of command. |
151
|
|
|
* |
152
|
|
|
* @param CommandInterface $command |
153
|
|
|
* @param CommandProcessorInterface $processor |
154
|
|
|
* @return void |
155
|
|
|
* |
156
|
|
|
* @codeCoverageIgnore |
157
|
|
|
*/ |
158
|
|
|
protected function afterProcessHook(CommandInterface $command, CommandProcessorInterface $processor) |
159
|
|
|
{ |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Hook called after successfull processing of command. |
164
|
|
|
* |
165
|
|
|
* @param CommandInterface $command |
166
|
|
|
* @param CommandProcessorInterface $processor |
167
|
|
|
* @param Exception $e Exception thrown by processor. |
168
|
|
|
* @return void |
169
|
|
|
* |
170
|
|
|
* @codeCoverageIgnore |
171
|
|
|
*/ |
172
|
|
|
protected function processorErrorHook(CommandInterface $command, CommandProcessorInterface $processor, Exception $e) |
173
|
|
|
{ |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|