![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/magento/module-quote/Model/Cart/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ declare(strict_types=1); namespace Magento\Quote\Model\Cart; use Magento\Framework\Exception\AlreadyExistsException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Quote\Api\CartManagementInterface; 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 customer cart or create empty cart. Ensure mask_id is created */ class CustomerCartResolver { /** * @var CartManagementInterface */ private $cartManagement; /** * @var QuoteIdMaskFactory */ private $quoteIdMaskFactory; /** * @var QuoteIdMaskResourceModel */ private $quoteIdMaskResourceModel; /** * @var QuoteIdToMaskedQuoteIdInterface */ private $quoteIdToMaskedQuoteId; /** * @param CartManagementInterface $cartManagement * @param QuoteIdMaskFactory $quoteIdMaskFactory * @param QuoteIdMaskResourceModel $quoteIdMaskResourceModel * @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId */ public function __construct( CartManagementInterface $cartManagement, QuoteIdMaskFactory $quoteIdMaskFactory, QuoteIdMaskResourceModel $quoteIdMaskResourceModel, QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId ) { $this->cartManagement = $cartManagement; $this->quoteIdMaskFactory = $quoteIdMaskFactory; $this->quoteIdMaskResourceModel = $quoteIdMaskResourceModel; $this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId; } /** * Get customer cart by customer id with predefined masked quote id * * @param int $customerId * @param string|null $predefinedMaskedQuoteId * @return Quote * @throws NoSuchEntityException * @throws \Magento\Framework\Exception\CouldNotSaveException */ public function resolve(int $customerId, string $predefinedMaskedQuoteId = null): Quote { try { /** @var Quote $cart */ $cart = $this->cartManagement->getCartForCustomer($customerId); } catch (NoSuchEntityException $e) { $this->cartManagement->createEmptyCartForCustomer($customerId); $cart = $this->cartManagement->getCartForCustomer($customerId); } try { $this->ensureQuoteMaskIdExist((int)$cart->getId(), $predefinedMaskedQuoteId); // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock } catch (AlreadyExistsException $e) { // do nothing, we already have masked id } return $cart; } /** * Create masked id for customer's active quote if it's not exists * * @param int $quoteId * @param string|null $predefinedMaskedQuoteId * @return void * @throws AlreadyExistsException */ private function ensureQuoteMaskIdExist(int $quoteId, string $predefinedMaskedQuoteId = null): void { try { $maskedId = $this->quoteIdToMaskedQuoteId->execute($quoteId); } catch (NoSuchEntityException $e) { $maskedId = ''; } if ($maskedId === '') { $quoteIdMask = $this->quoteIdMaskFactory->create(); $quoteIdMask->setQuoteId($quoteId); if (null !== $predefinedMaskedQuoteId) { $quoteIdMask->setMaskedId($predefinedMaskedQuoteId); } $this->quoteIdMaskResourceModel->save($quoteIdMask); } } }