Skip to content

Commit b4461ef

Browse files
author
Valeriy Naida
authored
ENGCOM-2994: Quote masked id creation logic moved to create cart resolver #169
2 parents 2eb6f84 + e0c7383 commit b4461ef

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

app/code/Magento/Quote/Model/MaskedQuoteIdToQuoteId.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Magento\Quote\Api\CartRepositoryInterface;
1111
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResource;
1212

13+
/**
14+
* MaskedQuoteId to QuoteId resolver
15+
*/
1316
class MaskedQuoteIdToQuoteId implements MaskedQuoteIdToQuoteIdInterface
1417
{
1518
/**

app/code/Magento/Quote/Model/QuoteIdToMaskedQuoteId.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,40 @@
88
namespace Magento\Quote\Model;
99

1010
use Magento\Quote\Api\CartRepositoryInterface;
11+
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResource;
1112

13+
/**
14+
* QuoteId to MaskedQuoteId resolver
15+
*/
1216
class QuoteIdToMaskedQuoteId implements QuoteIdToMaskedQuoteIdInterface
1317
{
1418
/**
1519
* @var QuoteIdMaskFactory
1620
*/
1721
private $quoteIdMaskFactory;
18-
1922
/**
2023
* @var CartRepositoryInterface
2124
*/
2225
private $cartRepository;
2326

27+
/**
28+
* @var QuoteIdMaskResource
29+
*/
30+
private $quoteIdMaskResource;
31+
2432
/**
2533
* @param QuoteIdMaskFactory $quoteIdMaskFactory
2634
* @param CartRepositoryInterface $cartRepository
35+
* @param QuoteIdMaskResource $quoteIdMaskResource
2736
*/
2837
public function __construct(
2938
QuoteIdMaskFactory $quoteIdMaskFactory,
30-
CartRepositoryInterface $cartRepository
39+
CartRepositoryInterface $cartRepository,
40+
QuoteIdMaskResource $quoteIdMaskResource
3141
) {
3242
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
3343
$this->cartRepository = $cartRepository;
44+
$this->quoteIdMaskResource = $quoteIdMaskResource;
3445
}
3546

3647
/**
@@ -42,8 +53,9 @@ public function execute(int $quoteId): string
4253
$this->cartRepository->get($quoteId);
4354

4455
$quoteIdMask = $this->quoteIdMaskFactory->create();
45-
$quoteIdMask->setQuoteId($quoteId)->save();
56+
$this->quoteIdMaskResource->load($quoteIdMask, $quoteId, 'quote_id');
57+
$maskedId = $quoteIdMask->getMaskedId() ?? '';
4658

47-
return $quoteIdMask->getMaskedId();
59+
return $maskedId;
4860
}
4961
}

app/code/Magento/QuoteGraphQl/Model/Resolver/Cart/CreateEmptyCart.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Quote\Api\CartManagementInterface;
1515
use Magento\Quote\Api\GuestCartManagementInterface;
1616
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
17+
use Magento\Quote\Model\QuoteIdMaskFactory;
1718

1819
/**
1920
* @inheritdoc
@@ -24,7 +25,6 @@ class CreateEmptyCart implements ResolverInterface
2425
* @var CartManagementInterface
2526
*/
2627
private $cartManagement;
27-
2828
/**
2929
* @var GuestCartManagementInterface
3030
*/
@@ -40,22 +40,30 @@ class CreateEmptyCart implements ResolverInterface
4040
*/
4141
private $userContext;
4242

43+
/**
44+
* @var QuoteIdMaskFactory
45+
*/
46+
private $quoteIdMaskFactory;
47+
4348
/**
4449
* @param CartManagementInterface $cartManagement
4550
* @param GuestCartManagementInterface $guestCartManagement
4651
* @param UserContextInterface $userContext
4752
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedId
53+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
4854
*/
4955
public function __construct(
5056
CartManagementInterface $cartManagement,
5157
GuestCartManagementInterface $guestCartManagement,
5258
UserContextInterface $userContext,
53-
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedId
59+
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedId,
60+
QuoteIdMaskFactory $quoteIdMaskFactory
5461
) {
5562
$this->cartManagement = $cartManagement;
5663
$this->guestCartManagement = $guestCartManagement;
5764
$this->userContext = $userContext;
5865
$this->quoteIdToMaskedId = $quoteIdToMaskedId;
66+
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
5967
}
6068

6169
/**
@@ -67,7 +75,13 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6775

6876
if (0 !== $customerId && null !== $customerId) {
6977
$quoteId = $this->cartManagement->createEmptyCartForCustomer($customerId);
70-
$maskedQuoteId = $this->quoteIdToMaskedId->execute($quoteId);
78+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quoteId);
79+
80+
if (empty($maskedQuoteId)) {
81+
$quoteIdMask = $this->quoteIdMaskFactory->create();
82+
$quoteIdMask->setQuoteId($quoteId)->save();
83+
$maskedQuoteId = $quoteIdMask->getMaskedId();
84+
}
7185
} else {
7286
$maskedQuoteId = $this->guestCartManagement->createEmptyCart();
7387
}

0 commit comments

Comments
 (0)