TaxClassNameRepository.php 3.6 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\Repository;
  7. use Magento\Customer\Api\GroupRepositoryInterface;
  8. use Magento\Framework\Api\SearchCriteriaBuilder;
  9. use Magento\Framework\Api\SearchCriteriaBuilderFactory;
  10. use Magento\Framework\Exception\InputException;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. use Magento\Tax\Api\TaxClassRepositoryInterface;
  13. use Vertex\Tax\Model\ExceptionLogger;
  14. /**
  15. * Repository of Tax Class Names
  16. */
  17. class TaxClassNameRepository
  18. {
  19. /** @var SearchCriteriaBuilderFactory */
  20. private $criteriaBuilderFactory;
  21. /** @var GroupRepositoryInterface */
  22. private $customerGroupRepository;
  23. /** @var ExceptionLogger */
  24. private $logger;
  25. /** @var TaxClassRepositoryInterface */
  26. private $repository;
  27. /**
  28. * @param TaxClassRepositoryInterface $repository
  29. * @param ExceptionLogger $logger
  30. * @param SearchCriteriaBuilderFactory $criteriaBuilderFactory
  31. * @param GroupRepositoryInterface $customerGroupRepository
  32. */
  33. public function __construct(
  34. TaxClassRepositoryInterface $repository,
  35. ExceptionLogger $logger,
  36. SearchCriteriaBuilderFactory $criteriaBuilderFactory,
  37. GroupRepositoryInterface $customerGroupRepository
  38. ) {
  39. $this->repository = $repository;
  40. $this->logger = $logger;
  41. $this->criteriaBuilderFactory = $criteriaBuilderFactory;
  42. $this->customerGroupRepository = $customerGroupRepository;
  43. }
  44. /**
  45. * Get the name of a Tax Class given a Customer Group it's assigned to
  46. *
  47. * @param int $customerGroupId
  48. * @return string
  49. */
  50. public function getByCustomerGroupId($customerGroupId)
  51. {
  52. try {
  53. $classId = $this->customerGroupRepository->getById($customerGroupId)->getTaxClassId();
  54. } catch (\Exception $e) {
  55. $this->logger->warning($e);
  56. $classId = 0;
  57. }
  58. return $this->getById($classId);
  59. }
  60. /**
  61. * Get the Tax Class Name given it's ID - or None if it could not be found
  62. *
  63. * @param int $taxClassId
  64. * @return string
  65. */
  66. public function getById($taxClassId)
  67. {
  68. if (!$taxClassId) {
  69. return 'None';
  70. }
  71. try {
  72. return $this->repository->get($taxClassId)
  73. ->getClassName();
  74. } catch (NoSuchEntityException $exception) {
  75. $this->logger->critical($exception);
  76. return 'None';
  77. }
  78. }
  79. /**
  80. * Get an array of Tax Class Names given an array of Tax Class IDs
  81. *
  82. * Will provide "None" when a tax class is not found for any given ID
  83. *
  84. * @param int[] $taxClassIds
  85. * @return string[] Indexed by tax class id
  86. */
  87. public function getListByIds(array $taxClassIds)
  88. {
  89. /** @var SearchCriteriaBuilder $criteriaBuilder */
  90. $criteriaBuilder = $this->criteriaBuilderFactory->create();
  91. $criteriaBuilder->addFilter('class_id', $taxClassIds, 'in');
  92. $criteria = $criteriaBuilder->create();
  93. try {
  94. $list = $this->repository->getList($criteria);
  95. } catch (InputException $exception) {
  96. $this->logger->critical($exception);
  97. }
  98. $result = [];
  99. foreach ($list->getItems() as $item) {
  100. $result[$item->getClassId()] = $item->getClassName();
  101. }
  102. foreach ($taxClassIds as $classId) {
  103. if (!isset($result[$classId])) {
  104. $result[$classId] = 'None';
  105. }
  106. }
  107. return $result;
  108. }
  109. }