Skip to content

Commit 5c29598

Browse files
author
Valeriy Nayda
committed
Merge remote-tracking branch 'origin/mutations-empty-cart' into GraphQL-165
# Conflicts: # composer.lock
2 parents 9bff89e + 439df24 commit 5c29598

File tree

15 files changed

+496
-0
lines changed

15 files changed

+496
-0
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" />
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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\QuoteGraphQl\Model\Resolver\Cart;
9+
10+
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Query\Resolver\Value;
13+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\Quote\Api\CartManagementInterface;
17+
use Magento\Quote\Api\GuestCartManagementInterface;
18+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
class CreateEmptyCart implements ResolverInterface
24+
{
25+
/**
26+
* @var CartManagementInterface
27+
*/
28+
private $cartManagement;
29+
30+
/**
31+
* @var GuestCartManagementInterface
32+
*/
33+
private $guestCartManagement;
34+
35+
/**
36+
* @var ValueFactory
37+
*/
38+
private $valueFactory;
39+
40+
/**
41+
* @var QuoteIdToMaskedQuoteIdInterface
42+
*/
43+
private $quoteIdToMaskedId;
44+
45+
/**
46+
* @var UserContextInterface
47+
*/
48+
private $userContext;
49+
50+
/**
51+
* @param CartManagementInterface $cartManagement
52+
* @param GuestCartManagementInterface $guestCartManagement
53+
* @param ValueFactory $valueFactory
54+
* @param UserContextInterface $userContext
55+
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedId
56+
*/
57+
public function __construct(
58+
CartManagementInterface $cartManagement,
59+
GuestCartManagementInterface $guestCartManagement,
60+
ValueFactory $valueFactory,
61+
UserContextInterface $userContext,
62+
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedId
63+
) {
64+
$this->cartManagement = $cartManagement;
65+
$this->guestCartManagement = $guestCartManagement;
66+
$this->valueFactory = $valueFactory;
67+
$this->userContext = $userContext;
68+
$this->quoteIdToMaskedId = $quoteIdToMaskedId;
69+
}
70+
71+
/**
72+
* @inheritDoc
73+
*/
74+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value
75+
{
76+
$customerId = $this->userContext->getUserId();
77+
78+
if (null !== $customerId) {
79+
$quoteId = $this->cartManagement->createEmptyCartForCustomer($customerId);
80+
$maskedQuoteId = $this->quoteIdToMaskedId->execute($quoteId);
81+
} else {
82+
$maskedQuoteId = $this->guestCartManagement->createEmptyCart();
83+
}
84+
85+
return $this->valueFactory->create(function () use ($maskedQuoteId) {
86+
return $maskedQuoteId;
87+
});
88+
}
89+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# QuoteGraphQl
2+
3+
**QuoteGraphQl** provides type and resolver information for the GraphQl module
4+
to generate quote (cart) information endpoints. Also provides endpoints for modifying a quote.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "magento/module-quote-graph-ql",
3+
"description": "N/A",
4+
"type": "magento2-module",
5+
"require": {
6+
"php": "~7.1.3||~7.2.0",
7+
"magento/framework": "*",
8+
"magento/module-authorization": "*",
9+
"magento/module-quote": "*"
10+
},
11+
"suggest": {
12+
"magento/module-graph-ql": "*",
13+
"magento/module-catalog-graph-ql": "*"
14+
},
15+
"license": [
16+
"OSL-3.0",
17+
"AFL-3.0"
18+
],
19+
"autoload": {
20+
"files": [
21+
"registration.php"
22+
],
23+
"psr-4": {
24+
"Magento\\QuoteGraphQl\\": ""
25+
}
26+
}
27+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_QuoteGraphQl"/>
10+
</config>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
type Mutation {
5+
createEmptyCart: String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Cart\\CreateEmptyCart") @doc(description:"Creates empty shopping cart for guest or logged in user")
6+
}

0 commit comments

Comments
 (0)