Skip to content

Commit

Permalink
Added methods UuidValueTrait::fromAggregateId() and `UuidValueTrait…
Browse files Browse the repository at this point in the history
…::toAggregateId()`
  • Loading branch information
tg666 committed Apr 19, 2023
1 parent e1908b3 commit e0e4f38
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 13 deletions.
12 changes: 11 additions & 1 deletion src/ArchitectureBundle/Domain/ValueObject/UuidValueTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function new(): static
return new static(Uuid::uuid4());
}

public static function fromUuid(UuidInterface $uuid): self
public static function fromUuid(UuidInterface $uuid): static
{
return new static($uuid);
}
Expand All @@ -41,6 +41,11 @@ public static function fromNative(mixed $native): static
}
}

public static function fromAggregateId(AggregateId $aggregateId): static
{
return self::fromUuid($aggregateId->toUuid());
}

public static function isValid(mixed $native): bool
{
return is_string($native) && Uuid::isValid($native);
Expand All @@ -56,6 +61,11 @@ public function toNative(): string
return $this->value->toString();
}

public function toAggregateId(): AggregateId
{
return AggregateId::fromUuid($this->toUuid());
}

public function equals(ValueObjectInterface $object): bool
{
return $object instanceof static && $object->toUuid()->equals($this->toUuid());
Expand Down
4 changes: 2 additions & 2 deletions src/ForgotPasswordBundle/Domain/PasswordRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function cancel(CancelPasswordRequestCommand $command): void

public function getAggregateId(): AggregateId
{
return AggregateId::fromUuid($this->id->toUuid());
return $this->id->toAggregateId();
}

public function isExpired(): bool
Expand All @@ -140,7 +140,7 @@ public function isExpired(): bool
*/
protected function whenPasswordChangeRequested(PasswordChangeRequested $event): void
{
$this->id = PasswordRequestId::fromUuid($event->getAggregateId()->toUuid());
$this->id = PasswordRequestId::fromAggregateId($event->getAggregateId());
$this->emailAddress = $event->getEmailAddress();
$this->status = Status::REQUESTED;
$this->requestedAt = $event->getCreatedAt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace SixtyEightPublishers\ForgotPasswordBundle\Infrastructure\Doctrine;

use SixtyEightPublishers\ArchitectureBundle\Domain\ValueObject\AggregateId;
use SixtyEightPublishers\ArchitectureBundle\Infrastructure\Doctrine\Repository\DoctrineAggregateRootRepositoryInterface;
use SixtyEightPublishers\ForgotPasswordBundle\Domain\Exception\PasswordRequestNotFoundException;
use SixtyEightPublishers\ForgotPasswordBundle\Domain\PasswordRequest;
Expand All @@ -24,7 +23,7 @@ public function save(PasswordRequest $passwordRequest): void

public function get(PasswordRequestId $id): PasswordRequest
{
$passwordRequest = $this->aggregateRootRepository->loadAggregateRoot(PasswordRequest::class, AggregateId::fromUuid($id->toUuid()));
$passwordRequest = $this->aggregateRootRepository->loadAggregateRoot(PasswordRequest::class, $id->toAggregateId());

if (!$passwordRequest instanceof PasswordRequest) {
throw PasswordRequestNotFoundException::withId($id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(

public function __invoke(PasswordChangeRequested $event): void
{
$currentId = PasswordRequestId::fromUuid($event->getAggregateId()->toUuid());
$currentId = PasswordRequestId::fromAggregateId($event->getAggregateId());

foreach ($this->queryBus->dispatch(new FindIdsOfRequestedPasswordChangesQuery($event->getEmailAddress()->toNative())) as $passwordRequestId) {
assert($passwordRequestId instanceof PasswordRequestId);
Expand Down
4 changes: 2 additions & 2 deletions src/MailingBundle/Domain/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static function create(

public function getAggregateId(): AggregateId
{
return AggregateId::fromUuid($this->id->toUuid());
return $this->id->toAggregateId();
}

public function changeCode(string $code, ?CodeGuardInterface $codeGuard = null): void
Expand Down Expand Up @@ -94,7 +94,7 @@ public function changeMessageBody(string $messageBody, string $locale): void

protected function whenMailCreated(MailCreated $event): void
{
$this->id = MailId::fromUuid($event->getAggregateId()->toUuid());
$this->id = MailId::fromAggregateId($event->getAggregateId());
$this->code = $event->getCode();
$this->translations = new ArrayCollection();

Expand Down
2 changes: 1 addition & 1 deletion src/UserBundle/Domain/Exception/UserNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static function withId(UserId $id): self
{
return new self(sprintf(
'User with the ID %s not found.',
$id->toUuid(),
$id->toNative(),
));
}
}
4 changes: 2 additions & 2 deletions src/UserBundle/Domain/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static function create(

public function getAggregateId(): AggregateId
{
return AggregateId::fromUuid($this->id->toUuid());
return $this->id->toAggregateId();
}

public function changeUsername(string $username, ?UsernameGuardInterface $usernameGuard = null): void
Expand Down Expand Up @@ -227,7 +227,7 @@ public function addAttributes(array $attributes, ?AttributesGuardInterface $attr

protected function whenUserCreated(UserCreated $event): void
{
$this->id = UserId::fromUuid($event->getAggregateId()->toUuid());
$this->id = UserId::fromAggregateId($event->getAggregateId());
$this->createdAt = $event->getCreatedAt();
$this->username = $event->getUsername();
$this->password = $event->getPassword();
Expand Down
3 changes: 1 addition & 2 deletions src/UserBundle/Infrastructure/Doctrine/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace SixtyEightPublishers\UserBundle\Infrastructure\Doctrine;

use SixtyEightPublishers\ArchitectureBundle\Domain\ValueObject\AggregateId;
use SixtyEightPublishers\ArchitectureBundle\Infrastructure\Doctrine\Repository\DoctrineAggregateRootRepositoryInterface;
use SixtyEightPublishers\UserBundle\Domain\Exception\UserNotFoundException;
use SixtyEightPublishers\UserBundle\Domain\User;
Expand All @@ -24,7 +23,7 @@ public function save(User $user): void

public function get(UserId $id): User
{
$user = $this->aggregateRootRepository->loadAggregateRoot(User::class, AggregateId::fromUuid($id->toUuid()));
$user = $this->aggregateRootRepository->loadAggregateRoot(User::class, $id->toAggregateId());

if (!$user instanceof User) {
throw UserNotFoundException::withId($id);
Expand Down

0 comments on commit e0e4f38

Please sign in to comment.