Skip to content

Instantly share code, notes, and snippets.

@codeliner codeliner/Message.php
Last active Dec 10, 2016

Embed
What would you like to do?
Single prooph message class
<?php
declare(strict_types = 1);
namespace Acme\Model;
use Prooph\Common\Messaging\DomainMessage;
use Prooph\Common\Messaging\Message as ProophMessage;
class Message extends DomainMessage
{
/**
* @var array
*/
protected $payload;
public static function command(MessageName $messageName, array $payload = [], array $metadata = []): Message
{
return new static($messageName, DomainMessage::TYPE_COMMAND, $payload, $metadata);
}
public static function event(MessageName $messageName, array $payload = [], array $metadata = []): Message
{
return new static($messageName, DomainMessage::TYPE_EVENT, $payload, $metadata);
}
public static function query(MessageName $messageName, array $payload = [], array $metadata = []): Message
{
return new static($messageName, DomainMessage::TYPE_QUERY, $payload, $metadata);
}
protected function __construct(MessageName $messageName, string $messageType, array $payload, array $metadata)
{
$this->messageName = $messageName->getValue();
$metadata['_message_type'] = $messageType;
$this->metadata = $metadata;
$this->setPayload($payload);
$this->init();
}
/**
* Should be one of Message::TYPE_COMMAND, Message::TYPE_EVENT or Message::TYPE_QUERY
*/
public function messageType(): string
{
return $this->metadata['_message_type'];
}
public function payload(): array
{
return $this->payload;
}
public function withMetadata(array $metadata): ProophMessage
{
$messageData = $this->toArray();
$metadata['_message_type'] = $this->messageType();
$messageData['metadata'] = $metadata;
return static::fromArray($messageData);
}
protected function setPayload(array $payload): void
{
$this->payload = $payload;
}
}
<?php
declare(strict_types = 1);
namespace Acme\Model;
use MabeEnum\Enum;
final class MessageName extends Enum
{
private const COMMAND_NAMESPACE = 'Acme.Command.';
private const EVENT_NAMESPACE = 'Acme.Event.';
private const QUERY_NAMESPACE = 'Acme.Query.';
public const EVENT_TEST = self::EVENT_NAMESPACE . 'Test';
//...
}
@sandrokeil

This comment has been minimized.

Copy link

commented Dec 10, 2016

Looks promising

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.