QuoteRepository.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model;
  7. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Api\SortOrder;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. use Magento\Quote\Api\Data\CartInterface;
  12. use Magento\Quote\Model\Quote;
  13. use Magento\Store\Model\StoreManagerInterface;
  14. use Magento\Framework\Api\Search\FilterGroup;
  15. use Magento\Quote\Model\ResourceModel\Quote\Collection as QuoteCollection;
  16. use Magento\Quote\Model\ResourceModel\Quote\CollectionFactory as QuoteCollectionFactory;
  17. use Magento\Framework\Exception\InputException;
  18. use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
  19. use Magento\Quote\Model\QuoteRepository\SaveHandler;
  20. use Magento\Quote\Model\QuoteRepository\LoadHandler;
  21. /**
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class QuoteRepository implements \Magento\Quote\Api\CartRepositoryInterface
  25. {
  26. /**
  27. * @var Quote[]
  28. */
  29. protected $quotesById = [];
  30. /**
  31. * @var Quote[]
  32. */
  33. protected $quotesByCustomerId = [];
  34. /**
  35. * @var QuoteFactory
  36. */
  37. protected $quoteFactory;
  38. /**
  39. * @var StoreManagerInterface
  40. */
  41. protected $storeManager;
  42. /**
  43. * @var \Magento\Quote\Model\ResourceModel\Quote\Collection
  44. * @deprecated 101.0.0
  45. */
  46. protected $quoteCollection;
  47. /**
  48. * @var \Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory
  49. */
  50. protected $searchResultsDataFactory;
  51. /**
  52. * @var JoinProcessorInterface
  53. */
  54. private $extensionAttributesJoinProcessor;
  55. /**
  56. * @var SaveHandler
  57. */
  58. private $saveHandler;
  59. /**
  60. * @var LoadHandler
  61. */
  62. private $loadHandler;
  63. /**
  64. * @var CollectionProcessorInterface
  65. */
  66. private $collectionProcessor;
  67. /**
  68. * @var \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory
  69. */
  70. private $quoteCollectionFactory;
  71. /**
  72. * Constructor
  73. *
  74. * @param QuoteFactory $quoteFactory
  75. * @param StoreManagerInterface $storeManager
  76. * @param \Magento\Quote\Model\ResourceModel\Quote\Collection $quoteCollection
  77. * @param \Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory $searchResultsDataFactory
  78. * @param JoinProcessorInterface $extensionAttributesJoinProcessor
  79. * @param CollectionProcessorInterface|null $collectionProcessor
  80. * @param \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory|null $quoteCollectionFactory
  81. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  82. */
  83. public function __construct(
  84. QuoteFactory $quoteFactory,
  85. StoreManagerInterface $storeManager,
  86. \Magento\Quote\Model\ResourceModel\Quote\Collection $quoteCollection,
  87. \Magento\Quote\Api\Data\CartSearchResultsInterfaceFactory $searchResultsDataFactory,
  88. JoinProcessorInterface $extensionAttributesJoinProcessor,
  89. CollectionProcessorInterface $collectionProcessor = null,
  90. \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory $quoteCollectionFactory = null
  91. ) {
  92. $this->quoteFactory = $quoteFactory;
  93. $this->storeManager = $storeManager;
  94. $this->searchResultsDataFactory = $searchResultsDataFactory;
  95. $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
  96. $this->collectionProcessor = $collectionProcessor ?: \Magento\Framework\App\ObjectManager::getInstance()
  97. ->get(\Magento\Framework\Api\SearchCriteria\CollectionProcessor::class);
  98. $this->quoteCollectionFactory = $quoteCollectionFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
  99. ->get(\Magento\Quote\Model\ResourceModel\Quote\CollectionFactory::class);
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function get($cartId, array $sharedStoreIds = [])
  105. {
  106. if (!isset($this->quotesById[$cartId])) {
  107. $quote = $this->loadQuote('loadByIdWithoutStore', 'cartId', $cartId, $sharedStoreIds);
  108. $this->getLoadHandler()->load($quote);
  109. $this->quotesById[$cartId] = $quote;
  110. }
  111. return $this->quotesById[$cartId];
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function getForCustomer($customerId, array $sharedStoreIds = [])
  117. {
  118. if (!isset($this->quotesByCustomerId[$customerId])) {
  119. $quote = $this->loadQuote('loadByCustomer', 'customerId', $customerId, $sharedStoreIds);
  120. $this->getLoadHandler()->load($quote);
  121. $this->quotesById[$quote->getId()] = $quote;
  122. $this->quotesByCustomerId[$customerId] = $quote;
  123. }
  124. return $this->quotesByCustomerId[$customerId];
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function getActive($cartId, array $sharedStoreIds = [])
  130. {
  131. $quote = $this->get($cartId, $sharedStoreIds);
  132. if (!$quote->getIsActive()) {
  133. throw NoSuchEntityException::singleField('cartId', $cartId);
  134. }
  135. return $quote;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function getActiveForCustomer($customerId, array $sharedStoreIds = [])
  141. {
  142. $quote = $this->getForCustomer($customerId, $sharedStoreIds);
  143. if (!$quote->getIsActive()) {
  144. throw NoSuchEntityException::singleField('customerId', $customerId);
  145. }
  146. return $quote;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function save(\Magento\Quote\Api\Data\CartInterface $quote)
  152. {
  153. if ($quote->getId()) {
  154. $currentQuote = $this->get($quote->getId(), [$quote->getStoreId()]);
  155. foreach ($currentQuote->getData() as $key => $value) {
  156. if (!$quote->hasData($key)) {
  157. $quote->setData($key, $value);
  158. }
  159. }
  160. }
  161. $this->getSaveHandler()->save($quote);
  162. unset($this->quotesById[$quote->getId()]);
  163. unset($this->quotesByCustomerId[$quote->getCustomerId()]);
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function delete(\Magento\Quote\Api\Data\CartInterface $quote)
  169. {
  170. $quoteId = $quote->getId();
  171. $customerId = $quote->getCustomerId();
  172. $quote->delete();
  173. unset($this->quotesById[$quoteId]);
  174. unset($this->quotesByCustomerId[$customerId]);
  175. }
  176. /**
  177. * Load quote with different methods
  178. *
  179. * @param string $loadMethod
  180. * @param string $loadField
  181. * @param int $identifier
  182. * @param int[] $sharedStoreIds
  183. * @throws NoSuchEntityException
  184. * @return Quote
  185. */
  186. protected function loadQuote($loadMethod, $loadField, $identifier, array $sharedStoreIds = [])
  187. {
  188. /** @var Quote $quote */
  189. $quote = $this->quoteFactory->create();
  190. if ($sharedStoreIds) {
  191. $quote->setSharedStoreIds($sharedStoreIds);
  192. }
  193. $quote->setStoreId($this->storeManager->getStore()->getId())->$loadMethod($identifier);
  194. if (!$quote->getId()) {
  195. throw NoSuchEntityException::singleField($loadField, $identifier);
  196. }
  197. return $quote;
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  203. {
  204. $this->quoteCollection = $this->quoteCollectionFactory->create();
  205. /** @var \Magento\Quote\Api\Data\CartSearchResultsInterface $searchData */
  206. $searchData = $this->searchResultsDataFactory->create();
  207. $searchData->setSearchCriteria($searchCriteria);
  208. $this->collectionProcessor->process($searchCriteria, $this->quoteCollection);
  209. $this->extensionAttributesJoinProcessor->process($this->quoteCollection);
  210. foreach ($this->quoteCollection->getItems() as $quote) {
  211. /** @var CartInterface $quote */
  212. $this->getLoadHandler()->load($quote);
  213. }
  214. $searchData->setItems($this->quoteCollection->getItems());
  215. $searchData->setTotalCount($this->quoteCollection->getSize());
  216. return $searchData;
  217. }
  218. /**
  219. * Adds a specified filter group to the specified quote collection.
  220. *
  221. * @param FilterGroup $filterGroup The filter group.
  222. * @param QuoteCollection $collection The quote collection.
  223. * @return void
  224. * @deprecated 101.0.0
  225. * @throws InputException The specified filter group or quote collection does not exist.
  226. */
  227. protected function addFilterGroupToCollection(FilterGroup $filterGroup, QuoteCollection $collection)
  228. {
  229. $fields = [];
  230. $conditions = [];
  231. foreach ($filterGroup->getFilters() as $filter) {
  232. $fields[] = $filter->getField();
  233. $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
  234. $conditions[] = [$condition => $filter->getValue()];
  235. }
  236. if ($fields) {
  237. $collection->addFieldToFilter($fields, $conditions);
  238. }
  239. }
  240. /**
  241. * Get new SaveHandler dependency for application code.
  242. * @return SaveHandler
  243. * @deprecated 100.1.0
  244. */
  245. private function getSaveHandler()
  246. {
  247. if (!$this->saveHandler) {
  248. $this->saveHandler = ObjectManager::getInstance()->get(SaveHandler::class);
  249. }
  250. return $this->saveHandler;
  251. }
  252. /**
  253. * @return LoadHandler
  254. * @deprecated 100.1.0
  255. */
  256. private function getLoadHandler()
  257. {
  258. if (!$this->loadHandler) {
  259. $this->loadHandler = ObjectManager::getInstance()->get(LoadHandler::class);
  260. }
  261. return $this->loadHandler;
  262. }
  263. }