1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gendoria\CommandQueue\SendDriver; |
4
|
|
|
|
5
|
|
|
use Gendoria\CommandQueue\Command\CommandInterface; |
6
|
|
|
use Gendoria\CommandQueue\ProcessorFactory\ProcessorFactoryInterface; |
7
|
|
|
use Gendoria\CommandQueue\ProcessorFactory\ProcessorNotFoundException; |
8
|
|
|
use Gendoria\CommandQueue\Worker\Exception\ProcessorErrorException; |
9
|
|
|
use Gendoria\CommandQueue\Worker\Exception\TranslateErrorException; |
10
|
|
|
use Gendoria\CommandQueue\Worker\WorkerInterface; |
11
|
|
|
use Psr\Log\LoggerAwareInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use Psr\Log\NullLogger; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Description of DirectProcessingDriver. |
17
|
|
|
* |
18
|
|
|
* @author Tomasz Struczyński <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class DirectProcessingDriver implements SendDriverInterface, WorkerInterface, LoggerAwareInterface |
21
|
|
|
{ |
22
|
|
|
use \Psr\Log\LoggerAwareTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Processor factory. |
26
|
|
|
* |
27
|
|
|
* @var ProcessorFactoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $processorFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class constructor. |
33
|
|
|
* |
34
|
|
|
* @param ProcessorFactoryInterface $processorFactory |
35
|
|
|
*/ |
36
|
6 |
|
public function __construct(ProcessorFactoryInterface $processorFactory, LoggerInterface $logger = null) |
37
|
|
|
{ |
38
|
6 |
|
$this->processorFactory = $processorFactory; |
39
|
6 |
|
$this->setLogger($logger ? $logger : new NullLogger()); |
40
|
6 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
4 |
|
public function send(CommandInterface $command) |
46
|
|
|
{ |
47
|
|
|
try { |
48
|
4 |
|
$this->process($command); |
49
|
4 |
|
} catch (ProcessorNotFoundException $e) { |
50
|
2 |
|
$this->logger->error('Exception while sending command: '.$e->getMessage()); |
51
|
3 |
|
} catch (\Exception $e) { |
52
|
1 |
|
$this->logger->error('Exception while sending command: '.$e->getMessage()); |
53
|
|
|
} |
54
|
4 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
6 |
|
public function process($command) |
60
|
|
|
{ |
61
|
6 |
|
if (!$command instanceof CommandInterface) { |
62
|
1 |
|
throw new TranslateErrorException($command, "Command has to be instance of command interface for direct processing driver", 500); |
63
|
|
|
} |
64
|
5 |
|
$processor = $this->getProcessor($command); |
65
|
|
|
try { |
66
|
2 |
|
$processor->process($command); |
67
|
2 |
|
} catch (\Exception $e) { |
68
|
1 |
|
throw new ProcessorErrorException($command, $processor, $e->getMessage(), $e->getCode(), $e); |
69
|
|
|
} |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
5 |
|
public function getProcessor(CommandInterface $command) |
76
|
|
|
{ |
77
|
5 |
|
$processor = $this->processorFactory->getProcessor($command); |
78
|
2 |
|
$processor->setLogger($this->logger); |
79
|
2 |
|
return $processor; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
6 |
|
public function setProcessorFactory(ProcessorFactoryInterface $processorFactory) |
86
|
|
|
{ |
87
|
6 |
|
$this->processorFactory = $processorFactory; |
88
|
6 |
|
} |
89
|
|
|
} |
90
|
|
|
|