Skip to content

Commit

Permalink
Allow to pass headers
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviapa committed Mar 10, 2022
1 parent 95aa458 commit bd2992c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 43 deletions.
33 changes: 14 additions & 19 deletions src/ExternalEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ExternalEvents
{
private const CAMEL_CASE_LETTERS_DETECTION = '#(?!(?<=^)|(?<=\\\))[A-Z]#';

public static function publish(Message $class): void
public static function publish(Message $class, array $headers = []): void
{
$routingKey = str_replace(
'\\',
Expand All @@ -26,15 +26,13 @@ public static function publish(Message $class): void
)
)
);
$message = [

$message = [
'data' => $class->serializeToJsonString(),
];

if (array_key_exists('HTTP_X_REQUEST_ID', $_SERVER)) {
$message['xRequestId'] = $_SERVER['HTTP_X_REQUEST_ID'];
}
if (array_key_exists('HTTP_X_MSG_PRIORITY', $_SERVER)) {
$message['xMsgPriority'] = $_SERVER['HTTP_X_MSG_PRIORITY'];
if ($headers) {
$message['headers'] = $headers;
}

publish($routingKey, $message);
Expand All @@ -44,19 +42,18 @@ public static function decorateListener(string $listenerClass): \Closure
{
return static function (string $event, array $message) use ($listenerClass) {
try {
$listener = resolve($listenerClass);

if (method_exists($listener, 'setHeaders')) {
$listener->setHeaders($message[0]['headers']);
}

$eventParameter = new ReflectionParameter([$listenerClass, 'handle'], 0);
$className = $eventParameter->getType()->getName();

$payload = ExternalEvents::decode($className, $message[0]['data']);

if (array_key_exists('xRequestId', $message[0])) {
$_SERVER['HTTP_X_REQUEST_ID'] = $message[0]['xRequestId'];
}
if (array_key_exists('xMsgPriority', $message[0])) {
$_SERVER['HTTP_X_MSG_PRIORITY'] = $message[0]['xMsgPriority'];
}

return resolve($listenerClass)->handle($payload);
return $listener->handle($payload);
} catch (ReflectionException $e) {
throw new BadMethodCallException(
"$listenerClass must have a handle method with a single parameter of type object child of \Google\Protobuf\Internal\Message"
Expand All @@ -75,10 +72,8 @@ public static function decode(string $expectedEvent, string $message): mixed
$event->mergeFromJsonString($message);

return $event;
} catch (Exception) {
throw new InvalidMessageException(
"The message is not a valid {$expectedEvent} message"
);
} catch (Exception $e) {
throw new InvalidMessageException("The message is not a valid {$expectedEvent} message", 0, $e);
}
}
}
72 changes: 48 additions & 24 deletions tests/ExternalEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,20 @@
function publish($routingKey, $message)
{
assertSame('softonic.laravel_protobuf_events.fake_proto.fake_message', $routingKey);
assertSame(
[
'data' => '{"content":":content:"}',
'xRequestId' => '7b15d663-8d55-4e2f-82cc-4473576a4a17',
'xMsgPriority' => 5,
],
$message
);

if (array_key_exists('headers', $message)) {
$expectedMessage = [
'data' => '{"content":":content:"}',
'headers' => ['xRequestId' => '7b15d663-8d55-4e2f-82cc-4473576a4a17'],
];
} else {
$expectedMessage = ['data' => '{"content":":content:"}'];
}
assertSame($expectedMessage, $message);
}

class ExternalEventsTest extends TestCase
{
protected function tearDown(): void
{
unset($_SERVER['HTTP_X_REQUEST_ID'], $_SERVER['HTTP_X_MSG_PRIORITY']);

parent::tearDown();
}

/**
* @test
*/
Expand Down Expand Up @@ -62,15 +57,25 @@ public function whenDecodeAnInvalidMessageItShouldThrowAnException(): void
*/
public function whenPublishMessageItShouldPublishIt(): void
{
$_SERVER['HTTP_X_REQUEST_ID'] = '7b15d663-8d55-4e2f-82cc-4473576a4a17';
$_SERVER['HTTP_X_MSG_PRIORITY'] = 5;

$message = new FakeMessage();
$message->setContent(':content:');

ExternalEvents::publish($message);
}

/**
* @test
*/
public function whenPublishMessageWithHeadersItShouldPublishIt(): void
{
$headers = ['xRequestId' => '7b15d663-8d55-4e2f-82cc-4473576a4a17'];

$message = new FakeMessage();
$message->setContent(':content:');

ExternalEvents::publish($message, $headers);
}

/**
* @test
*/
Expand Down Expand Up @@ -106,18 +111,37 @@ public function handle(FakeMessage $message)
$message = new FakeMessage();
$message->setContent(':content:');

ExternalEvents::decorateListener($listener::class)(':event:', [['data' => $message->serializeToJsonString()]]);
}

/**
* @test
*/
public function whenDecoratingAListenerWithHeadersItShouldExecuteIt(): void
{
$listener = new class() {
public function setHeaders(array $headers)
{
assertSame(['xRequestId' => '7b15d663-8d55-4e2f-82cc-4473576a4a17'], $headers);
}

public function handle(FakeMessage $message)
{
assertSame(':content:', $message->getContent());
}
};

$message = new FakeMessage();
$message->setContent(':content:');

ExternalEvents::decorateListener($listener::class)(
':event:',
[
[
'data' => $message->serializeToJsonString(),
'xRequestId' => '7b15d663-8d55-4e2f-82cc-4473576a4a17',
'xMsgPriority' => 5,
'data' => $message->serializeToJsonString(),
'headers' => ['xRequestId' => '7b15d663-8d55-4e2f-82cc-4473576a4a17'],
],
]
);

self::assertSame('7b15d663-8d55-4e2f-82cc-4473576a4a17', $_SERVER['HTTP_X_REQUEST_ID']);
self::assertSame(5, $_SERVER['HTTP_X_MSG_PRIORITY']);
}
}

0 comments on commit bd2992c

Please sign in to comment.