Skip to content

Commit

Permalink
Changed the code to match the requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
mamazu committed Nov 28, 2018
1 parent bb1dcff commit 5b2710e
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/Handler/CompleteOrderHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sylius\ShopApiPlugin\Command\CompleteOrder;
use Sylius\ShopApiPlugin\Exception\WrongUserException;
use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface;
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
use Webmozart\Assert\Assert;

final class CompleteOrderHandler
Expand Down Expand Up @@ -67,25 +68,32 @@ public function handle(CompleteOrder $completeOrder)

private function getCustomer(string $emailAddress): CustomerInterface
{
/** @var CustomerInterface|null $customer */
$customer = $this->customerRepository->findOneBy(['email' => $emailAddress]);
try {
$loggedInUser = $this->loggedInUserProvider->provide();

if($emailAddress !== ''){
throw new \InvalidArgumentException('Can not have a logged in user and an email address');
}

// If the customer does not exist then it's normal checkout
if ($customer === null) {
/** @var CustomerInterface $customer */
$customer = $this->customerFactory->createNew();
$customer->setEmail($emailAddress);
$customer = $loggedInUser->getCustomer();

return $customer;
}
}catch (TokenNotFoundException $notLoggedIn){

// If the customer does exist the user has to be logged in with this customer. Otherwise the user is not authorized to complete the checkout
$loggedInUser = $this->loggedInUserProvider->provide();
/** @var CustomerInterface|null $customer */
$customer = $this->customerRepository->findOneBy(['email' => $emailAddress]);

if ($loggedInUser->getCustomer() !== $customer) {
throw new WrongUserException();
}
// If the customer does not exist then it's normal checkout
if ($customer === null) {
/** @var CustomerInterface $customer */
$customer = $this->customerFactory->createNew();
$customer->setEmail($emailAddress);

return $customer;
return $customer;
} else {
throw new WrongUserException('Email is already taken');
}
}
}
}

0 comments on commit 5b2710e

Please sign in to comment.