Skip to content

Commit

Permalink
Added correlation ID and some helpers (#23)
Browse files Browse the repository at this point in the history
* Added correaltion ID and some helpers
  • Loading branch information
Hilari Moragrega authored and Alex Rufo committed Apr 25, 2018
1 parent 09267ae commit 45cd874
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 26 deletions.
100 changes: 77 additions & 23 deletions src/Cmp/Queues/Domain/Event/DomainEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
];
}
}
3 changes: 2 additions & 1 deletion src/Cmp/Queues/Domain/Event/JSONDomainEventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 19 additions & 2 deletions tests/spec/Cmp/Queues/Domain/Event/DomainEventSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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");
}
}
11 changes: 11 additions & 0 deletions tests/spec/Cmp/Queues/Domain/Event/JSONDomainEventFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit 45cd874

Please sign in to comment.