Repository.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order\Payment\Transaction;
  7. use Magento\Framework\Api\FilterBuilder;
  8. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  9. use Magento\Framework\Api\SearchCriteriaBuilder;
  10. use Magento\Framework\Api\SortOrderBuilder;
  11. use Magento\Framework\Data\Collection;
  12. use Magento\Framework\Exception\NoSuchEntityException;
  13. use Magento\Sales\Api\Data\TransactionInterface;
  14. use Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory as SearchResultFactory;
  15. use Magento\Sales\Api\TransactionRepositoryInterface;
  16. use Magento\Sales\Model\EntityStorage;
  17. use Magento\Sales\Model\EntityStorageFactory;
  18. use Magento\Sales\Model\ResourceModel\Metadata;
  19. use Magento\Sales\Model\ResourceModel\Order\Payment\Transaction as TransactionResource;
  20. /**
  21. * Repository class for \Magento\Sales\Model\Order\Payment\Transaction
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class Repository implements TransactionRepositoryInterface
  25. {
  26. /**
  27. * Collection Result Factory
  28. *
  29. * @var SearchResultFactory
  30. */
  31. private $searchResultFactory = null;
  32. /**
  33. * @var \Magento\Framework\Api\FilterBuilder
  34. */
  35. private $filterBuilder;
  36. /**
  37. * @var \Magento\Framework\Api\SearchCriteriaBuilder
  38. */
  39. private $searchCriteriaBuilder;
  40. /**
  41. * @var Metadata
  42. */
  43. private $metaData;
  44. /**
  45. * @var SortOrderBuilder
  46. */
  47. private $sortOrderBuilder;
  48. /**
  49. * @var EntityStorage
  50. */
  51. private $entityStorage;
  52. /**
  53. * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface
  54. */
  55. private $collectionProcessor;
  56. /**
  57. * Repository constructor.
  58. * @param SearchResultFactory $searchResultFactory
  59. * @param FilterBuilder $filterBuilder
  60. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  61. * @param SortOrderBuilder $sortOrderBuilder
  62. * @param Metadata $metaData
  63. * @param EntityStorageFactory $entityStorageFactory
  64. * @param CollectionProcessorInterface|null $collectionProcessor
  65. */
  66. public function __construct(
  67. SearchResultFactory $searchResultFactory,
  68. FilterBuilder $filterBuilder,
  69. SearchCriteriaBuilder $searchCriteriaBuilder,
  70. SortOrderBuilder $sortOrderBuilder,
  71. Metadata $metaData,
  72. EntityStorageFactory $entityStorageFactory,
  73. CollectionProcessorInterface $collectionProcessor = null
  74. ) {
  75. $this->searchResultFactory = $searchResultFactory;
  76. $this->filterBuilder = $filterBuilder;
  77. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  78. $this->sortOrderBuilder = $sortOrderBuilder;
  79. $this->metaData = $metaData;
  80. $this->entityStorage = $entityStorageFactory->create();
  81. $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
  82. }
  83. /**
  84. * @inheritdoc
  85. */
  86. public function get($id)
  87. {
  88. if (!$id) {
  89. throw new \Magento\Framework\Exception\InputException(__('An ID is needed. Set the ID and try again.'));
  90. }
  91. if (!$this->entityStorage->has($id)) {
  92. $entity = $this->metaData->getNewInstance()->load($id);
  93. if (!$entity->getTransactionId()) {
  94. throw new NoSuchEntityException(
  95. __("The entity that was requested doesn't exist. Verify the entity and try again.")
  96. );
  97. }
  98. $this->entityStorage->add($entity);
  99. }
  100. return $this->entityStorage->get($id);
  101. }
  102. /**
  103. * @param int $transactionType
  104. * @param int $paymentId
  105. * @return bool|\Magento\Framework\Model\AbstractModel|mixed
  106. * @throws \Magento\Framework\Exception\InputException
  107. */
  108. public function getByTransactionType($transactionType, $paymentId)
  109. {
  110. $identityFieldsForCache = [$transactionType, $paymentId];
  111. $cacheStorage = 'txn_type';
  112. $entity = $this->entityStorage->getByIdentifyingFields($identityFieldsForCache, $cacheStorage);
  113. if (!$entity) {
  114. $typeFilter = $this->filterBuilder
  115. ->setField(TransactionInterface::TXN_TYPE)
  116. ->setValue($transactionType)
  117. ->create();
  118. $idFilter = $this->filterBuilder
  119. ->setField(TransactionInterface::PAYMENT_ID)
  120. ->setValue($paymentId)
  121. ->create();
  122. $transactionIdSort = $this->sortOrderBuilder
  123. ->setField('transaction_id')
  124. ->setDirection(Collection::SORT_ORDER_DESC)
  125. ->create();
  126. $createdAtSort = $this->sortOrderBuilder
  127. ->setField('created_at')
  128. ->setDirection(Collection::SORT_ORDER_DESC)
  129. ->create();
  130. $entity = current(
  131. $this->getList(
  132. $this->searchCriteriaBuilder
  133. ->addFilters([$typeFilter])
  134. ->addFilters([$idFilter])
  135. ->addSortOrder($transactionIdSort)
  136. ->addSortOrder($createdAtSort)
  137. ->create()
  138. )->getItems()
  139. );
  140. if ($entity) {
  141. $this->entityStorage->addByIdentifyingFields($entity, $identityFieldsForCache, $cacheStorage);
  142. }
  143. }
  144. return $entity;
  145. }
  146. /**
  147. * @param int $transactionId
  148. * @param int $paymentId
  149. * @param int $orderId
  150. * @return bool|\Magento\Framework\Api\ExtensibleDataInterface|\Magento\Framework\Model\AbstractModel
  151. * @throws \Magento\Framework\Exception\InputException
  152. */
  153. public function getByTransactionId($transactionId, $paymentId, $orderId)
  154. {
  155. $identityFieldsForCache = [$transactionId, $paymentId, $orderId];
  156. $cacheStorage = 'txn_id';
  157. $entity = $this->entityStorage->getByIdentifyingFields($identityFieldsForCache, $cacheStorage);
  158. if (!$entity) {
  159. $entity = $this->metaData->getNewInstance();
  160. $this->metaData->getMapper()->loadObjectByTxnId(
  161. $entity,
  162. $orderId,
  163. $paymentId,
  164. $transactionId
  165. );
  166. if ($entity && $entity->getId()) {
  167. $this->entityStorage->addByIdentifyingFields($entity, $identityFieldsForCache, $cacheStorage);
  168. return $entity;
  169. }
  170. return false;
  171. }
  172. return $entity;
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  178. {
  179. /** @var TransactionResource\Collection $collection */
  180. $collection = $this->searchResultFactory->create();
  181. $collection->setSearchCriteria($searchCriteria);
  182. $this->collectionProcessor->process($searchCriteria, $collection);
  183. $collection->addPaymentInformation(['method']);
  184. $collection->addOrderInformation(['increment_id']);
  185. return $collection;
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function delete(\Magento\Sales\Api\Data\TransactionInterface $entity)
  191. {
  192. $this->metaData->getMapper()->delete($entity);
  193. $this->entityStorage->remove($entity->getTransactionId());
  194. return true;
  195. }
  196. /**
  197. * {@inheritdoc}
  198. */
  199. public function save(\Magento\Sales\Api\Data\TransactionInterface $entity)
  200. {
  201. $this->metaData->getMapper()->save($entity);
  202. $this->entityStorage->add($entity);
  203. return $this->entityStorage->get($entity->getTransactionId());
  204. }
  205. /**
  206. * Creates new Transaction instance.
  207. *
  208. * @return \Magento\Sales\Api\Data\TransactionInterface Transaction interface.
  209. */
  210. public function create()
  211. {
  212. return $this->metaData->getNewInstance();
  213. }
  214. /**
  215. * Retrieve collection processor
  216. *
  217. * @deprecated 101.0.0
  218. * @return CollectionProcessorInterface
  219. */
  220. private function getCollectionProcessor()
  221. {
  222. if (!$this->collectionProcessor) {
  223. $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
  224. \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
  225. );
  226. }
  227. return $this->collectionProcessor;
  228. }
  229. }