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