1 | <?php |
||
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() |
|
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) |
|
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) |
|
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() |
|
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() |
|
127 | |||
128 | /** |
||
129 | * Closes the connection with the AMQP broker. |
||
130 | * |
||
131 | * @return $this |
||
132 | * @throws Exception\RuntimeException |
||
133 | */ |
||
134 | 1 | public function disconnect() |
|
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() |
|
151 | } |
||
152 |