Skip to content

Commit

Permalink
feat: replaces the generic BasicEntityId with the more specific UuidE…
Browse files Browse the repository at this point in the history
…ntityId
  • Loading branch information
matiux committed Jul 19, 2022
1 parent bade0ea commit 4d64994
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 180 deletions.
65 changes: 13 additions & 52 deletions src/Matiux/DDDStarterPack/Aggregate/Domain/BasicEntityId.php
Expand Up @@ -4,69 +4,30 @@

namespace DDDStarterPack\Aggregate\Domain;

use Exception;
use InvalidArgumentException;
use Ramsey\Uuid\Uuid;

class BasicEntityId implements EntityId
/**
* @template T
* @implements EntityId<T>
*/
abstract class BasicEntityId implements EntityId
{
public const UUID_PATTERN = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/';

final protected function __construct(
private null|int|string $id
) {
}

public function __toString(): string
{
return !$this->id ? '' : (string) $this->id;
}

/**
* @throws Exception
*
* @return static
* @param T $id
*/
public static function create(): static
{
return new static(Uuid::uuid4()->toString());
}

public static function createFrom(int|string $id): static
{
if (!$id) {
throw new InvalidArgumentException(sprintf('Invalid ID: %s', $id));
}

return new static($id);
}

public static function createNUll(): static
{
return new static(null);
}

public static function isValidUuid(string $uuid): bool
{
if (1 === preg_match(self::UUID_PATTERN, $uuid)) {
return true;
}

return false;
final protected function __construct(
protected $id
) {
}

public function equals(EntityId $entityId): bool
{
return $this->id() === $entityId->id();
}

public function id(): null|int|string
/**
* @return T
*/
public function id()
{
return $this->id;
}

public function isNull(): bool
{
return is_null($this->id);
}
}
23 changes: 16 additions & 7 deletions src/Matiux/DDDStarterPack/Aggregate/Domain/EntityId.php
Expand Up @@ -4,17 +4,26 @@

namespace DDDStarterPack\Aggregate\Domain;

/**
* @template I of mixed
*/
interface EntityId
{
public function __toString(): string;

public static function create(): static;

public static function createFrom(int|string $id): static;

public static function createNull(): static;

public function id(): null|int|string;
/**
* @template T of I
*
* @param T $id
*
* @return static
*/
public static function createFrom($id): static;

/**
* @return I
*/
public function id();

public function equals(EntityId $entityId): bool;
}
55 changes: 55 additions & 0 deletions src/Matiux/DDDStarterPack/Aggregate/Domain/UuidEntityId.php
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace DDDStarterPack\Aggregate\Domain;

use Exception;
use InvalidArgumentException;
use Ramsey\Uuid\Uuid;

/**
* @extends BasicEntityId<string>
*/
class UuidEntityId extends BasicEntityId
{
public const UUID_PATTERN = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/';

public function __toString(): string
{
return !$this->id ? '' : $this->id;
}

/**
* @throws Exception
*
* @return static
*/
public static function create(): static
{
return new static(Uuid::uuid4()->toString());
}

/**
* @param string $id
*
* @return static
*/
public static function createFrom($id): static
{
if (!$id || !self::isValidUuid($id)) {
throw new InvalidArgumentException(sprintf('Invalid ID: %s', $id));
}

return new static($id);
}

public static function isValidUuid(string $uuid): bool
{
if (1 === preg_match(self::UUID_PATTERN, $uuid)) {
return true;
}

return false;
}
}
Expand Up @@ -4,16 +4,16 @@

namespace DDDStarterPack\Aggregate\Infrastructure\Doctrine;

use DDDStarterPack\Aggregate\Domain\BasicEntityId;
use DDDStarterPack\Aggregate\Domain\EntityId;
use DDDStarterPack\Aggregate\Domain\UuidEntityId;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\GuidType;

abstract class DoctrineEntityId extends GuidType
abstract class DoctrineUuidEntityId extends GuidType
{
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return $value instanceof EntityId ? $value->id() : $value ?? null;
return $value instanceof UuidEntityId ? $value->id() : $value ?? null;
}

public function convertToPHPValue($value, AbstractPlatform $platform)
Expand All @@ -35,25 +35,24 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
/**
* @param mixed $value
*
* @return null|int|string
* @return null|string
*/
private function prepareValue(mixed $value): null|int|string
private function prepareValue(mixed $value): null|string
{
return match (true) {
is_object($value), is_null($value) => null,
is_int($value) => $value,
default => (string) $value
};
}

/**
* @param int|string $value
* @param string $value
*
* @return bool
*/
private function isValidUuid(int|string $value): bool
private function isValidUuid(string $value): bool
{
return is_string($value) && BasicEntityId::isValidUuid($value);
return UuidEntityId::isValidUuid($value);
}

protected function isCustomValid(): bool
Expand Down
Expand Up @@ -28,10 +28,10 @@ class SQSMessageConsumer extends BasicMessageService implements MessageConsumerC
public const NAME = 'SQS';

/** @var string[] */
private array $attributeNames = ['All']; //'ApproximateReceiveCount'
private array $attributeNames = ['All']; // 'ApproximateReceiveCount'

/** @var string[] */
private array $messageAttributeNames = ['All']; //'Type', 'OccurredAt'
private array $messageAttributeNames = ['All']; // 'Type', 'OccurredAt'

public function __construct(
private AWSMessageFactory $AWSMessageFactory
Expand Down
Expand Up @@ -37,7 +37,7 @@ public function consume(null|string $queue = null): null|Message
* Inviando la conferma il messaggio verrà definitivamente cancellato dalla coda. No conferma = no cancellazione dalla coda e reinvio
* a un nuovo worker.
*/
//$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
// $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
};

$this->channel->basic_qos(null, 1, null);
Expand All @@ -50,7 +50,7 @@ public function consume(null|string $queue = null): null|Message
while (count($this->channel->callbacks)) {
if ($body) {
return $this->messageFactory->build($body, '', $deliveryTag, '', null, null);
//return new ArrayObject(['Body' => $body, 'ReceiptHandle' => $deliveryTag]);
// return new ArrayObject(['Body' => $body, 'ReceiptHandle' => $deliveryTag]);
}

try {
Expand Down
Expand Up @@ -4,10 +4,10 @@

namespace Tests\Support\Model\Doctrine;

use DDDStarterPack\Aggregate\Infrastructure\Doctrine\DoctrineEntityId;
use DDDStarterPack\Aggregate\Infrastructure\Doctrine\DoctrineUuidEntityId;
use Tests\Support\Model\PersonId;

class DoctrinePersonId extends DoctrineEntityId
class DoctrineUuidPersonId extends DoctrineUuidEntityId
{
public function getName()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Support/Model/PersonId.php
Expand Up @@ -4,8 +4,8 @@

namespace Tests\Support\Model;

use DDDStarterPack\Aggregate\Domain\BasicEntityId;
use DDDStarterPack\Aggregate\Domain\UuidEntityId;

class PersonId extends BasicEntityId
class PersonId extends UuidEntityId
{
}
4 changes: 2 additions & 2 deletions tests/Tool/EntityManagerBuilder.php
Expand Up @@ -13,7 +13,7 @@
use Doctrine\Persistence\ObjectRepository;
use InvalidArgumentException;
use Nyholm\Dsn\DsnParser;
use Tests\Support\Model\Doctrine\DoctrinePersonId;
use Tests\Support\Model\Doctrine\DoctrineUuidPersonId;

class EntityManagerBuilder
{
Expand Down Expand Up @@ -68,7 +68,7 @@ public static function create(bool $isDevMode = false): self
$builder = new self($isDevMode);

if (!Type::hasType('PersonId')) {
Type::addType('PersonId', DoctrinePersonId::class);
Type::addType('PersonId', DoctrineUuidPersonId::class);
}

$builder->ems['default'] = EntityManager::create($builder->connectionParams, $builder->config);
Expand Down
89 changes: 0 additions & 89 deletions tests/Unit/DDDStarterPack/Aggregate/Domain/BasicEntityIdTest.php

This file was deleted.

0 comments on commit 4d64994

Please sign in to comment.