Created
November 18, 2016 09:57
-
-
Save basz/d9406c34cf3e2b046d3fc6400c2d2565 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @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