|
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
|
|
|
public function __construct(ConnectionParameters $parameters) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->parameters = $parameters; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function connect() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->getStream()->reconnect(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getChannel(string $channelId = '') : ChannelInterface |
|
32
|
|
|
{ |
|
33
|
|
|
return new Channel($this->getStream()->channel($channelId)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function getStream() |
|
37
|
|
|
{ |
|
38
|
|
|
if (!$this->stream) { |
|
39
|
|
|
$this->stream = new AMQPStreamConnection( |
|
40
|
|
|
$this->parameters->getHost(), |
|
41
|
|
|
$this->parameters->getPort(), |
|
42
|
|
|
$this->parameters->getUser(), |
|
43
|
|
|
$this->parameters->getPassword(), |
|
44
|
|
|
$this->parameters->getVhost(), |
|
45
|
|
|
$this->parameters->shouldInsist(), |
|
46
|
|
|
$this->parameters->getLoginMethod(), |
|
47
|
|
|
$this->parameters->getLoginResponse(), |
|
48
|
|
|
$this->parameters->getLocale(), |
|
49
|
|
|
$this->parameters->getConnectionTimeout(), |
|
50
|
|
|
$this->parameters->getReadWriteTimeout(), |
|
51
|
|
|
$this->parameters->getContext(), |
|
52
|
|
|
$this->parameters->shouldKeepalive(), |
|
53
|
|
|
$this->parameters->getHeartbeat() |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->stream; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|