CustomerCodeRepository.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\CustomerCode;
  12. use Vertex\Tax\Model\Data\CustomerCodeFactory;
  13. use Vertex\Tax\Model\Registry\CustomerCodeRegistry;
  14. use Vertex\Tax\Model\ResourceModel\CustomerCode as ResourceModel;
  15. /**
  16. * Repository of Vertex Customer Codes
  17. */
  18. class CustomerCodeRepository
  19. {
  20. /** @var ResourceModel */
  21. private $resourceModel;
  22. /** @var CustomerCodeFactory */
  23. private $factory;
  24. /** @var CustomerCodeRegistry */
  25. private $registry;
  26. /**
  27. * @param ResourceModel $resourceModel
  28. * @param CustomerCodeFactory $factory
  29. * @param CustomerCodeRegistry $registry
  30. */
  31. public function __construct(
  32. ResourceModel $resourceModel,
  33. CustomerCodeFactory $factory,
  34. CustomerCodeRegistry $registry
  35. ) {
  36. $this->resourceModel = $resourceModel;
  37. $this->factory = $factory;
  38. $this->registry = $registry;
  39. }
  40. /**
  41. * Save a Customer Code
  42. *
  43. * @param CustomerCode $customerCode
  44. * @return $this
  45. * @throws AlreadyExistsException
  46. * @throws CouldNotSaveException
  47. */
  48. public function save(CustomerCode $customerCode)
  49. {
  50. // Exit early if code is already in the registry
  51. $registered = $this->registry->get($customerCode->getCustomerId());
  52. if ($registered === $customerCode->getCustomerCode()) {
  53. return $this;
  54. }
  55. try {
  56. $this->resourceModel->save($customerCode);
  57. $this->registry->set($customerCode->getCustomerId(), $customerCode->getCustomerCode());
  58. } catch (AlreadyExistsException $e) {
  59. throw $e;
  60. } catch (\Exception $originalException) {
  61. throw new CouldNotSaveException(__('Unable to save Customer Code'), $originalException);
  62. }
  63. return $this;
  64. }
  65. /**
  66. * Delete a Customer Code
  67. *
  68. * @param CustomerCode $customerCode
  69. * @return $this
  70. * @throws CouldNotDeleteException
  71. */
  72. public function delete(CustomerCode $customerCode)
  73. {
  74. return $this->deleteByCustomerId($customerCode->getCustomerId());
  75. }
  76. /**
  77. * Delete a Customer Code given a Customer ID
  78. *
  79. * @param int $customerId
  80. * @return $this
  81. * @throws CouldNotDeleteException
  82. */
  83. public function deleteByCustomerId($customerId)
  84. {
  85. /** @var CustomerCode $customerCode */
  86. $customerCode = $this->factory->create();
  87. $customerCode->setId($customerId);
  88. try {
  89. $this->resourceModel->delete($customerCode);
  90. $this->registry->delete($customerId);
  91. } catch (\Exception $originalException) {
  92. throw new CouldNotDeleteException(__('Unable to delete Customer Code'), $originalException);
  93. }
  94. return $this;
  95. }
  96. /**
  97. * Retrieve a Customer Code given a Customer ID
  98. *
  99. * @param int $customerId
  100. * @return CustomerCode
  101. * @throws NoSuchEntityException
  102. */
  103. public function getByCustomerId($customerId)
  104. {
  105. /** @var CustomerCode $customerCode */
  106. $customerCode = $this->factory->create();
  107. $registered = $this->registry->get($customerId);
  108. if ($registered === null) {
  109. throw NoSuchEntityException::singleField('customerId', $customerId);
  110. }
  111. if ($registered !== false) {
  112. $customerCode->setCustomerId($customerId);
  113. $customerCode->setCustomerCode($registered);
  114. return $customerCode;
  115. }
  116. $this->resourceModel->load($customerCode, $customerId);
  117. if (!$customerCode->getId()) {
  118. $this->registry->set($customerId, null);
  119. throw NoSuchEntityException::singleField('customerId', $customerId);
  120. }
  121. $this->registry->set($customerCode->getCustomerId(), $customerCode->getCustomerCode());
  122. return $customerCode;
  123. }
  124. /**
  125. * Retrieve an array of Customer Code's indexed by Customer ID
  126. *
  127. * @param int[] $customerIds
  128. * @return CustomerCode[] Indexed by Customer ID
  129. */
  130. public function getListByCustomerIds(array $customerIds)
  131. {
  132. $unregisteredIds = [];
  133. $registryCustomerCodes = [];
  134. foreach ($customerIds as $customerId) {
  135. $registered = $this->registry->get($customerId);
  136. if ($registered === false) {
  137. $unregisteredIds[] = $customerId;
  138. } else {
  139. $customerCode = $this->factory->create();
  140. $customerCode->setCustomerId($customerId);
  141. $customerCode->setCustomerCode($registered);
  142. $registryCustomerCodes[$customerId] = $customerCode;
  143. }
  144. }
  145. $dbCustomerCodes = [];
  146. if (!empty($unregisteredIds)) {
  147. $dbCustomerCodes = $this->resourceModel->getArrayByCustomerIds($unregisteredIds);
  148. }
  149. foreach ($dbCustomerCodes as $dbCustomerCode) {
  150. $this->registry->set($dbCustomerCode->getCustomerId(), $dbCustomerCode->getCustomerCode());
  151. }
  152. return array_replace($dbCustomerCodes, $registryCustomerCodes);
  153. }
  154. }