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

Empty cart creation added #124

Merged
merged 14 commits into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions app/code/Magento/Quote/Model/MaskedQuoteIdToQuoteId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Quote\Model;

use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResource;

class MaskedQuoteIdToQuoteId implements MaskedQuoteIdToQuoteIdInterface
{
/**
* @var CartRepositoryInterface
*/
private $cartRepository;

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

/**
* @var QuoteIdMaskResource
*/
private $quoteIdMaskResource;

/**
* @param QuoteIdMaskFactory $quoteIdMaskFactory
* @param CartRepositoryInterface $cartRepository
* @param QuoteIdMaskResource $quoteIdMaskResource
*/
public function __construct(
QuoteIdMaskFactory $quoteIdMaskFactory,
CartRepositoryInterface $cartRepository,
QuoteIdMaskResource $quoteIdMaskResource
) {
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
$this->cartRepository = $cartRepository;
$this->quoteIdMaskResource = $quoteIdMaskResource;
}

/**
* @inheritDoc
*/
public function execute(string $maskedQuoteId): int
{
$quoteIdMask = $this->quoteIdMaskFactory->create();
$this->quoteIdMaskResource->load($quoteIdMask, $maskedQuoteId, 'masked_id');

$cart = $this->cartRepository->get($quoteIdMask->getQuoteId());

return (int)$cart->getId();
}
}
24 changes: 24 additions & 0 deletions app/code/Magento/Quote/Model/MaskedQuoteIdToQuoteIdInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Quote\Model;

use Magento\Framework\Exception\NoSuchEntityException;

/**
* Converts masked quote id to the quote id (entity id)
* @api
*/
interface MaskedQuoteIdToQuoteIdInterface
{
/**
* @param string $maskedQuoteId
* @return int
* @throws NoSuchEntityException
*/
public function execute(string $maskedQuoteId): int;
}
49 changes: 49 additions & 0 deletions app/code/Magento/Quote/Model/QuoteIdToMaskedQuoteId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Quote\Model;

use Magento\Quote\Api\CartRepositoryInterface;

class QuoteIdToMaskedQuoteId implements QuoteIdToMaskedQuoteIdInterface
{
/**
* @var QuoteIdMaskFactory
*/
private $quoteIdMaskFactory;

/**
* @var CartRepositoryInterface
*/
private $cartRepository;

/**
* @param QuoteIdMaskFactory $quoteIdMaskFactory
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(
QuoteIdMaskFactory $quoteIdMaskFactory,
CartRepositoryInterface $cartRepository
) {
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
$this->cartRepository = $cartRepository;
}

/**
* @inheritDoc
*/
public function execute(int $quoteId): string
{
/* Check the quote exists to avoid database constraint issues */
$this->cartRepository->get($quoteId);

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

return $quoteIdMask->getMaskedId();
}
}
24 changes: 24 additions & 0 deletions app/code/Magento/Quote/Model/QuoteIdToMaskedQuoteIdInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Quote\Model;

use Magento\Framework\Exception\NoSuchEntityException;

/**
* Converts quote id to the masked quote id
* @api
*/
interface QuoteIdToMaskedQuoteIdInterface
{
/**
* @param int $quoteId
* @return string
* @throws NoSuchEntityException
*/
public function execute(int $quoteId): string;
}
2 changes: 2 additions & 0 deletions app/code/Magento/Quote/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<preference for="Magento\Quote\Api\Data\ShippingMethodInterface" type="Magento\Quote\Model\Cart\ShippingMethod" />
<preference for="Magento\Quote\Api\BillingAddressManagementInterface" type="Magento\Quote\Model\BillingAddressManagement" />
<preference for="Magento\Quote\Model\ShippingAddressManagementInterface" type="Magento\Quote\Model\ShippingAddressManagement" />
<preference for="Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface" type="Magento\Quote\Model\MaskedQuoteIdToQuoteId" />
<preference for="Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface" type="Magento\Quote\Model\QuoteIdToMaskedQuoteId" />
<preference for="Magento\Quote\Api\Data\AddressInterface" type="Magento\Quote\Model\Quote\Address" />
<preference for="Magento\Quote\Api\Data\CartItemInterface" type="Magento\Quote\Model\Quote\Item" />
<preference for="Magento\Quote\Api\Data\CartInterface" type="Magento\Quote\Model\Quote" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver\Cart;

use Magento\Authorization\Model\UserContextInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
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;

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

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

/**
* @var ValueFactory
*/
private $valueFactory;

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

/**
* @var UserContextInterface
*/
private $userContext;

/**
* @param CartManagementInterface $cartManagement
* @param GuestCartManagementInterface $guestCartManagement
* @param ValueFactory $valueFactory
* @param UserContextInterface $userContext
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedId
*/
public function __construct(
CartManagementInterface $cartManagement,
GuestCartManagementInterface $guestCartManagement,
ValueFactory $valueFactory,
UserContextInterface $userContext,
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedId
) {
$this->cartManagement = $cartManagement;
$this->guestCartManagement = $guestCartManagement;
$this->valueFactory = $valueFactory;
$this->userContext = $userContext;
$this->quoteIdToMaskedId = $quoteIdToMaskedId;
}

/**
* @inheritDoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value
{
$customerId = $this->userContext->getUserId();

if (null !== $customerId) {
$quoteId = $this->cartManagement->createEmptyCartForCustomer($customerId);
$maskedQuoteId = $this->quoteIdToMaskedId->execute($quoteId);
} else {
$maskedQuoteId = $this->guestCartManagement->createEmptyCart();
}

return $this->valueFactory->create(function () use ($maskedQuoteId) {
return $maskedQuoteId;
});
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/QuoteGraphQl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# QuoteGraphQl

**QuoteGraphQl** provides type and resolver information for the GraphQl module
to generate quote (cart) information endpoints. Also provides endpoints for modifying a quote.
27 changes: 27 additions & 0 deletions app/code/Magento/QuoteGraphQl/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "magento/module-quote-graph-ql",
"description": "N/A",
"type": "magento2-module",
"require": {
"php": "~7.1.3||~7.2.0",
"magento/framework": "*",
"magento/module-authorization": "*",
"magento/module-quote": "*"
},
"suggest": {
"magento/module-graph-ql": "*",
"magento/module-catalog-graph-ql": "*"
},
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\QuoteGraphQl\\": ""
}
}
}
10 changes: 10 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_QuoteGraphQl"/>
</config>
6 changes: 6 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

type Mutation {
createEmptyCart: String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Cart\\CreateEmptyCart") @doc(description:"Creates empty shopping cart for guest or logged in user")
}
10 changes: 10 additions & 0 deletions app/code/Magento/QuoteGraphQl/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_QuoteGraphQl', __DIR__);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
"magento/module-product-video": "*",
"magento/module-quote": "*",
"magento/module-quote-analytics": "*",
"magento/module-quote-graph-ql": "*",
"magento/module-release-notification": "*",
"magento/module-reports": "*",
"magento/module-require-js": "*",
Expand Down
Loading