Skip to content

Instantly share code, notes, and snippets.

@chukShirley
Last active March 20, 2017 17:50
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chukShirley/f907242659db0db618324468ddd445db to your computer and use it in GitHub Desktop.
Repositories
Class CustomerRepository
{
private $customerTable;
private $customerMapper;
public function __construct(CustomerTable $customerTable, CustomerMapper $customerMapper)
{
$this->customerTable = $customerTable;
$this->CustomerMapper = $customerMapper;
}
public function findAllPremiumCustomers()
{
$premiumCustomers = $this->customerTable->select()->where()->etc();
$collection = new PremiumCustomersCollection();
foreach ($premiumCustomers as $premiumCustomer) {
$collection->add($this->customerMapper->hydrate(new PremiumCustomer(), $premiumCustomer));
}
return $collection;
}
public function findAllPremiumCustomerIds()
{
$premiumCustomers = $this->customerTable->select()->where()->etc();
$collection = new PremiumCustomerIdsCollection();
foreach ($premiumCustomers as $premiumCustomer) {
$collection->add(PremiumCustomerId::fromString($premiumCustomer['id']));
}
return $collection;
}
}
class PlaceOrderForPremiumCustomerCommandHandler
{
private $customerRepository;
private $orderRepository;
public function __construct(CustomerRepository $customerRepository, OrderRepository $orderRepository)
{
$this->customerRepository = $customerRepository;
$this->orderRepository = $orderRepository;
}
public function handle(PlaceOrderForPremiumCustomerCommand $command)
{
$validCustomerIds = $this->customerRepository->findAllPremiumCustomerIds();
$factory = new OrderFactory();
$orderAggregate = $factory->create($command->customerId, $command->foo, $command->bar);
// How I would typically do this
$orderAggregate->placeForPremiumCustomer($validCustomerIds);
// How you seem to be suggesting
$orderAggregate->placeForPremiumCustomer($this->customerRepository);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment