Skip to content

Instantly share code, notes, and snippets.

@dgafka
Created December 10, 2021 16:38
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 dgafka/3ec12762a671932cf7a8eca1b03c13b8 to your computer and use it in GitHub Desktop.
Save dgafka/3ec12762a671932cf7a8eca1b03c13b8 to your computer and use it in GitHub Desktop.
Event Sourcing PHP Example 05
<?php
#[EventSourcingAggregate] // 1
class Product
{
#[AggregateIdentifier] // 2
private int $productId;
use WithAggregateVersioning; // 3
private float $price;
#[CommandHandler] // 4
public static function register(RegisterProduct $command): array
{
return [new ProductWasRegistered($command->getProductId(), $command->getPrice())];
}
#[CommandHandler]
public function changePrice(ChangePrice $command): array
{
return [new PriceWasChanged($this->productId, $command->getPrice())];
}
#[EventSourcingHandler] // 5
public function onProductWasRegistered(ProductWasRegistered $event): void
{
$this->productId = $event->getProductId();
$this->price = $event->getPrice();
}
#[EventSourcingHandler]
public function onPriceWasChanged(PriceWasChanged $event): void
{
$this->price = $event->getPrice();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment