InvoiceSentRegistry.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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;
  7. use Magento\Framework\Exception\AlreadyExistsException;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Sales\Api\Data\InvoiceInterface;
  10. use Vertex\Tax\Model\Data\InvoiceSent;
  11. use Vertex\Tax\Model\Data\InvoiceSentFactory;
  12. use Vertex\Tax\Model\Repository\InvoiceSentRepository;
  13. /**
  14. * Registry of Invoice Sent status
  15. *
  16. * Chiefly used to reduce database calls
  17. */
  18. class InvoiceSentRegistry
  19. {
  20. /** @var InvoiceSentRepository */
  21. private $repository;
  22. /** @var InvoiceSentFactory */
  23. private $invoiceSentFactory;
  24. /** @var bool[] Indexed by Invoice ID */
  25. private $isSentCache = [];
  26. /** @var ExceptionLogger */
  27. private $logger;
  28. /**
  29. * @param InvoiceSentRepository $repository
  30. * @param InvoiceSentFactory $invoiceSentFactory
  31. * @param ExceptionLogger $logger
  32. */
  33. public function __construct(
  34. InvoiceSentRepository $repository,
  35. InvoiceSentFactory $invoiceSentFactory,
  36. ExceptionLogger $logger
  37. ) {
  38. $this->repository = $repository;
  39. $this->invoiceSentFactory = $invoiceSentFactory;
  40. $this->logger = $logger;
  41. }
  42. /**
  43. * Determine if an invoice has already been sent to Vertex
  44. *
  45. * @param InvoiceInterface $invoice
  46. * @return bool
  47. */
  48. public function hasInvoiceBeenSentToVertex(InvoiceInterface $invoice)
  49. {
  50. if ($this->isInvoiceCachedAsSent($invoice)) {
  51. return true;
  52. }
  53. try {
  54. $invoiceSent = $this->repository->getByInvoiceId($invoice->getEntityId());
  55. } catch (NoSuchEntityException $exception) {
  56. return false;
  57. }
  58. $result = $invoiceSent->isSent();
  59. if ($result) {
  60. $this->cacheInvoiceSent($invoice);
  61. }
  62. return $result;
  63. }
  64. /**
  65. * Declare that an invoice has been sent to Vertex
  66. *
  67. * @param InvoiceInterface $invoice
  68. */
  69. public function setInvoiceHasBeenSentToVertex(InvoiceInterface $invoice)
  70. {
  71. if ($this->isInvoiceCachedAsSent($invoice) || !$invoice->getEntityId()) {
  72. return;
  73. }
  74. /** @var InvoiceSent $invoiceSent */
  75. $invoiceSent = $this->invoiceSentFactory->create();
  76. $invoiceSent->setInvoiceId($invoice->getEntityId());
  77. $invoiceSent->setIsSent(true);
  78. try {
  79. $this->repository->save($invoiceSent);
  80. } catch (AlreadyExistsException $exception) {
  81. // Too many cooks - or requests, as the case may be. Perfectly acceptable
  82. } catch (\Exception $exception) {
  83. $this->logger->critical($exception);
  84. }
  85. }
  86. /**
  87. * Check the cache for whether or not an Invoice has been sent to Vertex
  88. *
  89. * @param InvoiceInterface $invoice
  90. * @return bool
  91. */
  92. private function isInvoiceCachedAsSent(InvoiceInterface $invoice)
  93. {
  94. return $invoice->getEntityId()
  95. && isset($this->isSentCache[$invoice->getEntityId()])
  96. && $this->isSentCache[$invoice->getEntityId()];
  97. }
  98. /**
  99. * Add to the cache that an Invoice has been sent to Vertex
  100. *
  101. * @param InvoiceInterface $invoice
  102. */
  103. private function cacheInvoiceSent(InvoiceInterface $invoice)
  104. {
  105. if ($invoice->getEntityId()) {
  106. $this->isSentCache[$invoice->getEntityId()] = true;
  107. }
  108. }
  109. }