From cd05f8124d022eee591494ba9ad0f80136cf5aed Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 17 Apr 2020 13:31:34 +0200 Subject: [PATCH] Use own serialization and use visitor for remove dependencies on SpyMiddleware --- composer.json | 4 +- src/Messaging/SpyMiddleware.php | 42 +++++++++++++++---- ...aValidatorAggregateMessageSerializable.php | 21 ++++++++++ ...hemaValidatorSimpleMessageSerializable.php | 20 +++++++++ 4 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 src/Serialization/SchemaValidatorAggregateMessageSerializable.php create mode 100644 src/Serialization/SchemaValidatorSimpleMessageSerializable.php diff --git a/composer.json b/composer.json index df6363d..f426c2c 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,6 @@ "require": { "ext-json": "*", "php": "^7.2", - "pccomponentes/amqp": "^1.0", "symfony/yaml": "^4.0", "symfony/messenger": "^4.0", "justinrainbow/json-schema": "^5.2", @@ -30,7 +29,8 @@ "behat/mink": "@dev", "behat/mink-browserkit-driver": "^1.3", "behat/symfony2-extension": "^2.1", - "behat/mink-extension": "^2.3" + "behat/mink-extension": "^2.3", + "pccomponentes/ddd": "^1.2" }, "require-dev": { "pccomponentes/ganchudo": "^1.0", diff --git a/src/Messaging/SpyMiddleware.php b/src/Messaging/SpyMiddleware.php index 3e09af0..ab40dd0 100644 --- a/src/Messaging/SpyMiddleware.php +++ b/src/Messaging/SpyMiddleware.php @@ -2,30 +2,42 @@ namespace Pccomponentes\OpenApiMessagingContext\Messaging; -use Pccomponentes\Amqp\Messenger\MessageSerializer; +use Pccomponentes\Ddd\Util\Message\AggregateMessage; +use Pccomponentes\Ddd\Util\Message\Message; +use Pccomponentes\Ddd\Util\Message\MessageVisitor; +use Pccomponentes\Ddd\Util\Message\SimpleMessage; +use Pccomponentes\OpenApiMessagingContext\Serialization\SchemaValidatorAggregateMessageSerializable; +use Pccomponentes\OpenApiMessagingContext\Serialization\SchemaValidatorSimpleMessageSerializable; use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Middleware\MiddlewareInterface; use Symfony\Component\Messenger\Middleware\StackInterface; -final class SpyMiddleware implements MiddlewareInterface +final class SpyMiddleware implements MiddlewareInterface, MessageVisitor { private static $messages; - private $serializer; + private $simpleMessageSerializable; + private $aggregateMessageSerializable; - public function __construct(MessageSerializer $serializer) + public function __construct() { - $this->serializer = $serializer; + $this->simpleMessageSerializable = new SchemaValidatorSimpleMessageSerializable(); + $this->aggregateMessageSerializable = new SchemaValidatorAggregateMessageSerializable(); } public function handle(Envelope $envelope, StackInterface $stack): Envelope { - $serialized = $this->serializer->serialize($envelope->getMessage()); - $serialized = \json_encode(\json_decode($serialized)->data); - self::$messages[$this->serializer->routingKey($envelope->getMessage())] = $serialized; + /** @var Message $message */ + $message = $envelope->getMessage(); + $message->accept($this); return $stack->next()->handle($envelope, $stack); } + private function save($key, $data): void + { + self::$messages[$key] = $data; + } + public function getMessage(string $name) { if ($this->hasMessage($name)) { @@ -40,8 +52,20 @@ public function hasMessage(string $name): bool return \array_key_exists($name, self::$messages); } - public function reset() + public function reset(): void { self::$messages = []; } + + public function visitSimpleMessage(SimpleMessage $simpleMessage): void + { + $data = $this->simpleMessageSerializable->serialize($simpleMessage); + $this->save($simpleMessage::messageName(), $data); + } + + public function visitAggregateMessage(AggregateMessage $aggregateMessage): void + { + $data = $this->aggregateMessageSerializable->serialize($aggregateMessage); + $this->save($aggregateMessage::messageName(), $data); + } } diff --git a/src/Serialization/SchemaValidatorAggregateMessageSerializable.php b/src/Serialization/SchemaValidatorAggregateMessageSerializable.php new file mode 100644 index 0000000..eab548a --- /dev/null +++ b/src/Serialization/SchemaValidatorAggregateMessageSerializable.php @@ -0,0 +1,21 @@ + $message->messageId(), + 'type' => $message::messageName(), + 'occurred_on' => $message->occurredOn()->getTimestamp(), + 'attributes' => array_merge(['aggregate_id' => $message->aggregateId()->value()], $message->messagePayload()), + ] + ); + } +} diff --git a/src/Serialization/SchemaValidatorSimpleMessageSerializable.php b/src/Serialization/SchemaValidatorSimpleMessageSerializable.php new file mode 100644 index 0000000..b194da9 --- /dev/null +++ b/src/Serialization/SchemaValidatorSimpleMessageSerializable.php @@ -0,0 +1,20 @@ + $message->messageId(), + 'type' => $message::messageName(), + 'attributes' => $message->messagePayload(), + ] + ); + } +}