InvoiceSentRepository.php 3.1 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\Data\InvoiceSent;
  12. use Vertex\Tax\Model\Data\InvoiceSentFactory;
  13. use Vertex\Tax\Model\ResourceModel\InvoiceSent as ResourceModel;
  14. /**
  15. * Repository of Invoice sent data
  16. */
  17. class InvoiceSentRepository
  18. {
  19. /** @var ResourceModel */
  20. private $resourceModel;
  21. /** @var InvoiceSentFactory */
  22. private $factory;
  23. /**
  24. * @param ResourceModel $resourceModel
  25. * @param InvoiceSentFactory $factory
  26. */
  27. public function __construct(ResourceModel $resourceModel, InvoiceSentFactory $factory)
  28. {
  29. $this->resourceModel = $resourceModel;
  30. $this->factory = $factory;
  31. }
  32. /**
  33. * Save an InvoiceSent object
  34. *
  35. * @param InvoiceSent $invoiceSent
  36. * @return $this
  37. * @throws \Magento\Framework\Exception\CouldNotSaveException
  38. * @throws \Magento\Framework\Exception\AlreadyExistsException
  39. */
  40. public function save(InvoiceSent $invoiceSent)
  41. {
  42. try {
  43. $this->resourceModel->save($invoiceSent);
  44. } catch (AlreadyExistsException $e) {
  45. throw $e;
  46. } catch (\Exception $e) {
  47. throw new CouldNotSaveException(__('Unable to save Invoice Sent status'), $e);
  48. }
  49. return $this;
  50. }
  51. /**
  52. * Delete an InvoiceSent object
  53. *
  54. * @param InvoiceSent $invoiceSent
  55. * @return $this
  56. * @throws CouldNotDeleteException
  57. */
  58. public function delete(InvoiceSent $invoiceSent)
  59. {
  60. try {
  61. $this->resourceModel->delete($invoiceSent);
  62. } catch (\Exception $e) {
  63. throw new CouldNotDeleteException(__('Unable to delete Invoice Sent status'), $e);
  64. }
  65. return $this;
  66. }
  67. /**
  68. * Delete an InvoiceSent object given an Invoice ID
  69. *
  70. * @param int $invoiceId
  71. * @return $this
  72. * @throws CouldNotDeleteException
  73. */
  74. public function deleteByInvoiceId($invoiceId)
  75. {
  76. /** @var InvoiceSent $invoiceSent */
  77. $invoiceSent = $this->factory->create();
  78. $invoiceSent->setId($invoiceId);
  79. try {
  80. $this->resourceModel->delete($invoiceSent);
  81. } catch (\Exception $e) {
  82. throw new CouldNotDeleteException(__('Unable to delete Invoice Sent status'), $e);
  83. }
  84. return $this;
  85. }
  86. /**
  87. * Retrieve an InvoiceSent object for an Invoice
  88. *
  89. * @param int $invoiceId
  90. * @return InvoiceSent
  91. * @throws NoSuchEntityException
  92. */
  93. public function getByInvoiceId($invoiceId)
  94. {
  95. $invoiceSent = $this->factory->create();
  96. $this->resourceModel->load($invoiceSent, $invoiceId);
  97. if (!$invoiceSent->getId()) {
  98. throw NoSuchEntityException::singleField('invoiceId', $invoiceId);
  99. }
  100. return $invoiceSent;
  101. }
  102. }