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; |
9
|
|
|
use Gendoria\CommandQueue\ProcessorFactoryInterface; |
10
|
|
|
use Gendoria\CommandQueue\ProcessorNotFoundException; |
11
|
|
|
use Gendoria\CommandQueue\Worker\Exception\ProcessorErrorException; |
12
|
|
|
use Gendoria\CommandQueue\Worker\Exception\TranslateErrorException; |
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
use Psr\Log\NullLogger; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Base command queue worker |
18
|
|
|
* |
19
|
|
|
* @author Tomasz Struczyński <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class BaseWorker implements WorkerInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Processor factory instance. |
26
|
|
|
* |
27
|
|
|
* @var ProcessorFactoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $processorFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Logger instance. |
33
|
|
|
* |
34
|
|
|
* @var LoggerInterface |
35
|
|
|
*/ |
36
|
|
|
private $logger; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Class constructor. |
40
|
|
|
* |
41
|
|
|
* @param ProcessorFactoryInterface $processorFactory |
42
|
|
|
* @param LoggerInterface $logger Logger instance. |
43
|
|
|
*/ |
44
|
4 |
|
public function __construct(ProcessorFactoryInterface $processorFactory, LoggerInterface $logger = null) |
45
|
|
|
{ |
46
|
4 |
|
$this->processorFactory = $processorFactory; |
47
|
4 |
|
$this->logger = $logger ? $logger : new NullLogger(); |
48
|
4 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
* @param mixed $commandData |
53
|
|
|
* @return void |
54
|
|
|
* |
55
|
|
|
* @throws TranslateErrorException Thrown, when translation process resulted in an error. |
56
|
|
|
* @throws ProcessorNotFoundException Thrown, when processor for given command has not been found. |
57
|
|
|
* @throws ProcessorErrorException Thrown, when processor resulted in an error. |
58
|
|
|
*/ |
59
|
4 |
|
public function process($commandData) |
60
|
|
|
{ |
61
|
4 |
|
$this->beforeTranslateHook($commandData); |
62
|
|
|
try { |
63
|
4 |
|
$command = $this->translateCommand($commandData); |
64
|
4 |
|
} catch (Exception $e) { |
65
|
1 |
|
throw new TranslateErrorException($commandData, $e->getMessage(), $e->getCode(), $e); |
66
|
|
|
} |
67
|
3 |
|
$this->beforeGetProcessorHook($command); |
68
|
3 |
|
$processor = $this->getProcessor($command); |
69
|
2 |
|
$this->beforeProcessHook($command, $processor); |
70
|
|
|
try { |
71
|
2 |
|
$processor->process($command); |
72
|
2 |
|
} catch (Exception $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
|
|
|
public function setProcessorFactory(ProcessorFactoryInterface $processorFactory) |
92
|
|
|
{ |
93
|
|
|
$this->processorFactory = $processorFactory; |
94
|
|
|
} |
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
|
|
|
protected abstract function translateCommand($commandData); |
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Hook called before command translation. |
108
|
|
|
* |
109
|
|
|
* @param string $commandData |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
protected function beforeTranslateHook(&$commandData) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Hook called before getting processor for command. |
118
|
|
|
* |
119
|
|
|
* @param CommandInterface $command |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
protected function beforeGetProcessorHook(CommandInterface $command) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Hook called before processing command. |
128
|
|
|
* |
129
|
|
|
* @param CommandInterface $command |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
protected function beforeProcessHook(CommandInterface $command, CommandProcessorInterface $processor) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Hook called after successfull processing of command. |
138
|
|
|
* |
139
|
|
|
* @param CommandInterface $command |
140
|
|
|
* @param CommandProcessorInterface $processor |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
protected function afterProcessHook(CommandInterface $command, CommandProcessorInterface $processor) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|