11. - Cuandos los clientes entran en el café se sientan en una
mesa , un camarero/a , abre una cuenta para esa mesa
- Los clientes pueden ordenar bebida y/o comidas del
menú
- Una vez ordenadas las bebidas pueden ser servidas de
inmediato.
- La comida debe ser preparada en cocina. Una vez se ha
preparado puede ser servida
- Cuando los clientes terminan de comer pagan la cuenta (
pueden dejar propina ) y la cuenta se cierra
- No se puede cerrar una cuenta si hay bebida o comida
pendientes
14. Trasladar el modelo a objetos ( DDD táctico )
Creamos tests para asegurar su corrección
Arquitectura hexagonal para conectar el modelo con el mundo
exterior
16. <?php
class Tab
{
static public function open($table, $waiter): Tab {}
public function placeOrder($orderedItems) {}
public function serveDrinks($drinksServed) {}
public function prepareFood($foodPrepared) {}
public function serveFood($foodServed) {}
public function close(float $amount) {}
}
interface TabRepository
{
public function getById(TabId $tabId);
public function save(Tab $tab);
}
17. <?php
class OrderedItem
{
public function __construct(int $menuNumber, bool $IsDrink, float $price) {}
public function getMenuNumber(): int {}
public function isDrink(): bool {}
public function getPrice(): float {}
}
interface OrderedItemsRepository
{
public function findById($id): OrderedItem;
}
22. <?php
interface TabRepository
{
public function getById(TabId $tabId);
public function save(Tab $tab);
public function getTabsByWaiter($waiter);
public function getTabsWithDrinkPending();
public function getTabsWithFoodPrepared();
public function getTabsOpen();
public function getTabsClosed(DateTime $date);
}
38. class OpenTabCommand
{
private $tabId;
private $tableNumber;
private $waiterId;
public function __construct($tabId, $tableNumber, $waiterId)
{
$this->tabId = $tabId;
$this->tableNumber = $tableNumber;
$this->waiterId = $waiterId;
}
public function getTabId() { }
public function getTableNumber() { }
public function getWaiterId() { }
}
39. class OpenTabHandler
{
private $tabRepopsitory;
public function __construct(TabRepository $tabRepository)
{
$this->tabRepopsitory = $tabRepository;
}
public function handle(OpenTabCommand $command)
{
$newTab = Tab::openWithId(
TabId::fromString($command->getTabId()),
$command->getTableNumber(),
$command->getWaiterId()
);
$this->tabRepopsitory->add($newTab);
}
}
45. <?php
interface TabRepository
{
public function getById(TabId $tabId);
public function save(Tab $tab);
}
interface TabViewRepository
{
public function getTabsByWaiter($waiter);
public function getTabsWithDrinkPending();
public function getTabsWithFoodPrepared();
public function getTabsOpen();
public function getTabsClosed(DateTime $date);
}
59. Se ha abierto una cuenta
Se ordenan bebidas y comida
La comida está preparada
Las bebidas se han servido
La comida se ha preparado
La comida se ha servidor
Se ha cerrado una cuenta
67. El estado de nuestro sistema
es la suma de todos los eventos
ocurridos en el.
68. A1 {
x = 1
y = 2
}
ID X Y
A1 1 2
A2 3 5 A2 {
x = 3
y = 5
}
69. A2, y = 5
A1, y = 2
A2, x = 3
A1 , y = 7
A2, x = null y = null
A1 , x = 1
A1, x = null y = null
A1 {
x = null
y = null
}
Tiempo
70. A2, y = 5
A1, y = 2
A2, x = 3
A1 , y = 7
A2, x = null y = null
A1 , x = 1
A1, x = null y = null
A1 {
x = null
y = null
}
A1 {
x = 1
y = null
}
Tiempo
71. A2, y = 5
A1, y = 2
A2, x = 3
A1 , y = 7
A2, x = null y = null
A1 , x = 1
A1, x = null y = null
A1 {
x = null
y = null
}
A1 {
x = 1
y = null
}
A2 {
x = null
y = null
}
Tiempo
72. A2, y = 5
A1, y = 2
A2, x = 3
A1 , y = 7
A2, x = null y = null
A1 , x = 1
A1, x = null y = null
A1 {
x = null
y = null
}
A1 {
x = 1
y = null
}
A1 {
x = 1
y = 7
}
A2 {
x = null
y = null
}
Tiempo
73. A2, y = 5
A1, y = 2
A2, x = 3
A1 , y = 7
A2, x = null y = null
A1 , x = 1
A1, x = null y = null
A1 {
x = null
y = null
}
A1 {
x = 1
y = null
}
A1 {
x = 1
y = 7
}
A2 {
x = null
y = null
}
A2 {
x = 3
y = null
}
Tiempo
74. A2, y = 5
A1, y = 2
A2, x = 3
A1 , y = 7
A2, x = null y = null
A1 , x = 1
A1, x = null y = null
A1 {
x = null
y = null
}
A1 {
x = 1
y = null
}
A1 {
x = 1
y = 7
}
A1 {
x = 1
y = 2
}
A2 {
x = null
y = null
}
A2 {
x = 3
y = null
}
Tiempo
75. A2, y = 5
A1, y = 2
A2, x = 3
A1 , y = 7
A2, x = null y = null
A1 , x = 1
A1, x = null y = null
A1 {
x = null
y = null
}
A1 {
x = 1
y = null
}
A1 {
x = 1
y = 7
}
A1 {
x = 1
y = 2
}
A2 {
x = null
y = null
}
A2 {
x = 3
y = null
}
A2 {
x = 3
y = 5
}
Tiempo
76. A2, y = 5
A1, y = 2
A2, x = 3
A1 , y = 7
A2, x = null y = null
A1 , x = 1
A1, x = null y = null
A1 {
x = null
y = null
}
A1 {
x = 1
y = null
}
A1 {
x = 1
y = 7
}
A1 {
x = 1
y = 2
}
A2 {
x = null
y = null
}
A2 {
x = 3
y = null
}
A2 {
x = 3
y = null
}
89. Entidad raíz
Id del agregado
Almacenar los eventos
Reconstruir desde flujo de eventos
90. <?php
class Tab
{
static public function open($table, $waiter): Tab {}
public function placeOrder($orderedItems) {}
public function serveDrinks($drinksServed) {}
public function prepareFood($foodPrepared) {}
public function serveFood($foodServed) {}
public function close(float $amount) {}
}
91. <?php
class Tab
{
// TabOpened
static public function open($table, $waiter): Tab {}
// DrinksOrdered , FoodOrdered
public function placeOrder($orderedItems) {}
// DrinksServed
public function serveDrinks($drinksServed) {}
// FoodPrepared
public function prepareFood($foodPrepared) {}
// FoodServed
public function serveFood($foodServed) {}
// TabClosed
public function close(float $amount) {}
}
92. <?php
class Tab {
static public function open($table, $waiter): Tab
{
$id = TabId::create();
$newTab = new Tab($id, $table, $waiter);
DomainEventPublisher::instance()->publish(
new TabOpened($id, $table, $waiter)
);
return $newTab;
}
}
93. <?php
class Tab {
static public function open($table, $waiter): Tab
{
$id = TabId::create();
$newTab = new Tab($id, $table, $waiter);
DomainEventPublisher::instance()->publish(
new TabOpened($id, $table, $waiter)
);
return $newTab;
}
}
94. <?php
class Tab extends Aggregate {
static public function open($table, $waiter): Tab
{
$id = TabId::create();
$newTab = new Tab($id, $table, $waiter);
$this->recordThat(new TabOpened($id, $table, $waiter));
return $newTab;
}
}
95. abstract class Aggregate implements AggregateRoot
{
private $recordedEvents = [];
protected function recordThat(DomainEvent $aDomainEvent)
{
$this->recordedEvents[] = $aDomainEvent;
}
public function getRecordedEvents(): DomainEvents
{
return new DomainEvents($this->recordedEvents);
}
public function clearRecordedEvents()
{
$this->recordedEvents = [];
}
}
96. abstract class Aggregate implements AggregateRoot
{
public static function reconstituteFrom(AggregateHistory $anAggregateHistory) {
$anAggregate = static::createEmptyWithId(
$anAggregateHistory->getAggregateId()
);
foreach ($anAggregateHistory as $anEvent) {
$anAggregate->apply($anEvent);
}
return $anAggregate;
}
private function apply($anEvent)
{
$method = 'apply' . ClassFunctions::short($anEvent);
$this->$method($anEvent);
}
}
97. class Tab extends Aggregate {
public function applyDrinksServed(DrinksServed $drinksServed)
{
array_walk($drinksServed->getItems(),
function($drinkServedNumber) {
$item = $this->outstandingDrinks[$drinkServedNumber];
unset($this->outstandingDrinks[$drinkServedNumber]);
$this->servedItems[$drinkServedNumber] = $item;
});
}
}
98. class Tab extends Aggregate {
public function applyDrinksServed(DrinksServed $drinksServed)
{
array_walk($drinksServed->getItems(),
function($drinkServedNumber) {
$item = $this->outstandingDrinks[$drinkServedNumber];
unset($this->outstandingDrinks[$drinkServedNumber]);
$this->servedItems[$drinkServedNumber] = $item;
});
}
}
100. <?php
class Tab {
public function serveDrinks($drinksServed)
{
$this->assertDrinksAreOutstanding($drinksServed);
array_walk($drinksServed, function($drinkServedNumber) {
$item = $this->outstandingDrinks[$drinkServedNumber];
unset($this->outstandingDrinks[$drinkServedNumber]);
$this->servedItems[$drinkServedNumber] = $item;
});
}
}
101. <?php
class Tab extends Aggregate {
public function serveDrinks($drinksServed)
{
$this->assertDrinksAreOutstanding($drinksServed);
array_walk($drinksServed, function($drinkServedNumber) {
$item = $this->outstandingDrinks[$drinkServedNumber];
unset($this->outstandingDrinks[$drinkServedNumber]);
$this->servedItems[$drinkServedNumber] = $item;
});
$this->recordThat(new DrinksServed(
$this->getAggregateId(),
$drinksServed
));
}
}
102. <?php
class Tab extends Aggregate {
public function applyDrinksServed(DrinksServed $drinksServed)
{
array_walk($drinksServed->getItems(),
function($drinkServedNumber) {
$item = $this->outstandingDrinks[$drinkServedNumber];
unset($this->outstandingDrinks[$drinkServedNumber]);
$this->servedItems[$drinkServedNumber] = $item;
});
}
}
103. <?php
class Tab extends Aggregate {
public function serveDrinks($drinksServed)
{
$this->assertDrinksAreOutstanding($drinksServed);
array_walk($drinksServed, function($drinkServedNumber) {
$item = $this->outstandingDrinks[$drinkServedNumber];
unset($this->outstandingDrinks[$drinkServedNumber]);
$this->servedItems[$drinkServedNumber] = $item;
});
$this->recordThat(new DrinksServed(
$this->getAggregateId(),
$drinksServed
));
}
}
104. <?php
class Tab extends Aggregate {
public function serveDrinks($drinksServed)
{
$this->assertDrinksAreOutstanding($drinksServed);
$drinksServedEvend = new DrinksServed(
$this->getAggregateId(),
$drinksServed
);
$this->recordThat($drinksServedEvend);
$this->apply($drinksServedEvend);
}
}
105. class Tab extends Aggregate {
public function serveDrinks($drinksServed)
{
$this->assertDrinksAreOutstanding($drinksServed);
$this->applyAndRecordThat(new DrinksServed(
$this->getAggregateId(),
$drinksServed
));
}
}
106. class Tab extends Aggregate {
public function serveDrinks($drinksServed)
{
$this->assertDrinksAreOutstanding($drinksServed);
$this->applyAndRecordThat(new DrinksServed(
$this->getAggregateId(),
$drinksServed
));
}
}
1 - Comprobamos que el evento se puede aplicar
107. class Tab extends Aggregate {
public function serveDrinks($drinksServed)
{
$this->assertDrinksAreOutstanding($drinksServed);
$this->applyAndRecordThat(new DrinksServed(
$this->getAggregateId(),
$drinksServed
));
}
}
1 - Comprobamos que el evento se puede aplicar
2 - Lo Aplicamos y lo guardamos
118. <?php
class TabOpenedProjection implements Projection
{
private $pdo;
public function __construct($pdo)
{
$this->pdo = $pdo;
}
public function project($event)
{
$stmt = $this->pdo->prepare("INSERT INTO tabs (tab_id, waiter, tableNumber, open) VALUES
(:tab_id, :waiter, :tableNumber, 1)");
$stmt->execute([
':tab_id' => $event->getAggregateId(),
':waiter' => $event->getWaiterId(),
':tableNumber' => $event->getTableNumber(),
]);
}
public function eventType()
{
return TabOpened::class;
}
}
119. <?php
class Projector
{
private $projections = [];
public function register(array $projections)
{
foreach ($projections as $projection) {
$this->projections[$projection->eventType()] = $projection;
}
}
public function project(DomainEvents $events)
{
foreach ($events as $event) {
if (!isset($this->projections[get_class($event)]))
throw new NoProjectionExists();
$this->projections[get_class($event)]->project($event);
}
}
}