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 React\Promise\PromiseInterface; |
12
|
|
|
use Rx\Subject\Subject; |
13
|
|
|
use Throwable; |
14
|
|
|
|
15
|
|
|
final class ObservableBunny |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var LoopInterface |
19
|
|
|
*/ |
20
|
|
|
private $loop; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Client |
24
|
|
|
*/ |
25
|
|
|
private $bunny; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param LoopInterface $loop |
29
|
|
|
* @param Client $bunny |
30
|
|
|
*/ |
31
|
4 |
|
public function __construct(LoopInterface $loop, Client $bunny) |
32
|
|
|
{ |
33
|
4 |
|
$this->loop = $loop; |
34
|
4 |
|
$this->bunny = $bunny; |
35
|
4 |
|
} |
36
|
|
|
|
37
|
4 |
|
public function consume( |
38
|
|
|
string $queue = '', |
39
|
|
|
array $qos = [], |
40
|
|
|
string $consumerTag = '', |
41
|
|
|
bool $noLocal = false, |
42
|
|
|
bool $noAck = false, |
43
|
|
|
bool $exclusive = false, |
44
|
|
|
bool $nowait = false, |
45
|
|
|
array $arguments = [] |
46
|
|
|
): Subject { |
47
|
4 |
|
$subject = new Subject(); |
48
|
4 |
|
$consumeArgs = [$queue, $consumerTag, $noLocal, $noAck, $exclusive, $nowait, $arguments]; |
49
|
|
|
|
50
|
4 |
|
$channel = $this->bunny->channel(); |
51
|
3 |
|
$channel->then(function (Channel $channel) use ($qos) { |
|
|
|
|
52
|
3 |
|
if (count($qos) === 0) { |
53
|
2 |
|
return $channel; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return $channel->qos(...$qos)->then(function () use ($channel) { |
|
|
|
|
57
|
1 |
|
return $channel; |
58
|
1 |
|
}); |
59
|
|
|
})->then(function (Channel $channel) use ($subject, $consumeArgs) { |
60
|
|
|
/** @var string $consumerTag */ |
61
|
3 |
|
$consumerTag = null; |
62
|
3 |
|
$timer = $this->loop->addPeriodicTimer(1, function () use ($channel, $subject, &$timer, &$consumerTag) { |
63
|
1 |
|
if (!$subject->isDisposed()) { |
64
|
1 |
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
$this->cancelSubscription( |
|
|
|
|
68
|
1 |
|
$timer, |
69
|
1 |
|
$channel, |
70
|
1 |
|
$consumerTag |
71
|
1 |
|
)->done([$subject, 'onComplete'], $this->onError($subject, $timer)); |
72
|
3 |
|
}); |
73
|
3 |
|
$channel->consume( |
|
|
|
|
74
|
3 |
|
function (BunnyMessage $message, Channel $channel) use ($subject, &$timer, &$consumerTag) { |
75
|
2 |
|
if ($subject->isDisposed()) { |
76
|
1 |
|
$channel->nack($message); |
77
|
1 |
|
$this->cancelSubscription( |
|
|
|
|
78
|
1 |
|
$timer, |
79
|
1 |
|
$channel, |
80
|
1 |
|
$consumerTag |
81
|
1 |
|
)->done([$subject, 'onComplete'], $this->onError($subject, $timer)); |
82
|
|
|
|
83
|
1 |
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
$subject->onNext(new Message($message, $channel)); |
87
|
3 |
|
}, |
88
|
3 |
|
...$consumeArgs |
|
|
|
|
89
|
3 |
|
)->then(function (MethodBasicConsumeOkFrame $response) use (&$consumerTag) { |
90
|
2 |
|
$consumerTag = $response->consumerTag; |
91
|
3 |
|
})->done(null, $this->onError($subject, $timer)); |
92
|
4 |
|
})->done(null, [$subject, 'onError']); |
93
|
|
|
|
94
|
4 |
|
return $subject; |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
private function cancelSubscription(TimerInterface $timer, Channel $channel, string $consumerTag): PromiseInterface |
98
|
|
|
{ |
99
|
2 |
|
$this->loop->cancelTimer($timer); |
100
|
|
|
|
101
|
2 |
|
return $channel->cancel($consumerTag)->then(function () use ($channel) { |
|
|
|
|
102
|
2 |
|
return $channel->close(); |
103
|
2 |
|
}); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function onError(Subject $subject, TimerInterface $timer): callable |
107
|
|
|
{ |
108
|
3 |
|
return function (Throwable $et) use ($subject, $timer) { |
109
|
1 |
|
if ($this->loop->isTimerActive($timer)) { |
110
|
1 |
|
$this->loop->cancelTimer($timer); |
111
|
|
|
} |
112
|
1 |
|
$subject->onError($et); |
113
|
3 |
|
}; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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: