Skip to content

Commit 3176de0

Browse files
committed
Introduced API for working with quote masked id
1 parent 8fe1e21 commit 3176de0

File tree

8 files changed

+269
-15
lines changed

8 files changed

+269
-15
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model;
9+
10+
use Magento\Quote\Api\CartRepositoryInterface;
11+
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResource;
12+
13+
class MaskedQuoteIdToQuoteId implements MaskedQuoteIdToQuoteIdInterface
14+
{
15+
/**
16+
* @var CartRepositoryInterface
17+
*/
18+
private $cartRepository;
19+
20+
/**
21+
* @var QuoteIdMaskFactory
22+
*/
23+
private $quoteIdMaskFactory;
24+
25+
/**
26+
* @var QuoteIdMaskResource
27+
*/
28+
private $quoteIdMaskResource;
29+
30+
/**
31+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
32+
* @param CartRepositoryInterface $cartRepository
33+
* @param QuoteIdMaskResource $quoteIdMaskResource
34+
*/
35+
public function __construct(
36+
QuoteIdMaskFactory $quoteIdMaskFactory,
37+
CartRepositoryInterface $cartRepository,
38+
QuoteIdMaskResource $quoteIdMaskResource
39+
) {
40+
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
41+
$this->cartRepository = $cartRepository;
42+
$this->quoteIdMaskResource = $quoteIdMaskResource;
43+
}
44+
45+
/**
46+
* {@inheritDoc}
47+
*/
48+
public function execute(string $maskedQuoteId): int
49+
{
50+
$quoteIdMask = $this->quoteIdMaskFactory->create();
51+
$this->quoteIdMaskResource->load($quoteIdMask, $maskedQuoteId, 'masked_id');
52+
53+
$cart = $this->cartRepository->get($quoteIdMask->getQuoteId());
54+
55+
return (int) $cart->getId();
56+
}
57+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
12+
/**
13+
* Converts masked quote id to the quote id (entity id)
14+
* @api
15+
*/
16+
interface MaskedQuoteIdToQuoteIdInterface
17+
{
18+
/**
19+
* @param string $maskedQuoteId
20+
* @return int
21+
* @throws NoSuchEntityException
22+
*/
23+
public function execute(string $maskedQuoteId): int;
24+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model;
9+
10+
use Magento\Quote\Api\CartRepositoryInterface;
11+
12+
class QuoteIdToMaskedQuoteId implements QuoteIdToMaskedQuoteIdInterface
13+
{
14+
/**
15+
* @var QuoteIdMaskFactory
16+
*/
17+
private $quoteIdMaskFactory;
18+
19+
/**
20+
* @var CartRepositoryInterface
21+
*/
22+
private $cartRepository;
23+
24+
/**
25+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
26+
* @param CartRepositoryInterface $cartRepository
27+
*/
28+
public function __construct(
29+
QuoteIdMaskFactory $quoteIdMaskFactory,
30+
CartRepositoryInterface $cartRepository
31+
) {
32+
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
33+
$this->cartRepository = $cartRepository;
34+
}
35+
36+
/**
37+
* {@inheritDoc}
38+
*/
39+
public function execute(int $quoteId): string
40+
{
41+
/* Check the quote exists to avoid database constraint issues */
42+
$this->cartRepository->get($quoteId);
43+
44+
$quoteIdMask = $this->quoteIdMaskFactory->create();
45+
$quoteIdMask->setQuoteId($quoteId)->save();
46+
47+
return $quoteIdMask->getMaskedId();
48+
}
49+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
12+
/**
13+
* Converts quote id to the masked quote id
14+
* @api
15+
*/
16+
interface QuoteIdToMaskedQuoteIdInterface
17+
{
18+
/**
19+
* @param int $quoteId
20+
* @return string
21+
* @throws NoSuchEntityException
22+
*/
23+
public function execute(int $quoteId): string;
24+
}

app/code/Magento/Quote/etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<preference for="Magento\Quote\Api\Data\ShippingMethodInterface" type="Magento\Quote\Model\Cart\ShippingMethod" />
1212
<preference for="Magento\Quote\Api\BillingAddressManagementInterface" type="Magento\Quote\Model\BillingAddressManagement" />
1313
<preference for="Magento\Quote\Model\ShippingAddressManagementInterface" type="Magento\Quote\Model\ShippingAddressManagement" />
14+
<preference for="Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface" type="Magento\Quote\Model\MaskedQuoteIdToQuoteId" />
15+
<preference for="Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface" type="Magento\Quote\Model\QuoteIdToMaskedQuoteId" />
1416
<preference for="Magento\Quote\Api\Data\AddressInterface" type="Magento\Quote\Model\Quote\Address" />
1517
<preference for="Magento\Quote\Api\Data\CartItemInterface" type="Magento\Quote\Model\Quote\Item" />
1618
<preference for="Magento\Quote\Api\Data\CartInterface" type="Magento\Quote\Model\Quote" />

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1616
use Magento\Quote\Api\CartManagementInterface;
1717
use Magento\Quote\Api\GuestCartManagementInterface;
18-
use Magento\Quote\Model\QuoteIdMask;
19-
use Magento\Quote\Model\QuoteIdMaskFactory;
18+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
2019

2120
/**
2221
* {@inheritdoc}
@@ -39,9 +38,9 @@ class CreateEmptyCart implements ResolverInterface
3938
private $valueFactory;
4039

4140
/**
42-
* @var QuoteIdMaskFactory
41+
* @var QuoteIdToMaskedQuoteIdInterface
4342
*/
44-
private $quoteIdMaskFactory;
43+
private $quoteIdToMaskedId;
4544

4645
/**
4746
* @var UserContextInterface
@@ -53,20 +52,20 @@ class CreateEmptyCart implements ResolverInterface
5352
* @param GuestCartManagementInterface $guestCartManagement
5453
* @param ValueFactory $valueFactory
5554
* @param UserContextInterface $userContext
56-
* @param QuoteIdMaskFactory $quoteIdMaskFactory
55+
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedId
5756
*/
5857
public function __construct(
5958
CartManagementInterface $cartManagement,
6059
GuestCartManagementInterface $guestCartManagement,
6160
ValueFactory $valueFactory,
6261
UserContextInterface $userContext,
63-
QuoteIdMaskFactory $quoteIdMaskFactory
62+
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedId
6463
) {
6564
$this->cartManagement = $cartManagement;
6665
$this->guestCartManagement = $guestCartManagement;
6766
$this->valueFactory = $valueFactory;
6867
$this->userContext = $userContext;
69-
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
68+
$this->quoteIdToMaskedId = $quoteIdToMaskedId;
7069
}
7170

7271
/**
@@ -77,17 +76,14 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7776
$customerId = $this->userContext->getUserId();
7877

7978
if ($customerId) {
80-
$cartId = $this->cartManagement->createEmptyCartForCustomer($customerId);
81-
/** @var QuoteIdMask $quoteIdMask */
82-
$quoteIdMask = $this->quoteIdMaskFactory->create();
83-
$quoteIdMask->setQuoteId($cartId)->save();
84-
$cartId = $quoteIdMask->getMaskedId();
79+
$quoteId = $this->cartManagement->createEmptyCartForCustomer($customerId);
80+
$maskedQuoteId = $this->quoteIdToMaskedId->execute($quoteId);
8581
} else {
86-
$cartId = $this->guestCartManagement->createEmptyCart();
82+
$maskedQuoteId = $this->guestCartManagement->createEmptyCart();
8783
}
8884

89-
$result = function () use ($cartId) {
90-
return $cartId;
85+
$result = function () use ($maskedQuoteId) {
86+
return $maskedQuoteId;
9187
};
9288

9389
return $this->valueFactory->create($result);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model;
9+
10+
use Magento\TestFramework\Helper\Bootstrap as BootstrapHelper;
11+
use Magento\Quote\Api\GuestCartManagementInterface;
12+
13+
class MaskedQuoteIdToQuoteIdTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* @var MaskedQuoteIdToQuoteIdInterface
17+
*/
18+
private $maskedQuoteIdToQuoteId;
19+
20+
/**
21+
* @var GuestCartManagementInterface
22+
*/
23+
private $guestCartManagement;
24+
25+
protected function setUp()
26+
{
27+
$objectManager = BootstrapHelper::getObjectManager();
28+
$this->maskedQuoteIdToQuoteId = $objectManager->create(MaskedQuoteIdToQuoteIdInterface::class);
29+
$this->guestCartManagement = $objectManager->create(GuestCartManagementInterface::class);
30+
}
31+
32+
public function testMaskedIdToQuoteId()
33+
{
34+
$maskedQuoteId = $this->guestCartManagement->createEmptyCart();
35+
$quoteId = $this->maskedQuoteIdToQuoteId->execute($maskedQuoteId);
36+
37+
self::assertGreaterThan(0, $quoteId);
38+
}
39+
40+
public function testMaskedQuoteIdToQuoteIdForNonExistentQuote()
41+
{
42+
self::expectException('Magento\Framework\Exception\NoSuchEntityException');
43+
44+
$this->maskedQuoteIdToQuoteId->execute('test');
45+
}
46+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model;
9+
10+
use Magento\TestFramework\Helper\Bootstrap as BootstrapHelper;
11+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
12+
13+
class QuoteIdToMaskedQuoteIdTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* @var QuoteResource
17+
*/
18+
private $quoteResource;
19+
20+
/**
21+
* @var QuoteFactory
22+
*/
23+
private $quoteFactory;
24+
25+
/**
26+
* @var QuoteIdToMaskedQuoteIdInterface
27+
*/
28+
private $quoteIdToMaskedQuoteId;
29+
30+
protected function setUp()
31+
{
32+
$objectManager = BootstrapHelper::getObjectManager();
33+
$this->quoteIdToMaskedQuoteId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class);
34+
$this->quoteFactory = $objectManager->create(QuoteFactory::class);
35+
$this->quoteResource = $objectManager->create(QuoteResource::class);
36+
}
37+
38+
/**
39+
* @magentoDataFixture Magento/Sales/_files/quote.php
40+
*/
41+
public function testMaskedQuoteId()
42+
{
43+
$quote = $this->quoteFactory->create();
44+
$this->quoteResource->load($quote, 'test01', 'reserved_order_id');
45+
$maskedQuoteId = $this->quoteIdToMaskedQuoteId->execute((int) $quote->getId());
46+
47+
self::assertNotEmpty($maskedQuoteId);
48+
}
49+
50+
public function testMaskedQuoteIdWithNonExistentQuoteId()
51+
{
52+
self::expectException('Magento\Framework\Exception\NoSuchEntityException');
53+
54+
$this->quoteIdToMaskedQuoteId->execute(0);
55+
}
56+
}

0 commit comments

Comments
 (0)