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
No related branches found
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
public function getRecordedEvents(): DomainEvents;
public function clearRecordedEvents();
}
\ No newline at end of file
<?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
<?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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment