Repository navigation
Expand file tree
/
Copy pathEncodedConnection.php
More file actions
77 lines (70 loc) · 2.18 KB
/
Copy pathEncodedConnection.php
File metadata and controls
77 lines (70 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Nats;
use Closure;
use Hyperf\Nats\Encoders\Encoder;
/**
* Class EncodedConnection.
*/
class EncodedConnection extends Connection
{
/**
* EncodedConnection constructor.
*
* @param ConnectionOptions $options connection options object
* @param Encoder $encoder encoder to use with the payload
*/
public function __construct(ConnectionOptions $options, private Encoder $encoder)
{
parent::__construct($options);
}
/**
* Publish publishes the data argument to the given subject.
*
* @param string $subject message topic
* @param string $payload message data
* @param string $inbox message inbox
*/
public function publish(string $subject, mixed $payload = null, ?string $inbox = null): void
{
$payload = $this->encoder->encode($payload);
parent::publish($subject, $payload, $inbox);
}
/**
* Subscribes to a specific event given a subject.
*
* @param string $subject message topic
* @param Closure $callback closure to be executed as callback
*/
public function subscribe(string $subject, Closure $callback): string
{
$c = function ($message) use ($callback) {
$message->setBody($this->encoder->decode($message->getBody()));
$callback($message);
};
return parent::subscribe($subject, $c);
}
/**
* Subscribes to an specific event given a subject and a queue.
*
* @param string $subject message topic
* @param string $queue queue name
* @param Closure $callback closure to be executed as callback
*/
public function queueSubscribe(string $subject, string $queue, Closure $callback): string
{
$c = function ($message) use ($callback) {
$message->setBody($this->encoder->decode($message->getBody()));
$callback($message);
};
return parent::queueSubscribe($subject, $queue, $c);
}
}