1 | <?php |
||
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 | 1 | public function __construct($connection, Channel $channelPrototype = null) |
|
33 | { |
||
34 | 1 | if (!$connection instanceof Connection) { |
|
35 | 1 | $connection = new Connection($connection); |
|
|
|||
36 | } |
||
37 | 1 | $this->registerConnection($connection); |
|
38 | 1 | $this->registerChannel($channelPrototype ?: new Channel()); |
|
39 | 1 | } |
|
40 | |||
41 | /** |
||
42 | * @param Connection $connection |
||
43 | */ |
||
44 | 1 | public function registerConnection(Connection $connection) |
|
45 | { |
||
46 | 1 | $this->connection = $connection; |
|
47 | 1 | } |
|
48 | |||
49 | /** |
||
50 | * @param Channel $channel |
||
51 | */ |
||
52 | 1 | public function registerChannel(Channel $channel) |
|
53 | { |
||
54 | 1 | $this->channelPrototype = $channel; |
|
55 | 1 | } |
|
56 | |||
57 | /** |
||
58 | * @param LibChannel $resource |
||
59 | * @return Channel |
||
60 | * @throws Exception\RuntimeException |
||
61 | * @throws Exception\InvalidArgumentException |
||
62 | */ |
||
63 | public function createChannel($resource = null) |
||
64 | { |
||
65 | $channel = clone $this->channelPrototype; |
||
66 | |||
67 | $channel->setConnection($this->getConnection()); |
||
68 | |||
69 | if ($resource instanceof LibChannel) { |
||
70 | $channel->setResource($resource); |
||
71 | } else { |
||
72 | if (!$this->getConnection()->isConnected()) { |
||
73 | $this->getConnection()->connect(); |
||
74 | } |
||
75 | $channel->setResource($this->getConnection()->getResource()->channel()); |
||
76 | } |
||
77 | |||
78 | return $channel; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return Connection |
||
83 | */ |
||
84 | 1 | public function getConnection() |
|
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.