Skip to content

Instantly share code, notes, and snippets.

@basz
Created November 18, 2016 09:57
Show Gist options
  • Select an option

  • Save basz/d9406c34cf3e2b046d3fc6400c2d2565 to your computer and use it in GitHub Desktop.

Select an option

Save basz/d9406c34cf3e2b046d3fc6400c2d2565 to your computer and use it in GitHub Desktop.
/**
* Handle command
*
* @param ChangeCustomerProducts $command
*/
public function __invoke(ChangeCustomerProducts $command)
{
$customerId = $command->customerId();
/** @var Customer $customer */
if (!$customer = $this->customerRepository->get($customerId)) {
throw CustomerNotFound::withCustomerId($customerId);
}
$removeFromPractice = function(PracticeId $practiceId, Set $productRemovals) use (PracticeRepository $practiceRepository) {
$practice = $practiceRepository->get($practiceId);
$practice->removeProducts($productRemovals);
}
$customer->changeProducts($command->requestedAdditions(), $command->requestedRemovals(), $removeFromPractice);
}
/**
* @param Set $requestedAdditions
* @param Set $requestedRemovals
* @param callback $removeFromPractice
*/
public function changeProducts(Set $requestedAdditions, Set $requestedRemovals, callback $removeFromPractice)
{
$additions = $this->products->union($requestedAdditions)->diff($this->products);
$removals = $this->products->xor($this->products->diff($requestedRemovals));
if (!$additions->isEmpty() || !$removals->isEmpty()) {
$this->recordThat(CustomerProductsChanged::withChanges($this->customerId, $additions, $removals));
}
foreach($this->practices as $practiceId) {
$removeFromPractice($removeFromPractice, $removals);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment