Skip to content

Commit

Permalink
Refactored to use the user provider
Browse files Browse the repository at this point in the history
  • Loading branch information
mamazu authored and lchrusciel committed Jan 4, 2019
1 parent 836982c commit 069e714
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 2 additions & 0 deletions doc/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ paths:
description: "Invalid input, validation failed."
schema:
$ref: "#/definitions/GeneralError"
403:
description: "Not logged in or wrong email"
/taxon-products-by-slug/{slug}:
get:
tags:
Expand Down
26 changes: 13 additions & 13 deletions src/Controller/Checkout/CompleteOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use League\Tactician\CommandBus;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\ShopApiPlugin\Command\CompleteOrder;
use Sylius\ShopApiPlugin\Exception\NotLoggedInException;
use Sylius\ShopApiPlugin\Provider\LoggedInUserProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;

final class CompleteOrderAction
Expand All @@ -23,14 +22,17 @@ final class CompleteOrderAction
/** @var CommandBus */
private $bus;

/** @var TokenStorageInterface */
private $tokenStorage;
/** @var LoggedInUserProviderInterface */
private $loggedInUserProvider;

public function __construct(ViewHandlerInterface $viewHandler, CommandBus $bus, TokenStorageInterface $tokenStorage)
{
public function __construct(
ViewHandlerInterface $viewHandler,
CommandBus $bus,
LoggedInUserProviderInterface $loggedInUserProvider
) {
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->tokenStorage = $tokenStorage;
$this->loggedInUserProvider = $loggedInUserProvider;
}

public function __invoke(Request $request): Response
Expand Down Expand Up @@ -66,12 +68,10 @@ public function __invoke(Request $request): Response

private function provideUserEmail(Request $request): string
{
$user = $this->tokenStorage->getToken()->getUser();

if ($user instanceof ShopUserInterface) {
return $user->getCustomer()->getEmail();
try {
return $this->loggedInUserProvider->provide()->getEmail();
} catch (TokenNotFoundException $tokenNotFoundException) {
return $request->request->get('email');
}

return $request->request->get('email');
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/services/actions/checkout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
>
<argument type="service" id="fos_rest.view_handler" />
<argument type="service" id="tactician.commandbus" />
<argument type="service" id="security.token_storage" />
<argument type="service" id="sylius.shop_api_plugin.provider.current_user_provider" />
</service>
</services>
</container>

0 comments on commit 069e714

Please sign in to comment.