Skip to content

Instantly share code, notes, and snippets.

@prolic
Last active February 25, 2016 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prolic/a320d43ee1ca709384fb to your computer and use it in GitHub Desktop.
Save prolic/a320d43ee1ca709384fb to your computer and use it in GitHub Desktop.
inheritance aggregate root with prooph
A minor patch to the aggregate repository is needed for this! see: https://github.com/prooph/event-store/pull/154
<?php
abstract class User extends \Prooph\EventSourcing\AggregateRoot
{
protected $type;
public function type()
{
return $this->type;
}
}
class Admin extends User
{
public static function register($name, $email)
{
$self = new self();
$self->recordThat(UserWasRegisterd::withData('admin', $name, $email);
return $self;
}
}
class Member extends User
{
public static function register($name, $email)
{
$self = new self();
$self->recordThat(UserWasRegisterd::withData('member', $name, $email);
return $self;
}
}
final class UserAggregateTranslator extends \Prooph\EventSourcing\EventStoreIntegration\AggregateTranslator
{
/**
* @param \Prooph\EventStore\Aggregate\AggregateType $aggregateType
* @param \Iterator $historyEvents
* @return object reconstructed AggregateRoot
*/
public function reconstituteAggregateFromHistory(
\Prooph\EventStore\Aggregate\AggregateType $aggregateType,
\Iterator $historyEvents
) {
$aggregateRootDecorator = $this->getAggregateRootDecorator();
$firstEvent = $historyEvents->current();
$type = $firstEvent->type();
if ($type === 'admin') {
return $aggregateRootDecorator->fromHistory(Admin::class, $historyEvents);
} elseif ($type === 'member') {
return $aggregateRootDecorator->fromHistory(Member::class, $historyEvents);
}
}
}
final class EventStoreUserCollection extends
\Prooph\EventStore\Aggregate\AggregateRepository
{
public function add(User $user)
{
$this->addAggregateRoot($user);
}
public function get(UserId $userId)
{
return $this->getAggregateRoot($userId->toString());
}
protected function assertAggregateType($eventSourcedAggregateRoot)
{
\Assert\Assertion::isInstanceOf($eventSourcedAggregateRoot, User::class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment