Skip to content

Instantly share code, notes, and snippets.

@dgafka
Last active December 9, 2021 19:45
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/64cf74dd61e074006156e47d07a154c7 to your computer and use it in GitHub Desktop.
Save dgafka/64cf74dd61e074006156e47d07a154c7 to your computer and use it in GitHub Desktop.
Event Sourcing PHP 04
<?php
class PriceChangeOverTimeProjection
{
private array $priceChangeOverTime = [];
public function registerPriceChange(PriceWasChanged $event): void
{
$lastPrice = end($this->priceChangeOverTime[$event->getProductId()]);
$this->priceChangeOverTime[$event->getProductId()][] = new PriceChange($event->getPrice(), $event->getPrice() - $lastPrice->getPrice());
}
public function getPriceChangesFor(int $productId): array
{
if (!isset($this->priceChangeOverTime[$productId])) {
return [];
}
return $this->priceChangeOverTime[$productId];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment