Skip to content

Commit

Permalink
Merge pull request #29 from CMProductions/allow_message_attributes
Browse files Browse the repository at this point in the history
Added message attributes for Domain Events
  • Loading branch information
LunaBulnesCMP authored Mar 2, 2020
2 parents 3e20b81 + 656b9bd commit 89d50e8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 13 deletions.
49 changes: 41 additions & 8 deletions src/Cmp/Queues/Domain/Event/DomainEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,26 @@ class DomainEvent implements Message
protected $correlationId;

/**
* @param string $origin
* @param string $name
* @param string $version
* @param int $occurredOn
* @param array $body
* @param string $id
* @param bool $isDeprecated
* @var array
*/
private $attributes;

/**
* @var string
*/
private $context;

/**
* @param string $origin
* @param string $name
* @param string $version
* @param int $occurredOn
* @param array $body
* @param string $id
* @param bool $isDeprecated
* @param string|null $correlationId
* @param array $attributes
* @param string $context
* @throws DomainEventException
*/
public function __construct(
Expand All @@ -65,7 +77,9 @@ public function __construct(
array $body = [],
$id = null,
$isDeprecated = false,
$correlationId = null
$correlationId = null,
$context = "",
$attributes = []
) {
$this->setOrigin($origin)
->setName($name)
Expand All @@ -76,6 +90,8 @@ public function __construct(
$this->id = $id;
$this->isDeprecated = $isDeprecated;
$this->correlationId = $correlationId;
$this->attributes = $attributes;
$this->context = $context;
}

/**
Expand Down Expand Up @@ -183,6 +199,22 @@ public function getBodyValueOrFail($key)
return $this->body[$key];
}

/**
* @return string
*/
public function getContext()
{
return $this->context;
}

/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}

/**
* @param string $origin
* @return DomainEvent $this
Expand Down Expand Up @@ -252,6 +284,7 @@ public function jsonSerialize()
return [
'origin' => $this->origin,
'name' => $this->name,
'context' => $this->context,
'version' => $this->version,
'occurredOn' => $this->occurredOn,
'body' => $this->body,
Expand Down
7 changes: 4 additions & 3 deletions src/Cmp/Queues/Domain/Event/JSONDomainEventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function create($json)
throw new InvalidJSONDomainEventException("String is not valid JSON");
}

if (!isset($domainEventArray['origin'], $domainEventArray['name'], $domainEventArray['version'], $domainEventArray['occurredOn'], $domainEventArray['body'])) {
if (!isset($domainEventArray['origin'], $domainEventArray['name'], $domainEventArray['version'], $domainEventArray['occurredOn'])) {
throw new InvalidJSONDomainEventException("Cannot reconstruct domain event. Origin, name, version, occurredOn or body fields are missing");
}

Expand All @@ -32,10 +32,11 @@ public function create($json)
$domainEventArray['name'],
$domainEventArray['version'],
$domainEventArray['occurredOn'],
$domainEventArray['body'],
isset($domainEventArray['body']) ? $domainEventArray['body'] : [],
isset($domainEventArray['id']) ? $domainEventArray['id'] : null,
isset($domainEventArray['isDeprecated']) ? $domainEventArray['isDeprecated'] : false,
isset($domainEventArray['correlationId']) ? $domainEventArray['correlationId'] : null
isset($domainEventArray['correlationId']) ? $domainEventArray['correlationId'] : null,
isset($domainEventArray['context']) ? $domainEventArray['context'] : ""
);
} catch (DomainEventException $e) {
throw new InvalidJSONDomainEventException("Failed creating DomainEvent instance", 0, $e);
Expand Down
5 changes: 5 additions & 0 deletions src/Cmp/Queues/Domain/Queue/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public function getBody();
* @return int
*/
public function getDelay();

/**
* @return array
*/
public function getAttributes();
}
9 changes: 9 additions & 0 deletions src/Cmp/Queues/Domain/Task/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Task implements TaskInterface
* @param string $name
* @param array $body
* @param int $delay
* @throws TaskException
*/
public function __construct($name, array $body, $delay=0)
{
Expand Down Expand Up @@ -99,4 +100,12 @@ public function jsonSerialize()
'delay' => $this->delay
];
}

/**
* @return array
*/
public function getAttributes()
{
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ protected function send(Message $message)
{
try {
$this->sns->publish([
'TopicArn' => $this->topicArn,
'Message' => json_encode($message),
'TopicArn' => $this->topicArn,
'Message' => json_encode($message),
'MessageAttributes' => $message->getAttributes()
]);
} catch(\Exception $e) {
$this->logger->error('Error writing messages', ['exception' => $e]);
Expand Down

0 comments on commit 89d50e8

Please sign in to comment.