1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gendoria\CommandQueueDoctrineDriverBundle\SendDriver; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Gendoria\CommandQueue\Command\CommandInterface; |
8
|
|
|
use Gendoria\CommandQueue\SendDriver\SendDriverInterface; |
9
|
|
|
use Gendoria\CommandQueue\Serializer\SerializerInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Command queue send driver using RabbitMQ server. |
13
|
|
|
* |
14
|
|
|
* @author Tomasz Struczyński <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class DoctrineSendDriver implements SendDriverInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Serializer instance. |
20
|
|
|
* |
21
|
|
|
* @var SerializerInterface |
22
|
|
|
*/ |
23
|
|
|
private $serializer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Doctrine connection. |
27
|
|
|
* |
28
|
|
|
* @var Connection |
29
|
|
|
*/ |
30
|
|
|
private $connection; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Table name. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $tableName; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Pool. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $pool; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Class constructor. |
48
|
|
|
* |
49
|
|
|
* @param SerializerInterface $serializer Serializer. |
50
|
|
|
* @param Connection $connection Database connection. |
51
|
|
|
* @param string $tableName Table name. |
52
|
|
|
* @param string $pool Pool name. |
53
|
|
|
*/ |
54
|
1 |
|
public function __construct(SerializerInterface $serializer, Connection $connection, $tableName, $pool) |
55
|
|
|
{ |
56
|
1 |
|
$this->serializer = $serializer; |
57
|
1 |
|
$this->connection = $connection; |
58
|
1 |
|
$this->tableName = (string)$tableName; |
59
|
1 |
|
$this->pool = (string)$pool; |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Send command using RabbitMQ server. |
64
|
|
|
* |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
1 |
|
public function send(CommandInterface $command) |
68
|
|
|
{ |
69
|
1 |
|
$serialized = $this->serializer->serialize($command); |
70
|
|
|
$data = array( |
71
|
1 |
|
'command_class' => $serialized->getCommandClass(), |
72
|
1 |
|
'command' => $serialized->getSerializedCommand(), |
73
|
1 |
|
'pool' => $this->pool, |
74
|
1 |
|
'failed_no' => 0, |
75
|
1 |
|
'processed' => false, |
76
|
1 |
|
'process_after' => new DateTime(), |
77
|
1 |
|
); |
78
|
|
|
$types = array( |
79
|
1 |
|
'string', |
80
|
1 |
|
'blob', |
81
|
1 |
|
'string', |
82
|
1 |
|
'integer', |
83
|
1 |
|
'boolean', |
84
|
1 |
|
'datetime', |
85
|
1 |
|
); |
86
|
1 |
|
$this->connection->insert($this->tableName, $data, $types); |
87
|
1 |
|
} |
88
|
|
|
} |
89
|
|
|
|