diff --git a/src/Cmp/Queues/Domain/Event/DomainEvent.php b/src/Cmp/Queues/Domain/Event/DomainEvent.php index a05aff8..7d06c7c 100644 --- a/src/Cmp/Queues/Domain/Event/DomainEvent.php +++ b/src/Cmp/Queues/Domain/Event/DomainEvent.php @@ -42,25 +42,39 @@ class DomainEvent implements Message protected $isDeprecated = false; /** - * @param string $origin - * @param string $name - * @param string $version - * @param int $occurredOn - * @param array $body - * @param string $id - * @param bool $isDeprecated + * @var string|null */ - public function __construct($origin, $name, $version, $occurredOn, array $body = [], $id = null, $isDeprecated = false) - { + protected $correlationId; + + /** + * @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 + */ + public function __construct( + $origin, + $name, + $version, + $occurredOn, + array $body = [], + $id = null, + $isDeprecated = false, + $correlationId = null + ) { $this->setOrigin($origin) - ->setName($name) - ->setVersion($version) - ->setOccurredOn($occurredOn) - ; + ->setName($name) + ->setVersion($version) + ->setOccurredOn($occurredOn); - $this->body = $body; - $this->id = $id; - $this->isDeprecated = $isDeprecated; + $this->body = $body; + $this->id = $id; + $this->isDeprecated = $isDeprecated; + $this->correlationId = $correlationId; } /** @@ -129,6 +143,45 @@ public function isDeprecated() return $this->isDeprecated; } + /** + * @return string|null + */ + public function getCorrelationID() + { + return $this->correlationId; + } + + /** + * @param string $key + * @param mixed $default + * + * @return mixed + */ + public function getBodyValue($key, $default = null) + { + if (!array_key_exists($key, $this->body)) { + return $default; + } + + return $this->body[$key]; + } + + /** + * @param string $key + * + * @return mixed + * + * @throws \RuntimeException + */ + public function getBodyValueOrFail($key) + { + if (!array_key_exists($key, $this->body)) { + throw new \RuntimeException("No value in the body found for Key: $key"); + } + + return $this->body[$key]; + } + /** * @param string $origin * @return DomainEvent $this @@ -196,13 +249,14 @@ protected function setOccurredOn($occurredOn) public function jsonSerialize() { return [ - 'origin' => $this->origin, - 'name' => $this->name, - 'version' => $this->version, - 'occurredOn' => $this->occurredOn, - 'body' => $this->body, - 'id' => $this->id, - 'isDeprecated' => $this->isDeprecated, + 'origin' => $this->origin, + 'name' => $this->name, + 'version' => $this->version, + 'occurredOn' => $this->occurredOn, + 'body' => $this->body, + 'id' => $this->id, + 'isDeprecated' => $this->isDeprecated, + 'correlationId' => $this->correlationId, ]; } } \ No newline at end of file diff --git a/src/Cmp/Queues/Domain/Event/JSONDomainEventFactory.php b/src/Cmp/Queues/Domain/Event/JSONDomainEventFactory.php index 4947706..150bb48 100644 --- a/src/Cmp/Queues/Domain/Event/JSONDomainEventFactory.php +++ b/src/Cmp/Queues/Domain/Event/JSONDomainEventFactory.php @@ -34,7 +34,8 @@ public function create($json) $domainEventArray['occurredOn'], $domainEventArray['body'], isset($domainEventArray['id']) ? $domainEventArray['id'] : null, - isset($domainEventArray['isDeprecated']) ? $domainEventArray['isDeprecated'] : false + isset($domainEventArray['isDeprecated']) ? $domainEventArray['isDeprecated'] : false, + isset($domainEventArray['correlationId']) ? $domainEventArray['correlationId'] : null ); } catch (DomainEventException $e) { throw new InvalidJSONDomainEventException("Failed creating DomainEvent instance", 0, $e); diff --git a/tests/spec/Cmp/Queues/Domain/Event/DomainEventSpec.php b/tests/spec/Cmp/Queues/Domain/Event/DomainEventSpec.php index 427639c..4f2c4bd 100644 --- a/tests/spec/Cmp/Queues/Domain/Event/DomainEventSpec.php +++ b/tests/spec/Cmp/Queues/Domain/Event/DomainEventSpec.php @@ -12,7 +12,7 @@ class DomainEventSpec extends ObjectBehavior function let() { $this->time = microtime(true)-10; - $this->beConstructedWith('origin', 'name', '1.0.0', $this->time, array(1,2,3), 'uuid', true); + $this->beConstructedWith('origin', 'name', '1.0.0', $this->time, array("foo" => "bar", "empty" => null), 'uuid', true, 'correlation'); } function it_is_initializable() @@ -83,7 +83,7 @@ function it_should_get_occurredOn() function it_should_get_body() { - $this->getBody()->shouldBe(array(1,2,3)); + $this->getBody()->shouldBe(array("foo" => "bar", "empty" => null)); } function it_should_get_the_id() @@ -95,4 +95,21 @@ function it_should_get_the_deprecated_flag() { $this->isDeprecated()->shouldBe(true); } + + function it_should_have_the_correlation_id() + { + $this->getCorrelationId()->shouldBe("correlation"); + } + + function it_can_get_body_values() + { + $this->getBodyValue("foo")->shouldBe("bar"); + $this->getBodyValue("nope", "default")->shouldBe("default"); + $this->getBodyValue("empty", "default")->shouldBe(null); + } + + function it_can_get_body_values_or_fail() + { + $this->shouldThrow(\RuntimeException::class)->duringGetBodyValueOrFail("nope"); + } } diff --git a/tests/spec/Cmp/Queues/Domain/Event/JSONDomainEventFactorySpec.php b/tests/spec/Cmp/Queues/Domain/Event/JSONDomainEventFactorySpec.php index e613a97..4818e38 100644 --- a/tests/spec/Cmp/Queues/Domain/Event/JSONDomainEventFactorySpec.php +++ b/tests/spec/Cmp/Queues/Domain/Event/JSONDomainEventFactorySpec.php @@ -36,4 +36,15 @@ function it_throws_exception_when_missing_required_keys() $jsonStr = json_encode($decodedJsonData); $this->shouldThrow(InvalidJSONDomainEventException::class)->duringCreate($jsonStr); } + + function it_can_detect_a_optional_fields() + { + $id = "abc"; + $isDeprecated = true; + $correlationId = "def"; + + $taskPreFactory = new DomainEvent('origin', 'name', '1.0.0', time(), array(1,2,3,4,5), $id, $isDeprecated, $correlationId); + $this->create(json_encode($taskPreFactory))->shouldBeLike($taskPreFactory); + } + }