private function whenProductWasAddedToBasket(ProductWasAddedToBasket $event) { // Remember that all of this code used to be in `addProduct()` $productId = $event->getProductId(); if (!$this->productIsInBasket($productId)) { $this->products[(string) $productId] = 0; } ++$this->products[(string) $productId]; ++$this->productCount; }
// ... and in the name. We'll get to that. public function getProductName() { return $this->productName; } } // Notice that Domain Events are **immutable**. Once they have been initialized, there's no way to change them, there // are No setters. That makes perfect sense: History can't be altered! final class ProductWasRemovedFromBasket implements DomainEvent { private $basketId; private $productId; public function __construct(BasketId $basketId, ProductId $productId) { $this->basketId = $basketId; $this->productId = $productId; } public function getAggregateId() { return $this->basketId; } public function getProductId() { return $this->productId; } } // Let's test one of our Domain Events. Did we mention this documentation doubles as test suite? $event = new ProductWasAddedToBasket(new BasketId('BAS1'), new ProductId('PRO1'), "The Princess Bride"); it('should equal another instance with the same value', $event->getAggregateId()->equals(new BasketId('BAS1'))); it("should expose a ProductId", $event->getProductId()->equals(new ProductId('PRO1'))); it("should expose a productName", $event->getProductName() == "The Princess Bride");