Eloquent
Your Models as Aggregates
#[Aggregate]
final class Issue extends Model
{
use WithEvents;
#[CommandHandler()] // 1. issue.report factory method
public static function reportNew(ReportIssue $command): self
{
$issue = new self();
$issue->email = $command->email->address;
$issue->open = true;
$issue->content = $command->content;
$issue->save(); // 3. Saving
// 4. Event publishing
$issue->recordThat(new IssueWasReported($issue->id));
return $issue;
}
#[CommandHandler("issue.close")] // 2. issue.close action method
public function close(): void
{
if (!$this->open) {
return;
}
$this->open = false;
$this->recordThat(new IssueWasClosed($this->id));
}
#[AggregateIdentifierMethod("id")]
public function getId(): ?int
{
return $this->id;
}
}Demo
Last updated
Was this helpful?