Skip to content

Instantly share code, notes, and snippets.

@MarkRedeman
Last active January 28, 2016 10:29
Show Gist options
  • Select an option

  • Save MarkRedeman/8c64af42edcc67b88e3f to your computer and use it in GitHub Desktop.

Select an option

Save MarkRedeman/8c64af42edcc67b88e3f to your computer and use it in GitHub Desktop.
Event sourcing modeling problem
<?php
/*
* A bit of background:
*
* Because this is a project where one of the main goals for me is to learn new techniques,
* I want to apply event sourcing to this project.
*
* We're modelling a student association where we want to manage memberships of the association.
* Most members will finish their studies in about 4 - 6 years and if they do not stay active
* within the association, then they will be noted as former members.
*
* A key difference between a member and a formerm member (could) be that a former member
* can't join committees and it will leave all committees once it becomes a former member.
*
* I'm curious about different ways to model this. I quite like the idea of using
* different classes for the different states of a member (inspired from: http://www.slideshare.net/jeroenvdgulik/2015-0415-object-invariants-immutability-you-njjmegenphp),
* though I'm not too sure if this is a good practice when using event sourcing.
* Morevoer I would have to change some of the methods of Broadway's EventSourcedAggregateRoot
* so that they will always return the current (or new eg FormerMember) model
*/
class Member extends EventSourcedAggregateRoot
{
public function reconstitute(EventStream $stream)
{
$member = new static;
return array_reduce($stream, function($member, $event) {
return $member->handle($event);
}, $member);
}
public function becomeFormerMember()
{
// apply will call applyMemberBecameFormerMember
return $this->apply(new MemberBecameFormerMember($this->id));
}
private function handleMemberBecameFormerMember(MemberBecameFormerMember $event)
{
$this->leaveCommittees();
return new FormerMember($this);
}
public function joinCommitte(CommitteeId $committee)
{
// do stuff
}
}
class FormermMember extends EventSourcedAggregateRoot
{
// does not contain joinCommittee
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment