Repository.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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;
  7. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Sales\Api\Data\OrderPaymentSearchResultInterfaceFactory as SearchResultFactory;
  10. use Magento\Sales\Api\OrderPaymentRepositoryInterface;
  11. use Magento\Sales\Model\ResourceModel\Metadata;
  12. /**
  13. * Class Repository
  14. */
  15. class Repository implements OrderPaymentRepositoryInterface
  16. {
  17. /**
  18. * Magento\Sales\Model\Order\Payment\Transaction[]
  19. *
  20. * @var array
  21. */
  22. private $registry = [];
  23. /**
  24. * @var Metadata
  25. */
  26. protected $metaData;
  27. /**
  28. * @var SearchResultFactory
  29. */
  30. protected $searchResultFactory;
  31. /**
  32. * @var \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface
  33. */
  34. private $collectionProcessor;
  35. /**
  36. * @param Metadata $metaData
  37. * @param SearchResultFactory $searchResultFactory
  38. * @param CollectionProcessorInterface $collectionProcessor
  39. */
  40. public function __construct(
  41. Metadata $metaData,
  42. SearchResultFactory $searchResultFactory,
  43. CollectionProcessorInterface $collectionProcessor = null
  44. ) {
  45. $this->metaData = $metaData;
  46. $this->searchResultFactory = $searchResultFactory;
  47. $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
  48. }
  49. /**
  50. * Lists order payments that match specified search criteria.
  51. *
  52. * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria The search criteria.
  53. * @return \Magento\Sales\Api\Data\OrderPaymentSearchResultInterface Order payment search result interface.
  54. */
  55. public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  56. {
  57. /** @var \Magento\Sales\Model\ResourceModel\Order\Payment\Collection $collection */
  58. $collection = $this->searchResultFactory->create();
  59. $collection->setSearchCriteria($searchCriteria);
  60. $this->collectionProcessor->process($searchCriteria, $collection);
  61. return $collection;
  62. }
  63. /**
  64. * Loads a specified order payment.
  65. *
  66. * @param int $id The order payment ID.
  67. * @return \Magento\Sales\Api\Data\OrderPaymentInterface Order payment interface.
  68. * @throws NoSuchEntityException
  69. * @throws \Magento\Framework\Exception\InputException
  70. */
  71. public function get($id)
  72. {
  73. if (!$id) {
  74. throw new \Magento\Framework\Exception\InputException(__('An ID is needed. Set the ID and try again.'));
  75. }
  76. if (!isset($this->registry[$id])) {
  77. $entity = $this->metaData->getNewInstance()->load($id);
  78. if (!$entity->getId()) {
  79. throw new NoSuchEntityException(
  80. __("The entity that was requested doesn't exist. Verify the entity and try again.")
  81. );
  82. }
  83. $this->registry[$id] = $entity;
  84. }
  85. return $this->registry[$id];
  86. }
  87. /**
  88. * Deletes a specified order payment.
  89. *
  90. * @param \Magento\Sales\Api\Data\OrderPaymentInterface $entity The order payment ID.
  91. * @return bool
  92. */
  93. public function delete(\Magento\Sales\Api\Data\OrderPaymentInterface $entity)
  94. {
  95. $this->metaData->getMapper()->delete($entity);
  96. return true;
  97. }
  98. /**
  99. * Performs persist operations for a specified order payment.
  100. *
  101. * @param \Magento\Sales\Api\Data\OrderPaymentInterface $entity The order payment ID.
  102. * @return \Magento\Sales\Api\Data\OrderPaymentInterface Order payment interface.
  103. */
  104. public function save(\Magento\Sales\Api\Data\OrderPaymentInterface $entity)
  105. {
  106. $this->metaData->getMapper()->save($entity);
  107. return $entity;
  108. }
  109. /**
  110. * Creates new Order Payment instance.
  111. *
  112. * @return \Magento\Sales\Api\Data\OrderPaymentInterface Transaction interface.
  113. */
  114. public function create()
  115. {
  116. return $this->metaData->getNewInstance();
  117. }
  118. /**
  119. * Retrieve collection processor
  120. *
  121. * @deprecated 101.0.0
  122. * @return CollectionProcessorInterface
  123. */
  124. private function getCollectionProcessor()
  125. {
  126. if (!$this->collectionProcessor) {
  127. $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
  128. \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
  129. );
  130. }
  131. return $this->collectionProcessor;
  132. }
  133. }