Skip to content

Commit

Permalink
ENGCOM-4790: magento/graphql-ce#607: Expanded "createdEmptyCart" muta…
Browse files Browse the repository at this point in the history
…tion with not… #610
  • Loading branch information
naydav authored Apr 19, 2019
2 parents a48a507 + f1ee8ba commit 59f8b32
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 91 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;
}
}
96 changes: 58 additions & 38 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CreateEmptyCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +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 @@ -65,19 +59,45 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
{
$customerId = $context->getUserId();

if (0 !== $customerId && null !== $customerId) {
$quoteId = $this->cartManagement->createEmptyCartForCustomer($customerId);
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quoteId);

if (empty($maskedQuoteId)) {
$quoteIdMask = $this->quoteIdMaskFactory->create();
$quoteIdMask->setQuoteId($quoteId)->save();
$maskedQuoteId = $quoteIdMask->getMaskedId();
}
} else {
$maskedQuoteId = $this->guestCartManagement->createEmptyCart();
$predefinedMaskedQuoteId = null;
if (isset($args['input']['cart_id'])) {
$predefinedMaskedQuoteId = $args['input']['cart_id'];
$this->validateMaskedId($predefinedMaskedQuoteId);
}

$maskedQuoteId = (0 === $customerId || null === $customerId)
? $this->createEmptyCartForGuest->execute($predefinedMaskedQuoteId)
: $this->createEmptyCartForCustomer->execute($customerId, $predefinedMaskedQuoteId);
return $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.'));
}

if ($this->isQuoteWithSuchMaskedIdAlreadyExists($maskedId)) {
throw new GraphQlAlreadyExistsException(__('Cart with ID "%1" already exists.', $maskedId));
}
}

/**
* @param string $maskedId
* @return bool
*/
private function isQuoteWithSuchMaskedIdAlreadyExists(string $maskedId): bool
{
try {
$this->maskedQuoteIdToQuoteId->execute($maskedId);
return true;
} catch (NoSuchEntityException $e) {
return false;
}
}
}
6 changes: 5 additions & 1 deletion app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Query {
}

type Mutation {
createEmptyCart: String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CreateEmptyCart") @doc(description:"Creates an empty shopping cart for a guest or logged in user")
createEmptyCart(input: createEmptyCartInput): String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CreateEmptyCart") @doc(description:"Creates an empty shopping cart for a guest or logged in user")
addSimpleProductsToCart(input: AddSimpleProductsToCartInput): AddSimpleProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddSimpleProductsToCart")
addVirtualProductsToCart(input: AddVirtualProductsToCartInput): AddVirtualProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddSimpleProductsToCart")
applyCouponToCart(input: ApplyCouponToCartInput): ApplyCouponToCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ApplyCouponToCart")
Expand All @@ -21,6 +21,10 @@ type Mutation {
placeOrder(input: PlaceOrderInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\PlaceOrder")
}

input createEmptyCartInput {
cart_id: String
}

input AddSimpleProductsToCartInput {
cart_id: String!
cartItems: [SimpleProductCartItemInput!]!
Expand Down
Loading

0 comments on commit 59f8b32

Please sign in to comment.