Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
GraphQL-607: Expand createEmptyCart mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav committed Apr 19, 2019
1 parent 9009ccc commit a6cb829
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 233 deletions.
5 changes: 4 additions & 1 deletion app/code/Magento/Quote/Model/QuoteIdMask.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ protected function _construct()
* Initialize quote identifier before save
*
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function beforeSave()
{
parent::beforeSave();
$this->setMaskedId($this->randomDataGenerator->getUniqueHash());
if (empty($this->getMaskedId())) {
$this->setMaskedId($this->randomDataGenerator->getUniqueHash());
}
return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResourceModel;

/**
* Create empty cart for customer
*/
class CreateEmptyCartForCustomer
{
/**
* @var CartManagementInterface
*/
private $cartManagement;

/**
* @var QuoteIdMaskFactory
*/
private $quoteIdMaskFactory;

/**
* @var QuoteIdMaskResourceModel
*/
private $quoteIdMaskResourceModel;

/**
* @param CartManagementInterface $cartManagement
* @param QuoteIdMaskFactory $quoteIdMaskFactory
* @param QuoteIdMaskResourceModel $quoteIdMaskResourceModel
*/
public function __construct(
CartManagementInterface $cartManagement,
QuoteIdMaskFactory $quoteIdMaskFactory,
QuoteIdMaskResourceModel $quoteIdMaskResourceModel
) {
$this->cartManagement = $cartManagement;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
$this->quoteIdMaskResourceModel = $quoteIdMaskResourceModel;
}

/**
* @param string|null $predefinedMaskedQuoteId
* @return string
*/
public function execute(int $customerId, string $predefinedMaskedQuoteId = null): string
{
$quoteId = $this->cartManagement->createEmptyCartForCustomer($customerId);

$quoteIdMask = $this->quoteIdMaskFactory->create();
$quoteIdMask->setQuoteId($quoteId);

if (isset($predefinedMaskedQuoteId)) {
$quoteIdMask->setMaskedId($predefinedMaskedQuoteId);
}

$this->quoteIdMaskResourceModel->save($quoteIdMask);
return $quoteIdMask->getMaskedId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Quote\Api\GuestCartManagementInterface;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResourceModel;

/**
* Create empty cart for guest
*/
class CreateEmptyCartForGuest
{
/**
* @var GuestCartManagementInterface
*/
private $guestCartManagement;

/**
* @var QuoteIdMaskFactory
*/
private $quoteIdMaskFactory;

/**
* @var QuoteIdMaskResourceModel
*/
private $quoteIdMaskResourceModel;

/**
* @param GuestCartManagementInterface $guestCartManagement
* @param QuoteIdMaskFactory $quoteIdMaskFactory
* @param QuoteIdMaskResourceModel $quoteIdMaskResourceModel
*/
public function __construct(
GuestCartManagementInterface $guestCartManagement,
QuoteIdMaskFactory $quoteIdMaskFactory,
QuoteIdMaskResourceModel $quoteIdMaskResourceModel
) {
$this->guestCartManagement = $guestCartManagement;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
$this->quoteIdMaskResourceModel = $quoteIdMaskResourceModel;
}

/**
* @param string|null $predefinedMaskedQuoteId
* @return string
*/
public function execute(string $predefinedMaskedQuoteId = null): string
{
$maskedQuoteId = $this->guestCartManagement->createEmptyCart();

if (isset($predefinedMaskedQuoteId)) {
$quoteIdMask = $this->quoteIdMaskFactory->create();
$this->quoteIdMaskResourceModel->load($quoteIdMask, $maskedQuoteId, 'masked_id');

$quoteIdMask->setMaskedId($predefinedMaskedQuoteId);
$this->quoteIdMaskResourceModel->save($quoteIdMask);
}
return $predefinedMaskedQuoteId ?? $maskedQuoteId;
}
}
111 changes: 52 additions & 59 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CreateEmptyCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,49 @@

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Api\GuestCartManagementInterface;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Magento\QuoteGraphQl\Model\Cart\CreateEmptyCartForCustomer;
use Magento\QuoteGraphQl\Model\Cart\CreateEmptyCartForGuest;

/**
* @inheritdoc
*/
class CreateEmptyCart implements ResolverInterface
{
/**
* @var CartManagementInterface
* @var CreateEmptyCartForCustomer
*/
private $cartManagement;
private $createEmptyCartForCustomer;

/**
* @var GuestCartManagementInterface
* @var CreateEmptyCartForGuest
*/
private $guestCartManagement;
private $createEmptyCartForGuest;

/**
* @var QuoteIdToMaskedQuoteIdInterface
* @var MaskedQuoteIdToQuoteIdInterface
*/
private $quoteIdToMaskedId;
private $maskedQuoteIdToQuoteId;

/**
* @var QuoteIdMaskFactory
*/
private $quoteIdMaskFactory;

/**
* @param CartManagementInterface $cartManagement
* @param GuestCartManagementInterface $guestCartManagement
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedId
* @param QuoteIdMaskFactory $quoteIdMaskFactory
* @param CreateEmptyCartForCustomer $createEmptyCartForCustomer
* @param CreateEmptyCartForGuest $createEmptyCartForGuest
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
*/
public function __construct(
CartManagementInterface $cartManagement,
GuestCartManagementInterface $guestCartManagement,
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedId,
QuoteIdMaskFactory $quoteIdMaskFactory
CreateEmptyCartForCustomer $createEmptyCartForCustomer,
CreateEmptyCartForGuest $createEmptyCartForGuest,
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
) {
$this->cartManagement = $cartManagement;
$this->guestCartManagement = $guestCartManagement;
$this->quoteIdToMaskedId = $quoteIdToMaskedId;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
$this->createEmptyCartForCustomer = $createEmptyCartForCustomer;
$this->createEmptyCartForGuest = $createEmptyCartForGuest;
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
}

/**
Expand All @@ -66,45 +58,46 @@ public function __construct(
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$customerId = $context->getUserId();
$quoteIdMask = $this->quoteIdMaskFactory->create();
$maskedQuoteId = null;

$predefinedMaskedQuoteId = null;
if (isset($args['input']['cart_id'])) {
$maskedQuoteId = $args['input']['cart_id'];

if ($quoteIdMask->load($maskedQuoteId, 'masked_id') && $quoteIdMask->getQuoteId()) {
throw new GraphQlAlreadyExistsException(__('Specified parameter "cart_id" is non unique.'));
}

if (mb_strlen($maskedQuoteId) > 32) {
throw new GraphQlInputException(__('"cart_id" length have to be less than or equal to 32.'));
}
$predefinedMaskedQuoteId = $args['input']['cart_id'];
$this->validateMaskedId($predefinedMaskedQuoteId);
}

if (0 !== $customerId && null !== $customerId) {
$quoteId = $this->cartManagement->createEmptyCartForCustomer($customerId);
$existsMaskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quoteId);
$maskedQuoteId = (0 === $customerId || null === $customerId)
? $this->createEmptyCartForGuest->execute($predefinedMaskedQuoteId)
: $this->createEmptyCartForCustomer->execute($customerId, $predefinedMaskedQuoteId);
return $maskedQuoteId;
}

if (empty($existsMaskedQuoteId)) {
if (null !== $maskedQuoteId) {
$quoteIdMask->setMaskedId($maskedQuoteId);
}
/**
* @param string $maskedId
* @throws GraphQlAlreadyExistsException
* @throws GraphQlInputException
*/
private function validateMaskedId(string $maskedId): void
{
if (mb_strlen($maskedId) != 32) {
throw new GraphQlInputException(__('Cart ID length should to be 32 symbols.'));
}

$quoteIdMask->setQuoteId($quoteId)->save();
$maskedQuoteId = $quoteIdMask->getMaskedId();
}
} else {
if (null !== $maskedQuoteId) {
$cartId = $this->cartManagement->createEmptyCart();
$quoteIdMask
->setQuoteId($cartId)
->setMaskedId($maskedQuoteId)
->save();
} else {
$maskedQuoteId = $this->guestCartManagement->createEmptyCart();
}
if ($this->isQuoteWithSuchMaskedIdAlreadyExists($maskedId)) {
throw new GraphQlAlreadyExistsException(__('Cart with ID "%1" already exists.', $maskedId));
}
}

return $maskedQuoteId;
/**
* @param string $maskedId
* @return bool
*/
private function isQuoteWithSuchMaskedIdAlreadyExists(string $maskedId): bool
{
try {
$this->maskedQuoteIdToQuoteId->execute($maskedId);
return true;
} catch (NoSuchEntityException $e) {
return false;
}
}
}
33 changes: 0 additions & 33 deletions app/code/Magento/QuoteGraphQl/Plugin/MaskAlreadySetException.php

This file was deleted.

3 changes: 0 additions & 3 deletions app/code/Magento/QuoteGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,4 @@
type="Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCart"/>
<preference for="Magento\QuoteGraphQl\Model\Cart\SetShippingMethodsOnCartInterface"
type="Magento\QuoteGraphQl\Model\Cart\SetShippingMethodsOnCart"/>
<type name="Magento\Quote\Model\QuoteIdMask">
<plugin name="mask_already_set_exception" type="Magento\QuoteGraphQl\Plugin\MaskAlreadySetException"/>
</type>
</config>
Loading

0 comments on commit a6cb829

Please sign in to comment.