Skip to content

Commit

Permalink
refactor #490 [Checkout] Use new cart assignment logic in checkout (l…
Browse files Browse the repository at this point in the history
…chrusciel)

This PR was merged into the 1.0-dev branch.

Discussion
----------



Commits
-------

435f6a0 [Checkout] Use new cart assignment logic in checkout
  • Loading branch information
GSadee authored Jul 25, 2019
2 parents 94d3ceb + 435f6a0 commit 192f633
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 28 deletions.
35 changes: 9 additions & 26 deletions src/Controller/Checkout/CompleteOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Sylius\ShopApiPlugin\Command\Cart\AssignCustomerToCart;
use Sylius\ShopApiPlugin\Command\Cart\CompleteOrder;
use Sylius\ShopApiPlugin\Exception\WrongUserException;
use Sylius\ShopApiPlugin\Provider\LoggedInShopUserProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
Expand All @@ -23,39 +22,23 @@ final class CompleteOrderAction
/** @var MessageBusInterface */
private $bus;

/** @var LoggedInShopUserProviderInterface */
private $loggedInUserProvider;

public function __construct(
ViewHandlerInterface $viewHandler,
MessageBusInterface $bus,
LoggedInShopUserProviderInterface $loggedInUserProvider
) {
public function __construct(ViewHandlerInterface $viewHandler, MessageBusInterface $bus)
{
$this->viewHandler = $viewHandler;
$this->bus = $bus;
$this->loggedInUserProvider = $loggedInUserProvider;
}

public function __invoke(Request $request): Response
{
if ($this->loggedInUserProvider->isUserLoggedIn()) {
$defaultEmail = $this->loggedInUserProvider->provide()->getEmail();
}

try {
$this->bus->dispatch(
new AssignCustomerToCart(
$request->attributes->get('token'),
$request->request->get('email', $defaultEmail ?? null)
)
);
$orderToken = $request->attributes->get('token');
$email = $request->request->get('email');

if (null !== $email) {
$this->bus->dispatch(new AssignCustomerToCart($orderToken, $email));
}

$this->bus->dispatch(
new CompleteOrder(
$request->attributes->get('token'),
$request->request->get('notes')
)
);
$this->bus->dispatch(new CompleteOrder($orderToken, $request->request->get('notes')));
} catch (HandlerFailedException $exception) {
$previousException = $exception->getPrevious();

Expand Down
1 change: 0 additions & 1 deletion src/Resources/config/services/actions/checkout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
>
<argument type="service" id="fos_rest.view_handler" />
<argument type="service" id="sylius_shop_api_plugin.command_bus" />
<argument type="service" id="sylius.shop_api_plugin.provider.current_user_provider" />
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion tests/Controller/Checkout/CheckoutCompleteOrderApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function it_allows_to_complete_checkout_without_email_for_logged_in_custo
$bus->dispatch(new ChooseShippingMethod($token, 0, 'DHL'));
$bus->dispatch(new ChoosePaymentMethod($token, 0, 'PBC'));

$this->logInUser('oliver@queen.com', '123password');
$this->logInUserWithCart('oliver@queen.com', '123password', $token);

$response = $this->complete($token);
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);
Expand Down

0 comments on commit 192f633

Please sign in to comment.