Last active
February 25, 2016 12:45
-
-
Save prolic/a320d43ee1ca709384fb to your computer and use it in GitHub Desktop.
inheritance aggregate root with prooph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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