Skip to content

Commit

Permalink
Merge pull request #34 from PcComponentes/feature/allow_not_uuid_aggr…
Browse files Browse the repository at this point in the history
…egate_id

feat: allow not uuid aggregate id
  • Loading branch information
calmohallag authored Jul 14, 2022
2 parents dab09a7 + 2f694be commit 6c66124
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
32 changes: 28 additions & 4 deletions src/Util/Message/AggregateMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
namespace PcComponentes\Ddd\Util\Message;

use PcComponentes\Ddd\Domain\Model\ValueObject\DateTimeValueObject;
use PcComponentes\Ddd\Domain\Model\ValueObject\StringValueObject;
use PcComponentes\Ddd\Domain\Model\ValueObject\Uuid;
use PcComponentes\Ddd\Util\Message\ValueObject\AggregateId;

abstract class AggregateMessage extends Message
{
private Uuid $aggregateId;
private AggregateId $aggregateId;
private DateTimeValueObject $occurredOn;
private int $aggregateVersion;

final protected function __construct(
Uuid $messageId,
Uuid $aggregateId,
AggregateId $aggregateId,
int $aggregateVersion,
DateTimeValueObject $occurredOn,
array $payload
Expand All @@ -28,18 +30,40 @@ final protected function __construct(

final public static function fromPayload(
Uuid $messageId,
Uuid $aggregateId,
StringValueObject $aggregateId,
DateTimeValueObject $occurredOn,
array $payload,
int $aggregateVersion = 0
): self {
if (\is_a($aggregateId, Uuid::class) || \is_subclass_of($aggregateId, Uuid::class)) {
$aggregateId = AggregateId::from($aggregateId->value());

if(function_exists('trigger_deprecation')){
@trigger_deprecation(
'pccomponentes/ddd',
'3.0',
\sprintf(
"AggregateId value should be an instance of %s, %s given",
AggregateId::class,
\get_class($aggregateId),
),
);
}
} elseif (false === \is_a($aggregateId, AggregateId::class)) {
throw new \InvalidArgumentException(\sprintf(
"AggregateId value should be an instance of %s, %s given",
AggregateId::class,
\get_class($aggregateId),
));
}

$message = new static($messageId, $aggregateId, $aggregateVersion, $occurredOn, $payload);
$message->assertPayload();

return $message;
}

final public function aggregateId(): Uuid
final public function aggregateId(): AggregateId
{
return $this->aggregateId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PcComponentes\Ddd\Util\Message\Serialization\AggregateMessageUnserializable;
use PcComponentes\Ddd\Util\Message\Serialization\Exception\MessageClassNotFoundException;
use PcComponentes\Ddd\Util\Message\Serialization\MessageMappingRegistry;
use PcComponentes\Ddd\Util\Message\ValueObject\AggregateId;

final class AggregateMessageStreamDeserializer implements AggregateMessageUnserializable
{
Expand All @@ -33,7 +34,7 @@ public function unserialize($message): AggregateMessage

return $eventClass::fromPayload(
Uuid::from($message->messageId()),
Uuid::from($message->aggregateId()),
AggregateId::from($message->aggregateId()),
DateTimeValueObject::fromTimestamp($message->occurredOn()),
\json_decode($message->payload(), true, 512, \JSON_THROW_ON_ERROR),
$message->aggregateVersion(),
Expand Down
10 changes: 10 additions & 0 deletions src/Util/Message/ValueObject/AggregateId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace PcComponentes\Ddd\Util\Message\ValueObject;

use PcComponentes\Ddd\Domain\Model\ValueObject\StringValueObject;

final class AggregateId extends StringValueObject
{
}

0 comments on commit 6c66124

Please sign in to comment.