Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduced uuid v3, v4 and v5 value objects #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Domain/Event/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 5 additions & 16 deletions src/Domain/Id/UUID.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@
use Streak\Domain;

/**
* UUID v4.
*
* @author Alan Gabriel Bem <[email protected]>
*
* @see https://gist.github.com/tdomarkas/c5fbc10385ae004cbde6
*/
class UUID implements Domain\Id
abstract class UUID implements Domain\Id
{
private $value;

Expand All @@ -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;
Expand All @@ -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);
}
}
45 changes: 45 additions & 0 deletions src/Domain/Id/UUID3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* 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 <[email protected]>
*/
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;
}
34 changes: 34 additions & 0 deletions src/Domain/Id/UUID4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* 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 <[email protected]>
*/
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);
}
}
43 changes: 43 additions & 0 deletions src/Domain/Id/UUID5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* This file is part of the streak package.
*
* (C) Alan Gabriel Bem <[email protected]>
*
* 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 <[email protected]>
*/
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;
}
2 changes: 1 addition & 1 deletion src/Infrastructure/EventStore/InMemoryEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [];
Expand Down
5 changes: 3 additions & 2 deletions src/Infrastructure/EventStore/PDOPostgresEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions tests/Infrastructure/UnitOfWorkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down