Created
December 10, 2021 16:38
-
-
Save dgafka/3ec12762a671932cf7a8eca1b03c13b8 to your computer and use it in GitHub Desktop.
Event Sourcing PHP Example 05
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
<?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