Last active
December 9, 2021 19:45
-
-
Save dgafka/64cf74dd61e074006156e47d07a154c7 to your computer and use it in GitHub Desktop.
Event Sourcing PHP 04
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 | |
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