1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AMQPAL\Adapter\PhpAmqpLib; |
4
|
|
|
|
5
|
|
|
use PhpAmqpLib\Channel\AMQPChannel as LibChannel; |
6
|
|
|
use PhpAmqpLib\Connection\AbstractConnection as LibConnection; |
7
|
|
|
use AMQPAL\Adapter\AdapterInterface; |
8
|
|
|
use AMQPAL\Adapter\Exception; |
9
|
|
|
use AMQPAL\Adapter\PhpAmqpLib\Options\ConnectionOptions; |
10
|
|
|
|
11
|
|
|
class PhpAmqpLib implements AdapterInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Connection |
16
|
|
|
*/ |
17
|
|
|
protected $connection; |
18
|
|
|
/** |
19
|
|
|
* @var Channel |
20
|
|
|
*/ |
21
|
|
|
protected $channelPrototype; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* PhpAmqpLib constructor. |
25
|
|
|
* |
26
|
|
|
* @param Connection|ConnectionOptions|array|\Traversable|LibConnection $connection |
27
|
|
|
* @param Channel $channelPrototype |
28
|
|
|
* @throws Exception\BadMethodCallException |
29
|
|
|
* @throws Exception\InvalidArgumentException |
30
|
|
|
* @throws Exception\RuntimeException |
31
|
|
|
*/ |
32
|
4 |
|
public function __construct($connection, Channel $channelPrototype = null) |
33
|
|
|
{ |
34
|
4 |
|
if (!$connection instanceof Connection) { |
35
|
1 |
|
$connection = new Connection($connection); |
|
|
|
|
36
|
|
|
} |
37
|
4 |
|
$this->registerConnection($connection); |
38
|
4 |
|
$this->registerChannel($channelPrototype ?: new Channel()); |
39
|
4 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Connection $connection |
43
|
|
|
*/ |
44
|
4 |
|
public function registerConnection(Connection $connection) |
45
|
|
|
{ |
46
|
4 |
|
$this->connection = $connection; |
47
|
4 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Channel $channel |
51
|
|
|
*/ |
52
|
4 |
|
public function registerChannel(Channel $channel) |
53
|
|
|
{ |
54
|
4 |
|
$this->channelPrototype = $channel; |
55
|
4 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param LibChannel $resource |
59
|
|
|
* @return Channel |
60
|
|
|
* @throws Exception\RuntimeException |
61
|
|
|
* @throws Exception\InvalidArgumentException |
62
|
|
|
*/ |
63
|
3 |
|
public function createChannel($resource = null) |
64
|
|
|
{ |
65
|
3 |
|
$channel = clone $this->channelPrototype; |
66
|
|
|
|
67
|
3 |
|
$channel->setConnection($this->getConnection()); |
68
|
|
|
|
69
|
3 |
|
if ($resource instanceof LibChannel) { |
70
|
1 |
|
$channel->setResource($resource); |
71
|
|
|
} else { |
72
|
2 |
|
if (!$this->getConnection()->isConnected()) { |
73
|
1 |
|
$this->getConnection()->connect(); |
74
|
|
|
} |
75
|
2 |
|
$channel->setResource($this->getConnection()->getResource()->channel()); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
return $channel; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return Connection |
83
|
|
|
*/ |
84
|
4 |
|
public function getConnection() |
85
|
|
|
{ |
86
|
4 |
|
return $this->connection; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.