Skip to content

Instantly share code, notes, and snippets.

@Ocramius
Forked from chukShirley/CustomerRepository.php
Last active June 24, 2016 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ocramius/421807420c48835801dfba8e4cbbc208 to your computer and use it in GitHub Desktop.
Save Ocramius/421807420c48835801dfba8e4cbbc208 to your computer and use it in GitHub Desktop.
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