1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OldSound\RabbitMqBundle\RabbitMq; |
4
|
|
|
|
5
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\Exception\ValidationException; |
6
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
7
|
|
|
use PhpAmqpLib\Wire\AMQPTable; |
8
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\JsonValidator; |
9
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\XmlValidator; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Producer, that publishes AMQP Messages |
13
|
|
|
*/ |
14
|
|
|
class Producer extends BaseAmqp implements ProducerInterface |
15
|
|
|
{ |
16
|
|
|
protected $contentType = 'text/plain'; |
17
|
|
|
protected $deliveryMode = 2; |
18
|
|
|
protected $defaultRoutingKey = ''; |
19
|
|
|
public $validator = null; |
20
|
|
|
|
21
|
|
|
public function setValidator($validator) |
22
|
|
|
{ |
23
|
|
|
$this->validator = $validator; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function setContentType($contentType) |
27
|
|
|
{ |
28
|
|
|
$this->contentType = $contentType; |
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setDeliveryMode($deliveryMode) |
34
|
|
|
{ |
35
|
|
|
$this->deliveryMode = $deliveryMode; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setDefaultRoutingKey($defaultRoutingKey) |
41
|
|
|
{ |
42
|
|
|
$this->defaultRoutingKey = $defaultRoutingKey; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function getBasicProperties() |
48
|
|
|
{ |
49
|
|
|
return array('content_type' => $this->contentType, 'delivery_mode' => $this->deliveryMode); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function validateMessage($msg) |
53
|
|
|
{ |
54
|
|
|
if ($this->contentType != $this->validator->getContentType()) { |
55
|
|
|
throw new ValidationException($this->contentType . " message verification failed"); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$error = $this->validator->isValid($msg, $this->contentType); |
59
|
|
|
if ($error != null){ |
60
|
|
|
throw new ValidationException($this->contentType . " message verification failed. Error was: " . $error); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Publishes the message and merges additional properties with basic properties |
66
|
|
|
* |
67
|
|
|
* @param string $msgBody |
68
|
|
|
* @param string $routingKey |
69
|
|
|
* @param array $additionalProperties |
70
|
|
|
* @param array $headers |
71
|
|
|
*/ |
72
|
|
|
public function publish($msgBody, $routingKey = null, $additionalProperties = array(), array $headers = null) |
73
|
|
|
{ |
74
|
|
|
if ($this->validator != null){ |
75
|
|
|
$this->validateMessage($msgBody); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($this->autoSetupFabric) { |
79
|
|
|
$this->setupFabric(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$msg = new AMQPMessage((string) $msgBody, array_merge($this->getBasicProperties(), $additionalProperties)); |
83
|
|
|
|
84
|
|
|
if (!empty($headers)) { |
85
|
|
|
$headersTable = new AMQPTable($headers); |
86
|
|
|
$msg->set('application_headers', $headersTable); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$real_routingKey = $routingKey !== null ? $routingKey : $this->defaultRoutingKey; |
90
|
|
|
$this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$real_routingKey); |
91
|
|
|
$this->logger->debug('AMQP message published', array( |
92
|
|
|
'amqp' => array( |
93
|
|
|
'body' => $msgBody, |
94
|
|
|
'routingkeys' => $routingKey, |
95
|
|
|
'properties' => $additionalProperties, |
96
|
|
|
'headers' => $headers |
97
|
|
|
) |
98
|
|
|
)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|