From e65ebe1953d46ef52e8b7f902fd5c535489620b2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Kucha=C5=99?= <honza.kuchar@grifart.cz>
Date: Thu, 25 Feb 2016 15:51:49 +0100
Subject: [PATCH] Extracted EventsApplicable and ReconstitutesFromHistory
 interfaces

---
 app/EventsApplicable.php              | 17 +++++++++++
 app/ReconstitutesFromHistory.php      | 10 ++++++
 app/RecordsEvents.php                 |  1 +
 app/aggreagates/AbstractAggregate.php | 44 +++++++++++++++++++++------
 app/aggreagates/Basket.php            | 32 +------------------
 5 files changed, 63 insertions(+), 41 deletions(-)
 create mode 100644 app/EventsApplicable.php
 create mode 100644 app/ReconstitutesFromHistory.php

diff --git a/app/EventsApplicable.php b/app/EventsApplicable.php
new file mode 100644
index 0000000..a50e827
--- /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 0000000..66c8b6c
--- /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 c1c3458..22d200d 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 b0a8f83..87b50a3 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 6a9706d..5ff098e 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
-- 
GitLab