1 | <?php |
||
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) |
|
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
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.