OrderRepository.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * This file is part of the Klarna Core module
  4. *
  5. * (c) Klarna Bank AB (publ)
  6. *
  7. * For the full copyright and license information, please view the NOTICE
  8. * and LICENSE files that were distributed with this source code.
  9. */
  10. namespace Klarna\Core\Model;
  11. use Klarna\Core\Api\OrderInterface;
  12. use Klarna\Core\Api\OrderRepositoryInterface;
  13. use Klarna\Core\Model\ResourceModel\Order as OrderResource;
  14. use Magento\Framework\Exception\CouldNotSaveException;
  15. use Magento\Framework\Exception\NoSuchEntityException;
  16. use Magento\Sales\Api\Data\OrderInterface as MageOrder;
  17. /**
  18. * Class OrderRepository
  19. *
  20. * @package Klarna\Core\Model
  21. */
  22. class OrderRepository implements OrderRepositoryInterface
  23. {
  24. /**
  25. * Order factory
  26. *
  27. * @var OrderFactory
  28. */
  29. private $orderFactory;
  30. /**
  31. * Resource model
  32. *
  33. * @var OrderResource
  34. */
  35. private $resourceModel;
  36. /**
  37. * OrderRepository constructor.
  38. *
  39. * @param OrderFactory $orderFactory
  40. * @param OrderResource $resourceModel
  41. *
  42. * @codeCoverageIgnore
  43. */
  44. public function __construct(
  45. OrderFactory $orderFactory,
  46. OrderResource $resourceModel
  47. ) {
  48. $this->orderFactory = $orderFactory;
  49. $this->resourceModel = $resourceModel;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function save(OrderInterface $order)
  55. {
  56. try {
  57. $this->resourceModel->save($order);
  58. } catch (\Exception $e) {
  59. throw new CouldNotSaveException(__($e->getMessage()));
  60. }
  61. return $order;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getByKlarnaOrderId($klarnaOrderId)
  67. {
  68. $order = $this->orderFactory->create();
  69. $orderId = $this->resourceModel->getIdByKlarnaOrderId($klarnaOrderId);
  70. if (!$orderId) {
  71. $order->setKlarnaOrderId($klarnaOrderId);
  72. return $order;
  73. }
  74. $this->resourceModel->load($order, $orderId);
  75. return $order;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getByOrder(MageOrder $mageOrder)
  81. {
  82. $order = $this->orderFactory->create();
  83. $orderId = $this->resourceModel->getIdByOrder($mageOrder);
  84. if (!$orderId) {
  85. throw new NoSuchEntityException(__('Requested order doesn\'t exist'));
  86. }
  87. $this->resourceModel->load($order, $orderId);
  88. return $order;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getById($id)
  94. {
  95. $order = $this->orderFactory->create();
  96. $this->resourceModel->load($order, $id);
  97. if (!$order->getId()) {
  98. throw new NoSuchEntityException(__('Order with id "%1" does not exist.', $id));
  99. }
  100. return $order;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getByReservationId($reservationId)
  106. {
  107. $order = $this->orderFactory->create();
  108. $orderId = $this->resourceModel->getIdByReservationId($reservationId);
  109. if (!$orderId) {
  110. throw new NoSuchEntityException(__('Order with Reservation ID "%1" does not exist.', $reservationId));
  111. }
  112. $this->resourceModel->load($order, $orderId);
  113. return $order;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function getBySessionId($sessionId)
  119. {
  120. $order = $this->orderFactory->create();
  121. $orderId = $this->resourceModel->getIdBySessionId($sessionId);
  122. if (!$orderId) {
  123. throw new NoSuchEntityException(__('Order with session_id "%1" does not exist.', $sessionId));
  124. }
  125. $this->resourceModel->load($order, $orderId);
  126. return $order;
  127. }
  128. }