diff --git a/src/Domain/Event/Subscriber.php b/src/Domain/Event/Subscriber.php index a5aeddc7..03a3278b 100644 --- a/src/Domain/Event/Subscriber.php +++ b/src/Domain/Event/Subscriber.php @@ -34,7 +34,7 @@ class Subscriber implements Listener public function __construct(Event\Listener\Factory $listenerFactory, Event\Subscription\Factory $subscriptionFactory, Event\Subscription\Repository $subscriptionsRepository, UnitOfWork $uow) // TODO: GET RID OF UOW FROM HERE! { - $this->uuid = Domain\Id\UUID::create(); + $this->uuid = Domain\Id\UUID4::create(); $this->listenerFactory = $listenerFactory; $this->subscriptionFactory = $subscriptionFactory; $this->subscriptionsRepository = $subscriptionsRepository; diff --git a/src/Domain/Id/UUID.php b/src/Domain/Id/UUID.php index c28c2859..b5174e5a 100644 --- a/src/Domain/Id/UUID.php +++ b/src/Domain/Id/UUID.php @@ -16,13 +16,9 @@ use Streak\Domain; /** - * UUID v4. - * * @author Alan Gabriel Bem - * - * @see https://gist.github.com/tdomarkas/c5fbc10385ae004cbde6 */ -class UUID implements Domain\Id +abstract class UUID implements Domain\Id { private $value; @@ -46,14 +42,7 @@ final public function __construct(string $value) $this->value = $value; } - public static function create() - { - $uuid = \Ramsey\Uuid\Uuid::uuid4()->toString(); - - return new static($uuid); - } - - public function equals($uuid) : bool + final public function equals($uuid) : bool { if (!$uuid instanceof self) { return false; @@ -66,13 +55,13 @@ public function equals($uuid) : bool return true; } - public function toString() : string + final public function toString() : string { return $this->value; } - public static function fromString(string $uuid) : Domain\Id + final public static function fromString(string $uuid) : Domain\Id { - return new self($uuid); + return new static($uuid); } } diff --git a/src/Domain/Id/UUID3.php b/src/Domain/Id/UUID3.php new file mode 100644 index 00000000..29929f0c --- /dev/null +++ b/src/Domain/Id/UUID3.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Streak\Domain\Id; + +/** + * UUID v3. + * + * @author Alan Gabriel Bem + */ +abstract class UUID3 extends UUID +{ + /** + * Generates deterministic UUID based on MD5 of static namespace (also UUID) and name. + * + * Use it only if backward compatibility with e.g. outside system is required, use v5 otherwise. + * + * @param string $name + * + * @return static + */ + final public static function create(string $name) + { + $uuid = \Ramsey\Uuid\Uuid::uuid3(self::namespace()->toString(), $name)->toString(); + + return new static($uuid); + } + + /** + * Namespace UUID required for UUID v5 generation. + * + * @return UUID + */ + abstract protected static function namespace() : UUID; +} diff --git a/src/Domain/Id/UUID4.php b/src/Domain/Id/UUID4.php new file mode 100644 index 00000000..bdab83a7 --- /dev/null +++ b/src/Domain/Id/UUID4.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Streak\Domain\Id; + +/** + * UUID v4. + * + * @author Alan Gabriel Bem + */ +class UUID4 extends UUID +{ + /** + * Generates (pseudo-)random UUID. + * + * @return UUID4 + */ + final public static function create() + { + $uuid = \Ramsey\Uuid\Uuid::uuid4()->toString(); + + return new static($uuid); + } +} diff --git a/src/Domain/Id/UUID5.php b/src/Domain/Id/UUID5.php new file mode 100644 index 00000000..36ad8647 --- /dev/null +++ b/src/Domain/Id/UUID5.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Streak\Domain\Id; + +/** + * UUID v5. + * + * @author Alan Gabriel Bem + */ +abstract class UUID5 extends UUID +{ + /** + * Generates deterministic UUID based on SHA1 of static namespace (also UUID) and name. + * + * @param string $name + * + * @return static + */ + final public static function create(string $name) + { + $uuid = \Ramsey\Uuid\Uuid::uuid5(self::namespace()->toString(), $name)->toString(); + + return new static($uuid); + } + + /** + * Namespace UUID required for UUID v5 generation. + * + * @return UUID + */ + abstract protected static function namespace() : UUID; +} diff --git a/src/Infrastructure/EventStore/InMemoryEventStore.php b/src/Infrastructure/EventStore/InMemoryEventStore.php index ecab94aa..d0043280 100644 --- a/src/Infrastructure/EventStore/InMemoryEventStore.php +++ b/src/Infrastructure/EventStore/InMemoryEventStore.php @@ -78,7 +78,7 @@ public function add(Domain\Id $producerId, ?int $version, Event ...$events) : vo throw new Exception\EventAlreadyInStore($event); } - $uuid = Domain\Id\UUID::create()->toString(); + $uuid = Domain\Id\UUID4::create()->toString(); if (!isset($this->streams[$stream])) { $this->streams[$stream] = []; diff --git a/src/Infrastructure/EventStore/PDOPostgresEventStore.php b/src/Infrastructure/EventStore/PDOPostgresEventStore.php index eb7de932..61969329 100644 --- a/src/Infrastructure/EventStore/PDOPostgresEventStore.php +++ b/src/Infrastructure/EventStore/PDOPostgresEventStore.php @@ -19,6 +19,7 @@ use Streak\Domain\EventStore; use Streak\Domain\Exception; use Streak\Domain\Id\UUID; +use Streak\Domain\Id\UUID4; /** * @author Alan Gabriel Bem @@ -147,7 +148,7 @@ public function add(Domain\Id $producerId, ?int $version, Event ...$events) : vo throw new Exception\EventAlreadyInStore($event); } - $uuid = UUID::create(); + $uuid = UUID4::create(); $version = $this->bumpUp($version); $row = $this->toRow($producerId, $version, $uuid, $event); @@ -199,7 +200,7 @@ public function add(Domain\Id $producerId, ?int $version, Event ...$events) : vo $sequence = (string) $returned['number']; $uuid = $returned['uuid']; $uuid = mb_strtoupper($uuid); - $uuid = new UUID($uuid); + $uuid = new UUID4($uuid); $metadata = $returned['metadata']; $metadata = json_decode($metadata, true); $metadata = Event\Metadata::fromArray($metadata); diff --git a/tests/Infrastructure/UnitOfWorkTest.php b/tests/Infrastructure/UnitOfWorkTest.php index bfe0edc4..53d842d1 100644 --- a/tests/Infrastructure/UnitOfWorkTest.php +++ b/tests/Infrastructure/UnitOfWorkTest.php @@ -16,7 +16,7 @@ use PHPUnit\Framework\TestCase; use Streak\Domain\Event; use Streak\Domain\EventStore; -use Streak\Domain\Id\UUID; +use Streak\Domain\Id\UUID4; use Streak\Infrastructure\UnitOfWorkTest\NonVersionableEventSourcedStub; use Streak\Infrastructure\UnitOfWorkTest\VersionableEventSourcedStub; @@ -70,10 +70,10 @@ public function setUp() public function testObject() { - $id1 = UUID::create(); - $id2 = UUID::create(); - $id3 = UUID::create(); - $id4 = UUID::create(); + $id1 = UUID4::create(); + $id2 = UUID4::create(); + $id3 = UUID4::create(); + $id4 = UUID4::create(); $object1 = new VersionableEventSourcedStub($id1, 0, $this->event1); $object2 = new VersionableEventSourcedStub($id2, 1, $this->event2); @@ -165,8 +165,8 @@ public function testError() { $exception = new \RuntimeException(); - $id1 = UUID::create(); - $id2 = UUID::create(); + $id1 = UUID4::create(); + $id2 = UUID4::create(); $object1 = new VersionableEventSourcedStub($id1, 0, $this->event1); $object2 = new VersionableEventSourcedStub($id2, 0, $this->event2);