Skip to content

Commit

Permalink
Merge 0fa6a0d into 028b697
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Jan 5, 2019
2 parents 028b697 + 0fa6a0d commit c9281b5
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 89 deletions.
12 changes: 12 additions & 0 deletions docs/docs/changelog.md
Expand Up @@ -5,6 +5,18 @@ published_at: 2018-03-14
updated_at: 2018-03-26
---

## 0.5.0

### Breaking Changes

The abstract `BaseAggregateRoot` has now been removed and all the traits have
been collapsed into one. This trait has been moved to the root namespace.

## Fixed

* Multiple interactions and intermediate persisting of aggregates now has correct
versioning of messages.

## 0.4.0

### Fixed
Expand Down
14 changes: 1 addition & 13 deletions docs/docs/getting-started/1-creating-an-aggregate-root.md
Expand Up @@ -18,26 +18,14 @@ the interface so you won't have to.
namespace AcmeCompany\AcmeProject;

use EventSauce\EventSourcing\AggregateRoot;
use EventSauce\EventSourcing\AggregateRootBehaviour\AggregateRootBehaviour;
use EventSauce\EventSourcing\AggregateRootBehaviour\ConstructionBehaviour;
use EventSauce\EventSourcing\AggregateRootBehaviour\EventApplyingBehaviour;
use EventSauce\EventSourcing\AggregateRootBehaviour\EventRecordingBehaviour;
use EventSauce\EventSourcing\AggregateRootBehaviour;

class AcmeProcess implements AggregateRoot
{
// Use all traits.
use AggregateRootBehaviour;

// OR use individually
use ConstructionBehaviour,
EventApplyingBehaviour,
EventRecordingBehaviour;
}
```

Or if you're feeling extra lazy, you can just extend the `EventSauce\EventSourcing\BaseAggregateRoot`
base class.

## Aggregate Root ID

An aggregate root has an identifier. This is called the "aggregate root ID".
Expand Down
Expand Up @@ -2,21 +2,27 @@

declare(strict_types=1);

namespace EventSauce\EventSourcing\AggregateRootBehaviour;
namespace EventSauce\EventSourcing;

use EventSauce\EventSourcing\AggregateRoot;
use EventSauce\EventSourcing\AggregateRootId;
use Generator;

trait ConstructionBehaviour
trait AggregateRootBehaviour
{
/**
* @var AggregateRootId
*/
private $aggregateRootId;

/**
* @var int
*/
private $aggregateRootVersion = 0;

/**
* @var object[]
*/
private $recordedEvents = [];

public function __construct(AggregateRootId $aggregateRootId)
{
$this->aggregateRootId = $aggregateRootId;
Expand All @@ -37,6 +43,7 @@ public function aggregateRootVersion(): int
* @param Generator $events
*
* @return static
* @see AggregateRoot::reconstituteFromEvents
*/
public static function reconstituteFromEvents(AggregateRootId $aggregateRootId, Generator $events): AggregateRoot
{
Expand All @@ -45,11 +52,32 @@ public static function reconstituteFromEvents(AggregateRootId $aggregateRootId,
/** @var object $event */
foreach ($events as $event) {
$aggregateRoot->apply($event);
++$aggregateRoot->aggregateRootVersion;
}

return $aggregateRoot;
}

abstract protected function apply(object $event);
protected function apply(object $event)
{
$parts = explode('\\', get_class($event));
$this->{'apply' . end($parts)}($event);
$this->aggregateRootVersion++;
}

protected function recordThat(object $event)
{
$this->apply($event);
$this->recordedEvents[] = $event;
}

/**
* @return object[]
*/
public function releaseEvents(): array
{
$releasedEvents = $this->recordedEvents;
$this->recordedEvents = [];

return $releasedEvents;
}
}
10 changes: 0 additions & 10 deletions src/AggregateRootBehaviour/AggregateRootBehaviour.php

This file was deleted.

14 changes: 0 additions & 14 deletions src/AggregateRootBehaviour/EventApplyingBehaviour.php

This file was deleted.

32 changes: 0 additions & 32 deletions src/AggregateRootBehaviour/EventRecordingBehaviour.php

This file was deleted.

12 changes: 0 additions & 12 deletions src/BaseAggregateRoot.php

This file was deleted.

5 changes: 5 additions & 0 deletions src/ConstructingAggregateRootRepository.php
Expand Up @@ -5,6 +5,7 @@
namespace EventSauce\EventSourcing;

use function assert;
use function count;
use Generator;

final class ConstructingAggregateRootRepository implements AggregateRootRepository
Expand Down Expand Up @@ -71,6 +72,10 @@ public function persist(object $aggregateRoot)

public function persistEvents(AggregateRootId $aggregateRootId, int $aggregateRootVersion, object ...$events)
{
// decrease the aggregate root version by the number of raised events
// so the version of each message represents the version at the time
// of recording.
$aggregateRootVersion = $aggregateRootVersion - count($events);
$metadata = [Header::AGGREGATE_ROOT_ID => $aggregateRootId];
$messages = array_map(function (object $event) use ($metadata, &$aggregateRootVersion) {
return $this->decorator->decorate(new Message(
Expand Down
7 changes: 5 additions & 2 deletions src/Integration/TestingAggregates/DummyAggregate.php
Expand Up @@ -4,10 +4,13 @@

namespace EventSauce\EventSourcing\Integration\TestingAggregates;

use EventSauce\EventSourcing\BaseAggregateRoot;
use EventSauce\EventSourcing\AggregateRoot;
use EventSauce\EventSourcing\AggregateRootBehaviour;

class DummyAggregate extends BaseAggregateRoot
class DummyAggregate implements AggregateRoot
{
use AggregateRootBehaviour;

private $incrementedNumber = 0;

public function performDummyTask()
Expand Down
Expand Up @@ -13,6 +13,7 @@
use EventSauce\EventSourcing\Time\Clock;
use EventSauce\EventSourcing\UuidAggregateRootId;
use LogicException;
use function var_dump;

class ExampleAggregateRootTest extends AggregateRootTestCase
{
Expand Down

0 comments on commit c9281b5

Please sign in to comment.