-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #493 from patchlevel/rewrite-outbox-store
Update OutboxStore schema, extract MessageSerializer from WatchServer
- Loading branch information
Showing
23 changed files
with
547 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\EventBus\Serializer; | ||
|
||
use RuntimeException; | ||
|
||
use function get_debug_type; | ||
use function sprintf; | ||
|
||
final class DeserializeFailed extends RuntimeException | ||
{ | ||
public static function decodeFailed(): self | ||
{ | ||
return new self('Error while decoding message'); | ||
} | ||
|
||
public static function invalidData(mixed $value): self | ||
{ | ||
return new self( | ||
sprintf( | ||
'Invalid data: %s', | ||
get_debug_type($value), | ||
), | ||
); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/EventBus/Serializer/EventSerializerMessageSerializer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\EventBus\Serializer; | ||
|
||
use DateTimeImmutable; | ||
use Patchlevel\EventSourcing\EventBus\Message; | ||
use Patchlevel\EventSourcing\Serializer\EventSerializer; | ||
use Patchlevel\EventSourcing\Serializer\SerializedEvent; | ||
|
||
use function base64_decode; | ||
use function base64_encode; | ||
use function is_array; | ||
use function is_string; | ||
use function serialize; | ||
use function unserialize; | ||
|
||
final class EventSerializerMessageSerializer implements MessageSerializer | ||
{ | ||
public function __construct( | ||
private readonly EventSerializer $eventSerializer, | ||
) { | ||
} | ||
|
||
public function serialize(Message $message): string | ||
{ | ||
$serializedEvent = $this->eventSerializer->serialize($message->event()); | ||
|
||
return base64_encode( | ||
serialize( | ||
[ | ||
'serializedEvent' => $serializedEvent, | ||
'headers' => $message->headers(), | ||
], | ||
), | ||
); | ||
} | ||
|
||
public function deserialize(string $content): Message | ||
{ | ||
$decodedString = base64_decode($content, true); | ||
|
||
if (!is_string($decodedString)) { | ||
throw DeserializeFailed::decodeFailed(); | ||
} | ||
|
||
$data = unserialize( | ||
$decodedString, | ||
[ | ||
'allowed_classes' => [ | ||
SerializedEvent::class, | ||
DateTimeImmutable::class, | ||
], | ||
], | ||
); | ||
|
||
if ( | ||
!is_array($data) | ||
|| !isset($data['serializedEvent'], $data['headers']) | ||
|| !$data['serializedEvent'] instanceof SerializedEvent | ||
|| !is_array($data['headers']) | ||
) { | ||
throw DeserializeFailed::invalidData($data); | ||
} | ||
|
||
$event = $this->eventSerializer->deserialize($data['serializedEvent']); | ||
|
||
return Message::createWithHeaders($event, $data['headers']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\EventBus\Serializer; | ||
|
||
use Patchlevel\EventSourcing\EventBus\Message; | ||
|
||
use function base64_decode; | ||
use function base64_encode; | ||
use function is_string; | ||
use function serialize; | ||
use function unserialize; | ||
|
||
final class PhpNativeMessageSerializer implements MessageSerializer | ||
{ | ||
public function serialize(Message $message): string | ||
{ | ||
return base64_encode(serialize($message)); | ||
} | ||
|
||
public function deserialize(string $content): Message | ||
{ | ||
$decodedString = base64_decode($content, true); | ||
|
||
if (!is_string($decodedString)) { | ||
throw DeserializeFailed::decodeFailed(); | ||
} | ||
|
||
$message = unserialize( | ||
$decodedString, | ||
['allowed_classes' => true], | ||
); | ||
|
||
if (!$message instanceof Message) { | ||
throw DeserializeFailed::invalidData($message); | ||
} | ||
|
||
return $message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.