Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Repositories
<?php
interface CustomerRepository
{
public function isPremiumCustomer(CustomerId $customerId) : bool;
// ... more API (fetch collections, filter, etc):
public function findAllPremiumCustomer() : PremiumCustomersList;
public function findAllPremiumCustomerIds() : PremiumCustomersIdList;
}
<?php
// this is your aggregate
class Order
{
// ...
public static function placeForPremiumCustomer(
CustomerRepository $existingCustomers,
CustomerId $customerId,
$foo,
$bar
) : self {
if (! $existingCustomers->isPremiumCustomer($customerId)) {
throw CustomerIsNotAPreviousCustomer::fromCustomerId($customerId);
}
return new self($customerId, $foo, $bar);
}
// ...
}
<?php
class PlaceOrderForPremiumCustomerCommandHandler
{
private $customers;
private $oders;
public function __construct(CustomerRepository $customers, OrderRepository $orders)
{
$this->customers = $customers;
$this->orders = $orders;
}
public function handle(PlaceOrderForPremiumCustomerCommand $command)
{
$this->orders->store(Order::placeForPremiumCustomer(
$this->customers,
$command->customerId,
$command->foo,
$command->bar
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment