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