diff --git a/app/EventsApplicable.php b/app/EventsApplicable.php new file mode 100644 index 0000000000000000000000000000000000000000..a50e8270ea9111ef4d14d4fa4dcd2082d270ecc9 --- /dev/null +++ b/app/EventsApplicable.php @@ -0,0 +1,17 @@ +<?php + +interface EventsApplicable +{ + /** + * Apply domain event if this aggregate accepts this event + * @param \DomainEvent $domainEvent + * @internal + */ + public function applyIfAccepts(DomainEvent $domainEvent); + + /** + * Apply domain event; if objects does not accepts this event -> fail + * @param \DomainEvent $domainEvent + */ + public function apply(DomainEvent $domainEvent); +} \ No newline at end of file diff --git a/app/ReconstitutesFromHistory.php b/app/ReconstitutesFromHistory.php new file mode 100644 index 0000000000000000000000000000000000000000..66c8b6c4251f5c1750e50127348e45fc3957387f --- /dev/null +++ b/app/ReconstitutesFromHistory.php @@ -0,0 +1,10 @@ +<?php + +interface ReconstitutesFromHistory +{ + /** + * @param \AggregateHistory $aggregateHistory + * @return static + */ + public static function reconstituteFrom(AggregateHistory $aggregateHistory); +} \ No newline at end of file diff --git a/app/RecordsEvents.php b/app/RecordsEvents.php index c1c34584f7afff0ab96254be69850fbb01037a88..22d200d6d1d624a0adcd2747efba077601e0f6b3 100644 --- a/app/RecordsEvents.php +++ b/app/RecordsEvents.php @@ -5,4 +5,5 @@ interface RecordsEvents public function getRecordedEvents(): DomainEvents; public function clearRecordedEvents(); + } \ No newline at end of file diff --git a/app/aggreagates/AbstractAggregate.php b/app/aggreagates/AbstractAggregate.php index b0a8f837b9d131d140f38a82955920153c3cf4ef..87b50a39d005a36b81925c8ee463b07c7abb4334 100644 --- a/app/aggreagates/AbstractAggregate.php +++ b/app/aggreagates/AbstractAggregate.php @@ -1,8 +1,9 @@ <?php -abstract class AbstractAggregate +abstract class AbstractAggregate implements RecordsEvents, EventsApplicable, ReconstitutesFromHistory { + // ------------- implementation of EventApplicable ------------------------ /** * @param \DomainEvent $domainEvent * @return string @@ -12,11 +13,6 @@ abstract class AbstractAggregate return "apply" . get_class($domainEvent); } - /** - * Apply domain event if this aggregate accepts this event - * @param \DomainEvent $domainEvent - * @internal - */ public function applyIfAccepts(DomainEvent $domainEvent) { if(method_exists($this, $this->getApplyMethodForDomainEvent($domainEvent))) { @@ -24,14 +20,42 @@ abstract class AbstractAggregate } } - /** - * Apply domain event; if objects does not accepts this event -> fail - * @param \DomainEvent $domainEvent - */ public function apply(DomainEvent $domainEvent) { $method = $this->getApplyMethodForDomainEvent($domainEvent); $this->$method($domainEvent); } + // --------------------------------------------------------------------- + + public static function reconstituteFrom(AggregateHistory $aggregateHistory): self + { + $basketId = $aggregateHistory->getAggregateId(); + $basket = new static(new BasketId($basketId)); + + foreach($aggregateHistory as $event) { + $basket->apply($event); + } + return $basket; + } + + // -------- implementation of RecordsEvents ------------------ + private $recordedEvents = []; + + public function getRecordedEvents(): DomainEvents + { + return new DomainEvents($this->recordedEvents); + } + + public function clearRecordedEvents() + { + $this->recordedEvents = []; + } + + protected function recordThat(DomainEvent $domainEvent) + { + $this->recordedEvents[] = $domainEvent; + $this->apply($domainEvent); + } + } \ No newline at end of file diff --git a/app/aggreagates/Basket.php b/app/aggreagates/Basket.php index 6a9706d9557d321de978f23d93094087736050e8..5ff098e8d935997901d94d1ec05cd4d846813986 100644 --- a/app/aggreagates/Basket.php +++ b/app/aggreagates/Basket.php @@ -1,6 +1,6 @@ <?php -final class Basket extends AbstractAggregate implements RecordsEvents +final class Basket extends AbstractAggregate { /** @var BasketId $basketId */ @@ -29,17 +29,6 @@ final class Basket extends AbstractAggregate implements RecordsEvents return $basket; } - public static function reconstituteFrom(AggregateHistory $aggregateHistory): self - { - $basketId = $aggregateHistory->getAggregateId(); - $basket = new static(new BasketId($basketId)); - - foreach($aggregateHistory as $event) { - $basket->apply($event); - } - return $basket; - } - // ------------- public interface -------------- public function addProduct(ProductId $productId, $name) { @@ -111,23 +100,4 @@ final class Basket extends AbstractAggregate implements RecordsEvents return isset($this->itemsCountById[(string) $productId]) && ($this->itemsCountById[(string) $productId] > 0); } - // -------- implementation of RecordsEvents ------------------ - private $recordedEvents = []; - - public function getRecordedEvents(): DomainEvents - { - return new DomainEvents($this->recordedEvents); - } - - public function clearRecordedEvents() - { - $this->recordedEvents = []; - } - - private function recordThat(DomainEvent $domainEvent) - { - $this->recordedEvents[] = $domainEvent; - $this->apply($domainEvent); - } - } \ No newline at end of file