From 3b27e06437b22dc9236131a6999333bf7b83e2ce Mon Sep 17 00:00:00 2001 From: David Badura Date: Tue, 6 Feb 2024 18:54:34 +0100 Subject: [PATCH] improve message serializer --- src/EventBus/Serializer/DeserializeFailed.php | 30 +++++++++++++++++++ .../Serializer/PhpNativeMessageSerializer.php | 17 +++++++++-- src/Outbox/DoctrineOutboxStore.php | 19 ++++++++++-- src/Outbox/OutboxHeaderIssue.php | 23 ++++++++++++++ .../PhpNativeMessageSerializerTest.php | 19 ++++++++++++ 5 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 src/EventBus/Serializer/DeserializeFailed.php create mode 100644 src/Outbox/OutboxHeaderIssue.php diff --git a/src/EventBus/Serializer/DeserializeFailed.php b/src/EventBus/Serializer/DeserializeFailed.php new file mode 100644 index 000000000..b34f0a917 --- /dev/null +++ b/src/EventBus/Serializer/DeserializeFailed.php @@ -0,0 +1,30 @@ + true], ); + + if (!$message instanceof Message) { + throw DeserializeFailed::invalidMessage($message); + } + + return $message; } } diff --git a/src/Outbox/DoctrineOutboxStore.php b/src/Outbox/DoctrineOutboxStore.php index 593ed549f..db0fbc0ea 100644 --- a/src/Outbox/DoctrineOutboxStore.php +++ b/src/Outbox/DoctrineOutboxStore.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Types\Types; +use Patchlevel\EventSourcing\EventBus\HeaderNotFound; use Patchlevel\EventSourcing\EventBus\Message; use Patchlevel\EventSourcing\EventBus\Serializer\MessageSerializer; use Patchlevel\EventSourcing\Schema\SchemaConfigurator; @@ -71,8 +72,7 @@ public function markOutboxMessageConsumed(Message ...$messages): void $this->connection->transactional( function (Connection $connection) use ($messages): void { foreach ($messages as $message) { - $id = $message->customHeader(self::HEADER_OUTBOX_IDENTIFIER); - + $id = $this->extractId($message); $connection->delete($this->outboxTable, ['id' => $id]); } }, @@ -108,4 +108,19 @@ public function configureSchema(Schema $schema, Connection $connection): void $table->setPrimaryKey(['id']); } + + private function extractId(Message $message): int + { + try { + $value = $message->customHeader(self::HEADER_OUTBOX_IDENTIFIER); + } catch (HeaderNotFound) { + throw OutboxHeaderIssue::missingHeader(self::HEADER_OUTBOX_IDENTIFIER); + } + + if (!is_int($value)) { + throw OutboxHeaderIssue::invalidHeaderType($value); + } + + return $value; + } } diff --git a/src/Outbox/OutboxHeaderIssue.php b/src/Outbox/OutboxHeaderIssue.php new file mode 100644 index 000000000..817e1d8c2 --- /dev/null +++ b/src/Outbox/OutboxHeaderIssue.php @@ -0,0 +1,23 @@ +headers()); } + public function testDeserializeEncodeFailed(): void + { + $this->expectException(DeserializeFailed::class); + + $nativeSerializer = new PhpNativeMessageSerializer(); + + $nativeSerializer->deserialize('FOO'); + } + + public function testDeserializeNotAMessage(): void + { + $this->expectException(DeserializeFailed::class); + + $nativeSerializer = new PhpNativeMessageSerializer(); + + $nativeSerializer->deserialize('Tzo4OiJzdGRDbGFzcyI6MDp7fQ=='); + } + public function testEquals(): void { $event = new ProfileVisited(