diff --git a/src/ExternalEvents.php b/src/ExternalEvents.php index 06d704a..bf86d68 100644 --- a/src/ExternalEvents.php +++ b/src/ExternalEvents.php @@ -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( '\\', @@ -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); @@ -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" @@ -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); } } } diff --git a/tests/ExternalEventsTest.php b/tests/ExternalEventsTest.php index 61682b0..084b27b 100644 --- a/tests/ExternalEventsTest.php +++ b/tests/ExternalEventsTest.php @@ -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 */ @@ -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 */ @@ -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']); } }