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 Rx\Subject\Subject; |
11
|
|
|
|
12
|
|
|
final class ObservableBunny |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var LoopInterface |
16
|
|
|
*/ |
17
|
|
|
private $loop; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Client |
21
|
|
|
*/ |
22
|
|
|
private $bunny; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param LoopInterface $loop |
26
|
|
|
* @param Client $bunny |
27
|
|
|
*/ |
28
|
1 |
|
public function __construct(LoopInterface $loop, Client $bunny) |
29
|
|
|
{ |
30
|
1 |
|
$this->loop = $loop; |
31
|
1 |
|
$this->bunny = $bunny; |
32
|
1 |
|
} |
33
|
|
|
|
34
|
1 |
|
public function consume( |
35
|
|
|
string $queue = '', |
36
|
|
|
string $consumerTag = '', |
37
|
|
|
bool $noLocal = false, |
38
|
|
|
bool $noAck = false, |
39
|
|
|
bool $exclusive = false, |
40
|
|
|
bool $nowait = false, |
41
|
|
|
array $arguments = [] |
42
|
|
|
): Subject { |
43
|
1 |
|
$subject = new Subject(); |
44
|
1 |
|
$consumeArgs = [$queue, $consumerTag, $noLocal, $noAck, $exclusive, $nowait, $arguments]; |
45
|
|
|
|
46
|
1 |
|
$channel = $this->bunny->channel(); |
47
|
1 |
|
$channel->then(function (Channel $channel) use ($subject, $consumeArgs) { |
|
|
|
|
48
|
1 |
|
$consumerTag = null; |
49
|
1 |
|
$timer = $this->loop->addPeriodicTimer(1, function () use ($channel, $subject, &$timer, &$consumerTag) { |
50
|
1 |
|
if (!$subject->isDisposed()) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
$channel->cancel($consumerTag)->done([$subject, 'onComplete'], [$subject, 'onError']); |
55
|
1 |
|
$this->loop->cancelTimer($timer); |
56
|
1 |
|
}); |
57
|
1 |
|
$channel->consume( |
|
|
|
|
58
|
1 |
|
function (BunnyMessage $message, Channel $channel) use ($subject, &$timer, &$consumerTag) { |
59
|
1 |
|
if ($subject->isDisposed()) { |
60
|
|
|
$channel->nack($message); |
61
|
|
|
$channel->cancel($consumerTag)->done([$subject, 'onComplete'], [$subject, 'onError']); |
62
|
|
|
$this->loop->cancelTimer($timer); |
63
|
|
|
|
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
$subject->onNext(new Message($message, $channel)); |
68
|
1 |
|
}, |
69
|
1 |
|
...$consumeArgs |
|
|
|
|
70
|
1 |
|
)->then(function (MethodBasicConsumeOkFrame $response) use (&$consumerTag) { |
71
|
1 |
|
$consumerTag = $response->consumerTag; |
72
|
1 |
|
})->done(null, [$subject, 'onError']); |
73
|
1 |
|
})->done(null, [$subject, 'onError']); |
74
|
|
|
|
75
|
1 |
|
return $subject; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
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: