RmaAccess.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\ResourceModel\Rma;
  6. use Magento\Framework\ObjectManagerInterface;
  7. use Magento\Framework\Registry;
  8. use Magento\Rma\Api\Data\RmaInterface;
  9. use Magento\Rma\Api\RmaRepositoryInterface;
  10. use Magento\Rma\Model\ResourceModel\Item as RmaItemResource;
  11. use Temando\Shipping\Model\Config\ModuleConfigInterface;
  12. use Temando\Shipping\Model\ShipmentInterface;
  13. /**
  14. * Central entry point for all entities related to the EE RMA module.
  15. *
  16. * @package Temando\Shipping\Model
  17. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. * @link http://www.temando.com/
  20. */
  21. class RmaAccess
  22. {
  23. /**
  24. * @var ModuleConfigInterface
  25. */
  26. private $config;
  27. /**
  28. * @var Registry
  29. */
  30. private $registry;
  31. /**
  32. * @var ObjectManagerInterface
  33. */
  34. private $objectManager;
  35. /**
  36. * @var RmaInterface
  37. */
  38. private $currentRma;
  39. /**
  40. * @var ShipmentInterface
  41. */
  42. private $currentRmaShipment;
  43. /**
  44. * RmaAccess constructor.
  45. *
  46. * @param ModuleConfigInterface $config
  47. * @param Registry $registry
  48. * @param ObjectManagerInterface $objectManager
  49. */
  50. public function __construct(
  51. ModuleConfigInterface $config,
  52. Registry $registry,
  53. ObjectManagerInterface $objectManager
  54. ) {
  55. $this->config = $config;
  56. $this->registry = $registry;
  57. $this->objectManager = $objectManager;
  58. }
  59. /**
  60. * @param RmaInterface $rma
  61. * @return void
  62. */
  63. public function setCurrentRma(RmaInterface $rma)
  64. {
  65. $this->currentRma = $rma;
  66. }
  67. /**
  68. * Register the return shipment as fetched from the platform api.
  69. *
  70. * @param ShipmentInterface $rmaShipment
  71. * @return void
  72. */
  73. public function setCurrentRmaShipment(ShipmentInterface $rmaShipment)
  74. {
  75. $this->currentRmaShipment = $rmaShipment;
  76. }
  77. /**
  78. * @return RmaInterface|null
  79. */
  80. public function getCurrentRma()
  81. {
  82. if ($this->currentRma instanceof RmaInterface) {
  83. return $this->currentRma;
  84. }
  85. // fall back to RMA registered in core registry
  86. return $this->registry->registry('current_rma');
  87. }
  88. /**
  89. * @return ShipmentInterface|null
  90. */
  91. public function getCurrentRmaShipment()
  92. {
  93. return $this->currentRmaShipment;
  94. }
  95. /**
  96. * Wrapper around RMA factory which is not available in CE and thus cannot be injected.
  97. *
  98. * @see \Magento\Rma\Api\Data\RmaInterfaceFactory::create
  99. *
  100. * @param mixed[] $data
  101. * @return RmaInterface
  102. */
  103. public function create($data = [])
  104. {
  105. $rma = $this->objectManager->create(RmaInterface::class, $data);
  106. return $rma;
  107. }
  108. /**
  109. * Wrapper around RMA repository which is not available in CE and thus cannot be injected.
  110. *
  111. * @see \Magento\Rma\Api\RmaRepositoryInterface::get
  112. *
  113. * @param int $rmaId
  114. * @return RmaInterface|null
  115. */
  116. public function getById($rmaId)
  117. {
  118. if (!$this->config->isRmaAvailable()) {
  119. return null;
  120. }
  121. /** @var RmaRepositoryInterface $rmaRepository */
  122. $rmaRepository = $this->objectManager->create(RmaRepositoryInterface::class);
  123. return $rmaRepository->get($rmaId);
  124. }
  125. /**
  126. * Wrapper around RMA item resource which is not available in CE and thus cannot be injected.
  127. *
  128. * @see \Magento\Rma\Model\ResourceModel\Item::getAuthorizedItems
  129. *
  130. * @param int $rmaId
  131. * @return mixed[]
  132. */
  133. public function getAuthorizedItems($rmaId)
  134. {
  135. if (!$this->config->isRmaAvailable()) {
  136. return [];
  137. }
  138. /** @var RmaItemResource $rmaItemResource */
  139. $rmaItemResource = $this->objectManager->create(RmaItemResource::class);
  140. return $rmaItemResource->getAuthorizedItems($rmaId);
  141. }
  142. }