1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AMQPAL\Adapter\PhpAmqpLib; |
4
|
|
|
|
5
|
|
|
use AMQPAL\Adapter\Exception; |
6
|
|
|
use AMQPAL\Adapter\ConnectionInterface; |
7
|
|
|
use AMQPAL\Adapter\PhpAmqpLib\Options\ConnectionOptions; |
8
|
|
|
use PhpAmqpLib\Connection\AbstractConnection; |
9
|
|
|
use Traversable; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Connection |
13
|
|
|
* |
14
|
|
|
* @package AMQPAL\Adapter\PhpAmqpLib |
15
|
|
|
*/ |
16
|
|
|
class Connection implements ConnectionInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var AbstractConnection |
20
|
|
|
*/ |
21
|
|
|
protected $resource; |
22
|
|
|
/** |
23
|
|
|
* @var ConnectionOptions |
24
|
|
|
*/ |
25
|
|
|
protected $options; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Connection constructor. |
29
|
|
|
* |
30
|
|
|
* @param AbstractConnection|ConnectionOptions $connection |
31
|
|
|
* @throws Exception\RuntimeException |
32
|
|
|
* @throws Exception\InvalidArgumentException |
33
|
|
|
* @throws Exception\BadMethodCallException |
34
|
|
|
*/ |
35
|
7 |
|
public function __construct($connection) |
36
|
|
|
{ |
37
|
7 |
|
if (!$connection instanceof AbstractConnection) { |
38
|
|
|
$this->setOptions($connection); |
39
|
|
|
$connection = $this->createResource(); |
40
|
|
|
} |
41
|
|
|
|
42
|
7 |
|
$this->setResource($connection); |
43
|
7 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return ConnectionOptions |
47
|
|
|
*/ |
48
|
1 |
|
public function getOptions() |
49
|
|
|
{ |
50
|
1 |
|
return $this->options; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param ConnectionOptions|Traversable|array $options |
55
|
|
|
* @return $this |
56
|
|
|
* @throws Exception\InvalidArgumentException |
57
|
|
|
* @throws Exception\BadMethodCallException |
58
|
|
|
*/ |
59
|
2 |
|
public function setOptions($options) |
60
|
|
|
{ |
61
|
2 |
|
if (!$options instanceof ConnectionOptions) { |
62
|
1 |
|
$options = new ConnectionOptions($options); |
63
|
|
|
} |
64
|
2 |
|
$this->options = $options; |
65
|
|
|
|
66
|
2 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return AbstractConnection |
71
|
|
|
* @throws Exception\RuntimeException |
72
|
|
|
* @throws Exception\InvalidArgumentException |
73
|
|
|
*/ |
74
|
|
|
protected function createResource() |
75
|
|
|
{ |
76
|
|
|
$factory = $this->getOptions() |
77
|
|
|
->getConnectionFactoryFactory() |
78
|
|
|
->createFactory($this->getOptions()->getType()); |
79
|
|
|
return $factory->createConnection($this->getOptions()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param AbstractConnection $resource |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
7 |
|
public function setResource(AbstractConnection $resource) |
87
|
|
|
{ |
88
|
7 |
|
$this->resource = $resource; |
89
|
7 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return AbstractConnection |
94
|
|
|
*/ |
95
|
|
|
public function getResource() |
96
|
|
|
{ |
97
|
|
|
return $this->resource; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Establish a connection with the AMQP broker. |
102
|
|
|
* |
103
|
|
|
* @return $this |
104
|
|
|
* @throws Exception\RuntimeException |
105
|
|
|
* @throws Exception\InvalidArgumentException |
106
|
|
|
*/ |
107
|
1 |
|
public function connect() |
108
|
|
|
{ |
109
|
1 |
|
if (!$this->resource->isConnected()) { |
110
|
1 |
|
$this->resource->reconnect(); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Close any open connections and initiate a new one with the AMQP broker. |
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
1 |
|
public function reconnect() |
122
|
|
|
{ |
123
|
1 |
|
$this->resource->reconnect(); |
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Closes the connection with the AMQP broker. |
130
|
|
|
* |
131
|
|
|
* @return $this |
132
|
|
|
* @throws Exception\RuntimeException |
133
|
|
|
*/ |
134
|
1 |
|
public function disconnect() |
135
|
|
|
{ |
136
|
1 |
|
$this->resource->close(); |
137
|
|
|
|
138
|
1 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Check whether the connection to the AMQP broker is still valid. |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
* @throws Exception\RuntimeException |
146
|
|
|
*/ |
147
|
1 |
|
public function isConnected() |
148
|
|
|
{ |
149
|
1 |
|
return $this->resource->isConnected(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|