Would you consider a trait like this to reduce boilerplate code in aggregates? Not sure that it would be suited to every use-case, but could certainly be useful..?
trait CanRecordEvents
{
/**
* @var DomainEvent[]
*/
private $latestRecordedEvents = [];
private function recordThat(DomainEvent $event)
{
$this->latestRecordedEvents[] = $event;
}
/**
* @return DomainEvents
*/
public function getRecordedEvents()
{
return new DomainEvents($this->latestRecordedEvents);
}
public function clearRecordedEvents()
{
$this->latestRecordedEvents = [];
}
}