1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\ObservableBunny; |
4
|
|
|
|
5
|
|
|
use Bunny\Async\Client; |
6
|
|
|
use Bunny\Channel; |
7
|
|
|
use Bunny\Message as BunnyMessage; |
8
|
|
|
use Bunny\Protocol\MethodBasicConsumeOkFrame; |
9
|
|
|
use React\EventLoop\LoopInterface; |
10
|
|
|
use React\EventLoop\Timer\TimerInterface; |
11
|
|
|
use Rx\Subject\Subject; |
12
|
|
|
|
13
|
|
|
final class ObservableBunny |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var LoopInterface |
17
|
|
|
*/ |
18
|
|
|
private $loop; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Client |
22
|
|
|
*/ |
23
|
|
|
private $bunny; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param LoopInterface $loop |
27
|
|
|
* @param Client $bunny |
28
|
1 |
|
*/ |
29
|
|
|
public function __construct(LoopInterface $loop, Client $bunny) |
30
|
1 |
|
{ |
31
|
1 |
|
$this->loop = $loop; |
32
|
1 |
|
$this->bunny = $bunny; |
33
|
|
|
} |
34
|
1 |
|
|
35
|
|
|
public function consume( |
36
|
|
|
string $queue = '', |
37
|
|
|
string $consumerTag = '', |
38
|
|
|
bool $noLocal = false, |
39
|
|
|
bool $noAck = false, |
40
|
|
|
bool $exclusive = false, |
41
|
|
|
bool $nowait = false, |
42
|
|
|
array $arguments = [] |
43
|
1 |
|
): Subject { |
44
|
1 |
|
$subject = new Subject(); |
45
|
|
|
$consumeArgs = [$queue, $consumerTag, $noLocal, $noAck, $exclusive, $nowait, $arguments]; |
46
|
1 |
|
|
47
|
1 |
|
$channel = $this->bunny->channel(); |
48
|
1 |
|
$channel->then(function (Channel $channel) use ($subject, $consumeArgs) { |
|
|
|
|
49
|
1 |
|
/** @var string $consumerTag */ |
50
|
1 |
|
$consumerTag = null; |
51
|
|
|
$timer = $this->loop->addPeriodicTimer(1, function () use ($channel, $subject, &$timer, &$consumerTag) { |
52
|
|
|
if (!$subject->isDisposed()) { |
53
|
|
|
return; |
54
|
1 |
|
} |
55
|
1 |
|
|
56
|
1 |
|
$this->cancelSubscription( |
57
|
1 |
|
$timer, |
58
|
1 |
|
$channel, |
59
|
1 |
|
$consumerTag |
60
|
|
|
)->done([$subject, 'onComplete'], [$subject, 'onError']); |
61
|
|
|
}); |
62
|
|
|
$channel->consume( |
|
|
|
|
63
|
|
|
function (BunnyMessage $message, Channel $channel) use ($subject, &$timer, &$consumerTag) { |
64
|
|
|
if ($subject->isDisposed()) { |
65
|
|
|
$channel->nack($message); |
66
|
|
|
$this->cancelSubscription( |
67
|
1 |
|
$timer, |
68
|
1 |
|
$channel, |
69
|
1 |
|
$consumerTag |
70
|
1 |
|
)->done([$subject, 'onComplete'], [$subject, 'onError']); |
71
|
1 |
|
|
72
|
1 |
|
return; |
73
|
1 |
|
} |
74
|
|
|
|
75
|
1 |
|
$subject->onNext(new Message($message, $channel)); |
76
|
|
|
}, |
77
|
|
|
...$consumeArgs |
|
|
|
|
78
|
|
|
)->then(function (MethodBasicConsumeOkFrame $response) use (&$consumerTag) { |
79
|
|
|
$consumerTag = $response->consumerTag; |
80
|
|
|
})->done(null, [$subject, 'onError']); |
81
|
|
|
})->done(null, [$subject, 'onError']); |
82
|
|
|
|
83
|
|
|
return $subject; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function cancelSubscription(TimerInterface $timer, Channel $channel, string $consumerTag) |
87
|
|
|
{ |
88
|
|
|
$this->loop->cancelTimer($timer); |
89
|
|
|
|
90
|
|
|
return $channel->cancel($consumerTag)->then(function () use ($channel) { |
91
|
|
|
return $channel->close(); |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: