|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\Transport\Subscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Cmobi\RabbitmqBundle\Connection\CmobiAMQPChannel; |
|
6
|
|
|
use Cmobi\RabbitmqBundle\Connection\CmobiAMQPConnectionInterface; |
|
7
|
|
|
use Cmobi\RabbitmqBundle\Connection\ConnectionManager; |
|
8
|
|
|
use Cmobi\RabbitmqBundle\Queue\CmobiAMQPMessage; |
|
9
|
|
|
use Cmobi\RabbitmqBundle\Queue\QueueProducerInterface; |
|
10
|
|
|
|
|
11
|
|
|
class Publisher implements QueueProducerInterface |
|
12
|
|
|
{ |
|
13
|
|
|
private $connectionManager; |
|
14
|
|
|
private $fromName; |
|
15
|
|
|
private $queueName; |
|
16
|
|
|
private $exchange; |
|
17
|
|
|
private $exchangeType; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct( |
|
20
|
|
|
$exchange, |
|
21
|
|
|
$exchangeType = ExchangeType::FANOUT, |
|
22
|
|
|
ConnectionManager $manager, |
|
23
|
|
|
$fromName, |
|
24
|
|
|
$queueName = '' |
|
25
|
|
|
) { |
|
26
|
|
|
$this->exchange = $exchange; |
|
27
|
|
|
$this->exchangeType = $exchangeType; |
|
28
|
|
|
$this->queueName = $queueName; |
|
29
|
|
|
$this->fromName = $fromName; |
|
30
|
|
|
$this->connectionManager = $manager; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param $data |
|
35
|
|
|
* @param $expire |
|
36
|
|
|
* @param $priority |
|
37
|
|
|
*/ |
|
38
|
|
|
public function publish($data, $expire = self::DEFAULT_TTL, $priority = self::PRIORITY_LOW) |
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
|
|
/** @var CmobiAMQPConnectionInterface $connection */ |
|
42
|
|
|
$connection = $this->connectionManager->getConnection(); |
|
43
|
|
|
$channel = $connection->channel(); |
|
44
|
|
|
$queueBag = new SubscriberQueueBag($this->getExchange(), $this->getExchangeType(), $this->getQueueName()); |
|
45
|
|
|
$channel->exchangeDeclare($queueBag->getExchangeDeclare()); |
|
|
|
|
|
|
46
|
|
|
$msg = new CmobiAMQPMessage((string) $data); |
|
47
|
|
|
$channel->basic_publish($msg, $queueBag->getExchange()); |
|
48
|
|
|
|
|
49
|
|
|
$channel->close(); |
|
50
|
|
|
$connection->close(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getQueueName() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->queueName; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Return caller name. |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getFromName() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->fromName; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getExchange() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->exchange; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getExchangeType() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->exchangeType; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|