|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AMQPAL\Adapter\AMQP; |
|
4
|
|
|
|
|
5
|
|
|
use AMQPAL\Adapter\AMQP\Options\ConnectionOptions; |
|
6
|
|
|
use AMQPAL\Adapter\AdapterInterface; |
|
7
|
|
|
use AMQPAL\Adapter\Exception; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AMQP |
|
11
|
|
|
* |
|
12
|
|
|
* @package AMQPAL\Adapter\AMQP |
|
13
|
|
|
*/ |
|
14
|
|
|
class AMQP implements AdapterInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Connection |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $connection; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Channel |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $channelPrototype; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* AMQP constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param Connection|ConnectionOptions|array|\Traversable|\AMQPConnection $connection |
|
29
|
|
|
* @param Channel $channelPrototype |
|
30
|
|
|
* @throws Exception\InvalidArgumentException |
|
31
|
|
|
* @throws Exception\BadMethodCallException |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function __construct($connection, Channel $channelPrototype = null) |
|
34
|
|
|
{ |
|
35
|
1 |
|
if (!$connection instanceof Connection) { |
|
36
|
1 |
|
$connection = new Connection($connection); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
1 |
|
$this->registerConnection($connection); |
|
39
|
1 |
|
$this->registerChannel($channelPrototype ?: new Channel()); |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Connection $connection |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function registerConnection(Connection $connection) |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->connection = $connection; |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param Channel $channel |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function registerChannel(Channel $channel) |
|
54
|
|
|
{ |
|
55
|
1 |
|
$this->channelPrototype = $channel; |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return Connection |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function getConnection() |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->connection; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param \AMQPChannel $resource |
|
68
|
|
|
* @return Channel |
|
69
|
|
|
* @throws \AMQPConnectionException |
|
70
|
|
|
*/ |
|
71
|
|
|
public function createChannel($resource = null) |
|
72
|
|
|
{ |
|
73
|
|
|
$channel = clone $this->channelPrototype; |
|
74
|
|
|
|
|
75
|
|
|
$channel->setConnection($this->getConnection()); |
|
76
|
|
|
|
|
77
|
|
|
if ($resource instanceof \AMQPChannel) { |
|
78
|
|
|
$channel->setResource($resource); |
|
79
|
|
|
} else { |
|
80
|
|
|
if (!$this->getConnection()->isConnected()) { |
|
81
|
|
|
$this->getConnection()->connect(); |
|
82
|
|
|
} |
|
83
|
|
|
$channel->setResource($this->createChannelResource()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $channel; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return \AMQPChannel |
|
91
|
|
|
* @throws \AMQPConnectionException |
|
92
|
|
|
* @codeCoverageIgnore |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function createChannelResource() |
|
95
|
|
|
{ |
|
96
|
|
|
return new \AMQPChannel($this->connection->getResource()); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.