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