InvoiceRepository.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Model\Order;
  7. use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
  8. use Magento\Sales\Api\InvoiceRepositoryInterface;
  9. use Magento\Sales\Model\ResourceModel\Metadata;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. use Magento\Sales\Api\Data\InvoiceSearchResultInterfaceFactory as SearchResultFactory;
  12. /**
  13. * Class InvoiceRepository
  14. */
  15. class InvoiceRepository implements InvoiceRepositoryInterface
  16. {
  17. /**
  18. * \Magento\Sales\Api\Data\InvoiceInterface[]
  19. *
  20. * @var array
  21. */
  22. protected $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. * InvoiceRepository constructor.
  37. * @param Metadata $invoiceMetadata
  38. * @param SearchResultFactory $searchResultFactory
  39. * @param CollectionProcessorInterface|null $collectionProcessor
  40. */
  41. public function __construct(
  42. Metadata $invoiceMetadata,
  43. SearchResultFactory $searchResultFactory,
  44. CollectionProcessorInterface $collectionProcessor = null
  45. ) {
  46. $this->metadata = $invoiceMetadata;
  47. $this->searchResultFactory = $searchResultFactory;
  48. $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
  49. }
  50. /**
  51. * Load entity
  52. *
  53. * @param int $id
  54. * @return mixed
  55. * @throws NoSuchEntityException
  56. * @throws \Magento\Framework\Exception\InputException
  57. */
  58. public function get($id)
  59. {
  60. if (!$id) {
  61. throw new \Magento\Framework\Exception\InputException(__('An ID is needed. Set the ID and try again.'));
  62. }
  63. if (!isset($this->registry[$id])) {
  64. /** @var \Magento\Sales\Api\Data\InvoiceInterface $entity */
  65. $entity = $this->metadata->getNewInstance()->load($id);
  66. if (!$entity->getEntityId()) {
  67. throw new NoSuchEntityException(
  68. __("The entity that was requested doesn't exist. Verify the entity and try again.")
  69. );
  70. }
  71. $this->registry[$id] = $entity;
  72. }
  73. return $this->registry[$id];
  74. }
  75. /**
  76. * @return \Magento\Sales\Api\Data\InvoiceInterface
  77. */
  78. public function create()
  79. {
  80. return $this->metadata->getNewInstance();
  81. }
  82. /**
  83. * Find entities by criteria
  84. *
  85. * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
  86. * @return \Magento\Sales\Api\Data\InvoiceInterface[]
  87. */
  88. public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  89. {
  90. /** @var \Magento\Sales\Model\ResourceModel\Order\Invoice\Collection $collection */
  91. $collection = $this->searchResultFactory->create();
  92. $this->collectionProcessor->process($searchCriteria, $collection);
  93. $collection->setSearchCriteria($searchCriteria);
  94. return $collection;
  95. }
  96. /**
  97. * Register entity to delete
  98. *
  99. * @param \Magento\Sales\Api\Data\InvoiceInterface $entity
  100. * @return bool
  101. */
  102. public function delete(\Magento\Sales\Api\Data\InvoiceInterface $entity)
  103. {
  104. $this->metadata->getMapper()->delete($entity);
  105. unset($this->registry[$entity->getEntityId()]);
  106. return true;
  107. }
  108. /**
  109. * Delete entity by Id
  110. *
  111. * @param int $id
  112. * @return bool
  113. */
  114. public function deleteById($id)
  115. {
  116. $entity = $this->get($id);
  117. return $this->delete($entity);
  118. }
  119. /**
  120. * Perform persist operations for one entity
  121. *
  122. * @param \Magento\Sales\Api\Data\InvoiceInterface $entity
  123. * @return \Magento\Sales\Api\Data\InvoiceInterface
  124. */
  125. public function save(\Magento\Sales\Api\Data\InvoiceInterface $entity)
  126. {
  127. $this->metadata->getMapper()->save($entity);
  128. $this->registry[$entity->getEntityId()] = $entity;
  129. return $this->registry[$entity->getEntityId()];
  130. }
  131. /**
  132. * Retrieve collection processor
  133. *
  134. * @deprecated 101.0.0
  135. * @return CollectionProcessorInterface
  136. */
  137. private function getCollectionProcessor()
  138. {
  139. if (!$this->collectionProcessor) {
  140. $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
  141. \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
  142. );
  143. }
  144. return $this->collectionProcessor;
  145. }
  146. }