OrderInvoiceStatusRepository.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model\Repository;
  7. use Magento\Framework\Exception\AlreadyExistsException;
  8. use Magento\Framework\Exception\CouldNotDeleteException;
  9. use Magento\Framework\Exception\CouldNotSaveException;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. use Vertex\Tax\Model\ResourceModel\OrderInvoiceStatus as ResourceModel;
  12. use Vertex\Tax\Model\Data\OrderInvoiceStatusFactory as Factory;
  13. use Vertex\Tax\Model\Data\OrderInvoiceStatus;
  14. /**
  15. * Repository of Order Invoice data
  16. */
  17. class OrderInvoiceStatusRepository
  18. {
  19. /** @var ResourceModel */
  20. private $resourceModel;
  21. /** @var Factory */
  22. private $factory;
  23. /**
  24. * @param ResourceModel $resourceModel
  25. * @param Factory $factory
  26. */
  27. public function __construct(ResourceModel $resourceModel, Factory $factory)
  28. {
  29. $this->resourceModel = $resourceModel;
  30. $this->factory = $factory;
  31. }
  32. /**
  33. * Save an OrderInvoiceStatus object
  34. *
  35. * @param OrderInvoiceStatus $orderInvoiceStatus
  36. * @return OrderInvoiceStatusRepository
  37. * @throws AlreadyExistsException
  38. * @throws CouldNotSaveException
  39. */
  40. public function save(OrderInvoiceStatus $orderInvoiceStatus)
  41. {
  42. try {
  43. $this->resourceModel->save($orderInvoiceStatus);
  44. } catch (AlreadyExistsException $e) {
  45. throw $e;
  46. } catch (\Exception $e) {
  47. throw new CouldNotSaveException(__('Unable to save Order Invoice Sent status'), $e);
  48. }
  49. return $this;
  50. }
  51. /**
  52. * Delete an OrderInvoiceStatus object
  53. *
  54. * @param OrderInvoiceStatus $orderInvoiceStatus
  55. * @return void
  56. * @throws CouldNotDeleteException
  57. */
  58. public function delete(OrderInvoiceStatus $orderInvoiceStatus)
  59. {
  60. try {
  61. $this->resourceModel->delete($orderInvoiceStatus);
  62. } catch (\Exception $e) {
  63. throw new CouldNotDeleteException(__('Unable to delete Order Invoice Sent status'), $e);
  64. }
  65. }
  66. /**
  67. * Delete an OrderInvoiceStatus object given an Order ID
  68. *
  69. * @param int $orderId
  70. * @return OrderInvoiceStatusRepository
  71. * @throws CouldNotDeleteException
  72. */
  73. public function deleteByOrderId($orderId)
  74. {
  75. /** @var OrderInvoiceStatus $orderInvoiceStatus */
  76. $orderInvoiceStatus = $this->factory->create();
  77. $orderInvoiceStatus->setId($orderId);
  78. try {
  79. $this->resourceModel->delete($orderInvoiceStatus);
  80. } catch (\Exception $e) {
  81. throw new CouldNotDeleteException(__('Unable to delete Order Invoice Sent status'), $e);
  82. }
  83. return $this;
  84. }
  85. /**
  86. * Retrieve an OrderInvoiceStatus object for an Order
  87. *
  88. * @param int $orderId
  89. * @return OrderInvoiceStatus
  90. * @throws NoSuchEntityException
  91. */
  92. public function getByOrderId($orderId)
  93. {
  94. /** @var OrderInvoiceStatus $orderInvoiceStatus */
  95. $orderInvoiceStatus = $this->factory->create();
  96. $this->resourceModel->load($orderInvoiceStatus, $orderId);
  97. if (!$orderInvoiceStatus->getId()) {
  98. throw NoSuchEntityException::singleField('orderId', $orderId);
  99. }
  100. return $orderInvoiceStatus;
  101. }
  102. }