Skip to content

Commit

Permalink
ENGCOM-4679: Fix #12802 - allow to override preference over CartInter…
Browse files Browse the repository at this point in the history
…face and return correct object from QuoteRepository #22149
  • Loading branch information
sivaschenko authored Apr 28, 2019
2 parents 243aa8b + fd891aa commit bf616d2
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 74 deletions.
90 changes: 54 additions & 36 deletions app/code/Magento/Quote/Model/QuoteRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,33 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Quote\Model;

use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\Framework\Api\Search\FilterGroup;
use Magento\Framework\Api\SearchCriteria\CollectionProcessor;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Api\Search\FilterGroup;
use Magento\Quote\Model\ResourceModel\Quote\Collection as QuoteCollection;
use Magento\Quote\Model\ResourceModel\Quote\CollectionFactory as QuoteCollectionFactory;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\Quote\Api\Data\CartInterfaceFactory;
use Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory;
use Magento\Quote\Model\QuoteRepository\SaveHandler;
use Magento\Quote\Model\QuoteRepository\LoadHandler;
use Magento\Quote\Model\ResourceModel\Quote\Collection as QuoteCollection;
use Magento\Quote\Model\ResourceModel\Quote\CollectionFactory as QuoteCollectionFactory;
use Magento\Store\Model\StoreManagerInterface;

/**
* Quote repository.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class QuoteRepository implements \Magento\Quote\Api\CartRepositoryInterface
class QuoteRepository implements CartRepositoryInterface
{
/**
* @var Quote[]
Expand All @@ -37,6 +43,7 @@ class QuoteRepository implements \Magento\Quote\Api\CartRepositoryInterface

/**
* @var QuoteFactory
* @deprecated
*/
protected $quoteFactory;

Expand All @@ -46,13 +53,13 @@ class QuoteRepository implements \Magento\Quote\Api\CartRepositoryInterface
protected $storeManager;

/**
* @var \Magento\Quote\Model\ResourceModel\Quote\Collection
* @var QuoteCollection
* @deprecated 100.2.0
*/
protected $quoteCollection;

/**
* @var \Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory
* @var CartSearchResultsInterfaceFactory
*/
protected $searchResultsDataFactory;

Expand All @@ -77,43 +84,51 @@ class QuoteRepository implements \Magento\Quote\Api\CartRepositoryInterface
private $collectionProcessor;

/**
* @var \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory
* @var QuoteCollectionFactory
*/
private $quoteCollectionFactory;

/**
* @var CartInterfaceFactory
*/
private $cartFactory;

/**
* Constructor
*
* @param QuoteFactory $quoteFactory
* @param StoreManagerInterface $storeManager
* @param \Magento\Quote\Model\ResourceModel\Quote\Collection $quoteCollection
* @param \Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory $searchResultsDataFactory
* @param QuoteCollection $quoteCollection
* @param CartSearchResultsInterfaceFactory $searchResultsDataFactory
* @param JoinProcessorInterface $extensionAttributesJoinProcessor
* @param CollectionProcessorInterface|null $collectionProcessor
* @param \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory|null $quoteCollectionFactory
* @param QuoteCollectionFactory|null $quoteCollectionFactory
* @param CartInterfaceFactory|null $cartFactory
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
QuoteFactory $quoteFactory,
StoreManagerInterface $storeManager,
\Magento\Quote\Model\ResourceModel\Quote\Collection $quoteCollection,
\Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory $searchResultsDataFactory,
QuoteCollection $quoteCollection,
CartSearchResultsInterfaceFactory $searchResultsDataFactory,
JoinProcessorInterface $extensionAttributesJoinProcessor,
CollectionProcessorInterface $collectionProcessor = null,
\Magento\Quote\Model\ResourceModel\Quote\CollectionFactory $quoteCollectionFactory = null
QuoteCollectionFactory $quoteCollectionFactory = null,
CartInterfaceFactory $cartFactory = null
) {
$this->quoteFactory = $quoteFactory;
$this->storeManager = $storeManager;
$this->searchResultsDataFactory = $searchResultsDataFactory;
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
$this->collectionProcessor = $collectionProcessor ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Api\SearchCriteria\CollectionProcessor::class);
$this->quoteCollectionFactory = $quoteCollectionFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Quote\Model\ResourceModel\Quote\CollectionFactory::class);
$this->collectionProcessor = $collectionProcessor ?: ObjectManager::getInstance()
->get(CollectionProcessor::class);
$this->quoteCollectionFactory = $quoteCollectionFactory ?: ObjectManager::getInstance()
->get(QuoteCollectionFactory::class);
$this->cartFactory = $cartFactory ?: ObjectManager::getInstance()->get(CartInterfaceFactory::class);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function get($cartId, array $sharedStoreIds = [])
{
Expand All @@ -126,7 +141,7 @@ public function get($cartId, array $sharedStoreIds = [])
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getForCustomer($customerId, array $sharedStoreIds = [])
{
Expand All @@ -140,7 +155,7 @@ public function getForCustomer($customerId, array $sharedStoreIds = [])
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getActive($cartId, array $sharedStoreIds = [])
{
Expand All @@ -152,7 +167,7 @@ public function getActive($cartId, array $sharedStoreIds = [])
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getActiveForCustomer($customerId, array $sharedStoreIds = [])
{
Expand All @@ -164,9 +179,9 @@ public function getActiveForCustomer($customerId, array $sharedStoreIds = [])
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function save(\Magento\Quote\Api\Data\CartInterface $quote)
public function save(CartInterface $quote)
{
if ($quote->getId()) {
$currentQuote = $this->get($quote->getId(), [$quote->getStoreId()]);
Expand All @@ -184,9 +199,9 @@ public function save(\Magento\Quote\Api\Data\CartInterface $quote)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function delete(\Magento\Quote\Api\Data\CartInterface $quote)
public function delete(CartInterface $quote)
{
$quoteId = $quote->getId();
$customerId = $quote->getCustomerId();
Expand All @@ -203,13 +218,13 @@ public function delete(\Magento\Quote\Api\Data\CartInterface $quote)
* @param int $identifier
* @param int[] $sharedStoreIds
* @throws NoSuchEntityException
* @return Quote
* @return CartInterface
*/
protected function loadQuote($loadMethod, $loadField, $identifier, array $sharedStoreIds = [])
{
/** @var Quote $quote */
$quote = $this->quoteFactory->create();
if ($sharedStoreIds) {
/** @var CartInterface $quote */
$quote = $this->cartFactory->create();
if ($sharedStoreIds && method_exists($quote, 'setSharedStoreIds')) {
$quote->setSharedStoreIds($sharedStoreIds);
}
$quote->setStoreId($this->storeManager->getStore()->getId())->$loadMethod($identifier);
Expand All @@ -220,9 +235,9 @@ protected function loadQuote($loadMethod, $loadField, $identifier, array $shared
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
public function getList(SearchCriteriaInterface $searchCriteria)
{
$this->quoteCollection = $this->quoteCollectionFactory->create();
/** @var \Magento\Quote\Api\Data\CartSearchResultsInterface $searchData */
Expand Down Expand Up @@ -265,6 +280,7 @@ protected function addFilterGroupToCollection(FilterGroup $filterGroup, QuoteCol

/**
* Get new SaveHandler dependency for application code.
*
* @return SaveHandler
* @deprecated 100.1.0
*/
Expand All @@ -277,6 +293,8 @@ private function getSaveHandler()
}

/**
* Get load handler instance.
*
* @return LoadHandler
* @deprecated 100.1.0
*/
Expand Down
Loading

0 comments on commit bf616d2

Please sign in to comment.