1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\Transport\PubSub; |
4
|
|
|
|
5
|
|
|
use Cmobi\RabbitmqBundle\Connection\ConnectionManager; |
6
|
|
|
use Cmobi\RabbitmqBundle\Connection\Exception\InvalidAMQPChannelException; |
7
|
|
|
use Cmobi\RabbitmqBundle\Queue\Queue; |
8
|
|
|
use Cmobi\RabbitmqBundle\Queue\QueueBuilderInterface; |
9
|
|
|
use Cmobi\RabbitmqBundle\Queue\QueueServiceInterface; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
|
12
|
|
|
class SubscriberBuilder implements QueueBuilderInterface |
13
|
|
|
{ |
14
|
|
|
private $exchangeName; |
15
|
|
|
private $exchangeType; |
16
|
|
|
private $connectionManager; |
17
|
|
|
private $logger; |
18
|
|
|
private $parameters; |
19
|
|
|
|
20
|
|
|
public function __construct( |
21
|
|
|
$exchangeName, |
22
|
|
|
$exchangeType = ExchangeType::FANOUT, |
23
|
|
|
ConnectionManager $connManager, |
24
|
|
|
LoggerInterface $logger, |
25
|
|
|
array $parameters |
26
|
|
|
) { |
27
|
|
|
$this->exchangeName = $exchangeName; |
28
|
|
|
$this->exchangeType = $exchangeType; |
29
|
|
|
$this->connectionManager = $connManager; |
30
|
|
|
$this->logger = $logger; |
31
|
|
|
$this->parameters = $parameters; |
32
|
|
|
$this->channel = null; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $queueName |
37
|
|
|
* @param QueueServiceInterface $queueService |
38
|
|
|
* |
39
|
|
|
* @return Queue |
40
|
|
|
* |
41
|
|
|
* @throws InvalidAMQPChannelException |
42
|
|
|
*/ |
43
|
|
|
public function buildQueue($queueName, QueueServiceInterface $queueService) |
44
|
|
|
{ |
45
|
|
|
$qos = 1; |
46
|
|
|
|
47
|
|
|
if (array_key_exists('cmobi_rabbitmq.basic_qos', $this->parameters)) { |
48
|
|
|
$qos = $this->parameters['cmobi_rabbitmq.basic_qos']; |
49
|
|
|
} |
50
|
|
|
$subQueueBag = new SubscriberQueueBag($this->getExchangeName(), $this->getExchangeType(), $queueName, $qos); |
51
|
|
|
|
52
|
|
|
$queue = new Queue($this->getConnectionManager(), $subQueueBag, $this->logger); |
53
|
|
|
$queueCallback = new SubscriberQueueCallback($queueService); |
54
|
|
|
$queue->setCallback($queueCallback); |
55
|
|
|
|
56
|
|
|
return $queue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return ConnectionManager |
61
|
|
|
*/ |
62
|
|
|
public function getConnectionManager() |
63
|
|
|
{ |
64
|
|
|
return $this->connectionManager; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string|false |
69
|
|
|
*/ |
70
|
|
|
public function getExchangeName() |
71
|
|
|
{ |
72
|
|
|
return $this->exchangeName; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string|false |
77
|
|
|
*/ |
78
|
|
|
public function getExchangeType() |
79
|
|
|
{ |
80
|
|
|
return $this->exchangeType; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: