Skip to content

Commit

Permalink
Use own serialization and use visitor for remove dependencies on SpyM…
Browse files Browse the repository at this point in the history
…iddleware
  • Loading branch information
AaronBernabeu committed Apr 17, 2020
1 parent ebe5d85 commit cd05f81
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
"require": {
"ext-json": "*",
"php": "^7.2",
"pccomponentes/amqp": "^1.0",
"symfony/yaml": "^4.0",
"symfony/messenger": "^4.0",
"justinrainbow/json-schema": "^5.2",
"behat/behat": "^3.5",
"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",
Expand Down
42 changes: 33 additions & 9 deletions src/Messaging/SpyMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
}
}
21 changes: 21 additions & 0 deletions src/Serialization/SchemaValidatorAggregateMessageSerializable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace Pccomponentes\OpenApiMessagingContext\Serialization;

use Pccomponentes\Ddd\Util\Message\AggregateMessage;
use Pccomponentes\Ddd\Util\Message\Serialization\AggregateMessageSerializable;

final class SchemaValidatorAggregateMessageSerializable implements AggregateMessageSerializable
{
public function serialize(AggregateMessage $message)
{
return \json_encode(
[
'message_id' => $message->messageId(),
'type' => $message::messageName(),
'occurred_on' => $message->occurredOn()->getTimestamp(),
'attributes' => array_merge(['aggregate_id' => $message->aggregateId()->value()], $message->messagePayload()),
]
);
}
}
20 changes: 20 additions & 0 deletions src/Serialization/SchemaValidatorSimpleMessageSerializable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace Pccomponentes\OpenApiMessagingContext\Serialization;

use Pccomponentes\Ddd\Util\Message\Serialization\SimpleMessageSerializable;
use Pccomponentes\Ddd\Util\Message\SimpleMessage;

final class SchemaValidatorSimpleMessageSerializable implements SimpleMessageSerializable
{
public function serialize(SimpleMessage $message)
{
return \json_encode(
[
'message_id' => $message->messageId(),
'type' => $message::messageName(),
'attributes' => $message->messagePayload(),
]
);
}
}

0 comments on commit cd05f81

Please sign in to comment.