Skip to content

Commit

Permalink
simplify identities
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Feb 1, 2019
1 parent 03a056f commit 8e07f94
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 557 deletions.
34 changes: 6 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,19 @@ require './vendor/autoload.php';

### Aggregate identity

By extending `Gears\Aggregate\AbstractAggregateIdentity` you can easily create your own aggregate identities

```php
use Gears\Aggregate\AbstractAggregateIdentity;

class CustomAggregateIdentity extends AbstractAggregateIdentity
{
public static function fromString(string $value)
{
// Check $value validity

return new static($value);
}
}
```

Most widely used aggregate identities are UUID based, for that reason some UUID aggregate identity classes are provided for you to use or extend from

* `Gears\Aggregate\UuidAggregateIdentity` Basic plain UUID aggregate identity
* `Gears\Aggregate\ShortUuidAggregateIdentity` Shortened UUID aggregate identity. Requires [pascaldevink/shortuuid](https://github.com/pascaldevink/shortuuid)
* `Gears\Aggregate\HashUuidAggregateIdentity` Hashed UUID aggregate identity. Requires [hashids/hashids](https://github.com/ivanakimov/hashids.php)

Both ShortUuidAggregateIdentity and HashUuidAggregateIdentity allows you to recover original UUID

If you want to expand on UUID aggregate identities head to [gears/identity](https://github.com/phpgears/identity)
Aggregate identities are provided by [gears/identity](https://github.com/phpgears/identity), head over there to learn about them

### Aggregate root

Aggregate roots should implement `Gears\Aggregate\AggregateRoot` interface. You can extend from `Gears\Aggregate\AbstractAggregateRoot` for simplicity

```php
use Gears\Aggregate\AbstractAggregateRoot;
use Gears\Identity\Identity

class CustomAggregate extends AbstractAggregateRoot
{
public static function instantiate(AggregateIdentity $identity): self
public static function instantiate(Identity $identity): self
{
return new self($identity);
}
Expand All @@ -86,10 +63,11 @@ Aggregate roots can record [gears/event](https://github.com/phpgears/event) as o

```php
use Gears\Aggregate\AbstractAggregateRoot;
use Gears\Identity\Identity;

class CustomAggregate extends AbstractAggregateRoot
{
public static function instantiate(AggregateIdentity $identity): self
public static function instantiate(Identity $identity): self
{
return new self($identity);
}
Expand All @@ -106,7 +84,7 @@ class CustomAggregate extends AbstractAggregateRoot
This events could later be collected and sent to an event bus such as [gears/event](https://github.com/phpgears/event)

```php
$customAggregate = new UuidAggregate(CustomAggregateIdentity::fromString('4c4316cb-b48b-44fb-a034-90d789966bac'));
$customAggregate = new UuidAggregate(CustomIdentity::fromString('4c4316cb-b48b-44fb-a034-90d789966bac'));
$customAggregate->doSomething();

foreach ($customAggregate->collectRecordedEvents() as $event) {
Expand Down
2 changes: 0 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ parameters:
level: max
paths:
- src
ignoreErrors:
- '/^Call to an undefined method object::getValue\(\)\.$/'
105 changes: 0 additions & 105 deletions src/AbstractAggregateIdentity.php

This file was deleted.

51 changes: 33 additions & 18 deletions src/AbstractAggregateRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,76 @@
use Gears\Event\Event;
use Gears\Event\EventArrayCollection;
use Gears\Event\EventCollection;
use Gears\Identity\Identity;

/**
* Abstract aggregate root class.
*/
abstract class AbstractAggregateRoot implements AggregateRoot
{
/**
* @var AggregateIdentity
* @var Identity
*/
private $identity;

/**
* @var Event[]
*/
private $events = [];
private $recordedEvents = [];

/**
* AbstractAggregateRoot constructor.
*
* @param AggregateIdentity $identity
* @param Identity $identity
*/
final protected function __construct(AggregateIdentity $identity)
final protected function __construct(Identity $identity)
{
$this->identity = $identity;
}

/**
* Get aggregate identity.
*
* @return AggregateIdentity
* {@inheritdoc}
*/
final public function getIdentity(): AggregateIdentity
final public function getIdentity(): Identity
{
return $this->identity;
}

/**
* {@inheritdoc}
* Record event.
*
* @param Event $event
*/
final public function collectRecordedEvents(): EventCollection
final protected function recordEvent(Event $event): void
{
$events = new EventArrayCollection($this->events);
$this->recordedEvents[] = $event;
}

$this->events = [];
/**
* {@inheritdoc}
*/
final public function getRecordedEvents(): EventCollection
{
return new EventArrayCollection($this->recordedEvents);
}

return $events;
/**
* {@inheritdoc}
*/
final public function clearRecordedEvents(): void
{
$this->recordedEvents = [];
}

/**
* Record event.
*
* @param Event $event
* {@inheritdoc}
*/
final protected function recordEvent(Event $event): void
final public function collectRecordedEvents(): EventCollection
{
$this->events[] = $event;
$events = new EventArrayCollection($this->recordedEvents);

$this->recordedEvents = [];

return $events;
}
}
23 changes: 0 additions & 23 deletions src/AggregateIdentity.php

This file was deleted.

17 changes: 15 additions & 2 deletions src/AggregateRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Gears\Aggregate;

use Gears\Event\EventCollection;
use Gears\Identity\Identity;

/**
* AggregateRoot interface.
Expand All @@ -23,9 +24,21 @@ interface AggregateRoot
/**
* Get aggregate identity.
*
* @return AggregateIdentity
* @return Identity
*/
public function getIdentity(): AggregateIdentity;
public function getIdentity(): Identity;

/**
* Get recorded events.
*
* @return EventCollection
*/
public function getRecordedEvents(): EventCollection;

/**
* Remove recorded events from aggregate root.
*/
public function clearRecordedEvents(): void;

/**
* Collect recorded events and remove them from aggregate root.
Expand Down
51 changes: 0 additions & 51 deletions src/HashUuidAggregateIdentity.php

This file was deleted.

0 comments on commit 8e07f94

Please sign in to comment.