mirror of
https://github.com/bdnugget/magento2.git
synced 2026-11-08 01:37:36 +00:00
112 lines
3.5 KiB
PHP
112 lines
3.5 KiB
PHP
<?php
|
|
/**
|
|
* Copyright © Magento, Inc. All rights reserved.
|
|
* See COPYING.txt for license details.
|
|
*/
|
|
declare(strict_types=1);
|
|
|
|
namespace Magento\QuoteGraphQl\Model\Resolver;
|
|
|
|
use Magento\Framework\Exception\AlreadyExistsException;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Magento\Framework\Exception\NoSuchEntityException;
|
|
use Magento\Framework\GraphQl\Config\Element\Field;
|
|
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
|
|
use Magento\Framework\GraphQl\Query\ResolverInterface;
|
|
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
|
|
use Magento\Quote\Model\Quote;
|
|
use Magento\Quote\Model\QuoteIdMaskFactory;
|
|
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
|
|
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResourceModel;
|
|
|
|
/**
|
|
* Get cart id from the cart
|
|
*/
|
|
class MaskedCartId implements ResolverInterface
|
|
{
|
|
/**
|
|
* @var QuoteIdToMaskedQuoteIdInterface
|
|
*/
|
|
private QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId;
|
|
|
|
/**
|
|
* @var QuoteIdMaskFactory
|
|
*/
|
|
private QuoteIdMaskFactory $quoteIdMaskFactory;
|
|
|
|
/**
|
|
* @var QuoteIdMaskResourceModel
|
|
*/
|
|
private QuoteIdMaskResourceModel $quoteIdMaskResourceModel;
|
|
|
|
/**
|
|
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
|
|
* @param QuoteIdMaskFactory $quoteIdMaskFactory
|
|
* @param QuoteIdMaskResourceModel $quoteIdMaskResourceModel
|
|
*/
|
|
public function __construct(
|
|
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId,
|
|
QuoteIdMaskFactory $quoteIdMaskFactory,
|
|
QuoteIdMaskResourceModel $quoteIdMaskResourceModel
|
|
) {
|
|
$this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId;
|
|
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
|
|
$this->quoteIdMaskResourceModel = $quoteIdMaskResourceModel;
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
|
|
{
|
|
if (!isset($value['model'])) {
|
|
throw new LocalizedException(__('"model" value should be specified'));
|
|
}
|
|
/** @var Quote $cart */
|
|
$cart = $value['model'];
|
|
$cartId = (int) $cart->getId();
|
|
$maskedCartId = $this->getQuoteMaskId($cartId);
|
|
return $maskedCartId;
|
|
}
|
|
|
|
/**
|
|
* Get masked id for cart
|
|
*
|
|
* @param int $quoteId
|
|
* @return string
|
|
* @throws GraphQlNoSuchEntityException
|
|
*/
|
|
private function getQuoteMaskId(int $quoteId): string
|
|
{
|
|
try {
|
|
$maskedId =$this->ensureQuoteMaskExist($quoteId);
|
|
} catch (NoSuchEntityException $exception) {
|
|
throw new GraphQlNoSuchEntityException(__('Current user does not have an active cart.'));
|
|
}
|
|
return $maskedId;
|
|
}
|
|
|
|
/**
|
|
* Create masked id for quote if it's not exists
|
|
*
|
|
* @param int $quoteId
|
|
* @return string
|
|
* @throws AlreadyExistsException
|
|
*/
|
|
private function ensureQuoteMaskExist(int $quoteId): string
|
|
{
|
|
try {
|
|
$maskedId = $this->quoteIdToMaskedQuoteId->execute($quoteId);
|
|
} catch (NoSuchEntityException $e) {
|
|
$maskedId = '';
|
|
}
|
|
if ($maskedId === '') {
|
|
$quoteIdMask = $this->quoteIdMaskFactory->create();
|
|
$quoteIdMask->setQuoteId($quoteId);
|
|
$this->quoteIdMaskResourceModel->save($quoteIdMask);
|
|
$maskedId = $this->quoteIdToMaskedQuoteId->execute($quoteId);
|
|
}
|
|
return $maskedId;
|
|
}
|
|
}
|