1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ccovey\RabbitMQ\Connection; |
4
|
|
|
|
5
|
|
|
use Ccovey\RabbitMQ\Channel; |
6
|
|
|
use Ccovey\RabbitMQ\ChannelInterface; |
7
|
|
|
use PhpAmqpLib\Connection\AMQPStreamConnection; |
8
|
|
|
|
9
|
|
|
class Connection implements ConnectionInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var ConnectionParameters |
13
|
|
|
*/ |
14
|
|
|
private $parameters; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var AMQPStreamConnection |
18
|
|
|
*/ |
19
|
|
|
private $stream; |
20
|
|
|
|
21
|
3 |
|
public function __construct(ConnectionParameters $parameters) |
22
|
|
|
{ |
23
|
3 |
|
$this->parameters = $parameters; |
24
|
3 |
|
} |
25
|
|
|
|
26
|
1 |
|
public function connect() |
27
|
|
|
{ |
28
|
1 |
|
$this->getStream()->reconnect(); |
29
|
1 |
|
} |
30
|
|
|
|
31
|
2 |
|
public function getChannel(string $channelId = '') : ChannelInterface |
32
|
|
|
{ |
33
|
2 |
|
return new Channel($this->getStream()->channel($channelId)); |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
protected function getStream() |
37
|
|
|
{ |
38
|
1 |
|
if (!$this->stream) { |
39
|
1 |
|
$this->stream = new AMQPStreamConnection( |
40
|
1 |
|
$this->parameters->getHost(), |
41
|
1 |
|
$this->parameters->getPort(), |
42
|
1 |
|
$this->parameters->getUser(), |
43
|
1 |
|
$this->parameters->getPassword(), |
44
|
1 |
|
$this->parameters->getVhost(), |
45
|
1 |
|
$this->parameters->shouldInsist(), |
46
|
1 |
|
$this->parameters->getLoginMethod(), |
47
|
1 |
|
$this->parameters->getLoginResponse(), |
48
|
1 |
|
$this->parameters->getLocale(), |
49
|
1 |
|
$this->parameters->getConnectionTimeout(), |
50
|
1 |
|
$this->parameters->getReadWriteTimeout(), |
51
|
1 |
|
$this->parameters->getContext(), |
52
|
1 |
|
$this->parameters->shouldKeepalive(), |
53
|
1 |
|
$this->parameters->getHeartbeat() |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
return $this->stream; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|