Skip to content
Snippets Groups Projects
Commit e65ebe19 authored by Jan Kuchař's avatar Jan Kuchař
Browse files

Extracted EventsApplicable and ReconstitutesFromHistory interfaces

parent c9938409
Branches
No related tags found
No related merge requests found
<?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
<?php
interface ReconstitutesFromHistory
{
/**
* @param \AggregateHistory $aggregateHistory
* @return static
*/
public static function reconstituteFrom(AggregateHistory $aggregateHistory);
}
\ No newline at end of file
...@@ -5,4 +5,5 @@ interface RecordsEvents ...@@ -5,4 +5,5 @@ interface RecordsEvents
public function getRecordedEvents(): DomainEvents; public function getRecordedEvents(): DomainEvents;
public function clearRecordedEvents(); public function clearRecordedEvents();
} }
\ No newline at end of file
<?php <?php
abstract class AbstractAggregate abstract class AbstractAggregate implements RecordsEvents, EventsApplicable, ReconstitutesFromHistory
{ {
// ------------- implementation of EventApplicable ------------------------
/** /**
* @param \DomainEvent $domainEvent * @param \DomainEvent $domainEvent
* @return string * @return string
...@@ -12,11 +13,6 @@ abstract class AbstractAggregate ...@@ -12,11 +13,6 @@ abstract class AbstractAggregate
return "apply" . get_class($domainEvent); return "apply" . get_class($domainEvent);
} }
/**
* Apply domain event if this aggregate accepts this event
* @param \DomainEvent $domainEvent
* @internal
*/
public function applyIfAccepts(DomainEvent $domainEvent) public function applyIfAccepts(DomainEvent $domainEvent)
{ {
if(method_exists($this, $this->getApplyMethodForDomainEvent($domainEvent))) { if(method_exists($this, $this->getApplyMethodForDomainEvent($domainEvent))) {
...@@ -24,14 +20,42 @@ abstract class AbstractAggregate ...@@ -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) public function apply(DomainEvent $domainEvent)
{ {
$method = $this->getApplyMethodForDomainEvent($domainEvent); $method = $this->getApplyMethodForDomainEvent($domainEvent);
$this->$method($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
<?php <?php
final class Basket extends AbstractAggregate implements RecordsEvents final class Basket extends AbstractAggregate
{ {
/** @var BasketId $basketId */ /** @var BasketId $basketId */
...@@ -29,17 +29,6 @@ final class Basket extends AbstractAggregate implements RecordsEvents ...@@ -29,17 +29,6 @@ final class Basket extends AbstractAggregate implements RecordsEvents
return $basket; 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 interface --------------
public function addProduct(ProductId $productId, $name) public function addProduct(ProductId $productId, $name)
{ {
...@@ -111,23 +100,4 @@ final class Basket extends AbstractAggregate implements RecordsEvents ...@@ -111,23 +100,4 @@ final class Basket extends AbstractAggregate implements RecordsEvents
return isset($this->itemsCountById[(string) $productId]) && ($this->itemsCountById[(string) $productId] > 0); 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment