1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Da\Mailer\Queue\Backend\RabbitMq; |
4
|
|
|
|
5
|
|
|
use Da\Mailer\Queue\Backend\AbstractQueueStoreConnection; |
6
|
|
|
use PhpAmqpLib\Connection\AMQPStreamConnection; |
7
|
|
|
|
8
|
|
|
class RabbitMqQueueConnection extends AbstractQueueStoreConnection |
9
|
|
|
{ |
10
|
|
|
/** @var AMQPStreamConnection|null */ |
11
|
|
|
private $connection = null; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* RabbitMqQueueConnection constructor |
15
|
|
|
* |
16
|
|
|
* @param array $configuration |
17
|
|
|
* |
18
|
|
|
* refer to https://php-amqplib.github.io/php-amqplib/classes/PhpAmqpLib-Connection-AMQPStreamConnection.html#method___construct |
19
|
|
|
* for full list of configuration values |
20
|
|
|
*/ |
21
|
|
|
public function __construct(array $configuration = []) |
22
|
|
|
{ |
23
|
|
|
parent::__construct($configuration); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
|
|
public function connect() |
30
|
|
|
{ |
31
|
|
|
if (! is_null($this->connection)) { |
32
|
|
|
$this->disconnect(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->connection = new AMQPStreamConnection( |
36
|
|
|
$this->configuration['host'], |
37
|
|
|
$this->configuration['port'], |
38
|
|
|
$this->configuration['user'], |
39
|
|
|
$this->configuration['password'] |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$this->instance = $this->connection->channel(); |
43
|
|
|
$this->instance->confirm_select(); |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritDoc |
50
|
|
|
*/ |
51
|
|
|
public function getInstance() |
52
|
|
|
{ |
53
|
|
|
if (is_null($this->instance)) { |
54
|
|
|
$this->connect(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->instance; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function disconnect() |
61
|
|
|
{ |
62
|
|
|
if (is_null($this->connection)) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->instance->close(); |
67
|
|
|
$this->connection->close(); |
68
|
|
|
$this->instance = null; |
69
|
|
|
$this->connection = null; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|