TaxRuleRegistry.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\Calculation;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Tax\Model\Calculation\Rule as TaxRuleModel;
  9. use Magento\Tax\Model\Calculation\RuleFactory as TaxRuleModelFactory;
  10. class TaxRuleRegistry
  11. {
  12. /**
  13. * @var \Magento\Tax\Model\Calculation\RuleFactory
  14. */
  15. private $taxRuleModelFactory;
  16. /**
  17. * @var array taxRuleId => TaxRuleModel
  18. */
  19. private $registry = [];
  20. /**
  21. * Constructor
  22. *
  23. * @param TaxRuleModelFactory $taxRuleModelFactory
  24. */
  25. public function __construct(
  26. TaxRuleModelFactory $taxRuleModelFactory
  27. ) {
  28. $this->taxRuleModelFactory = $taxRuleModelFactory;
  29. }
  30. /**
  31. * Registers TaxRule Model to registry
  32. *
  33. * @param TaxRuleModel $taxRuleModel
  34. * @return void
  35. */
  36. public function registerTaxRule(TaxRuleModel $taxRuleModel)
  37. {
  38. $this->registry[$taxRuleModel->getId()] = $taxRuleModel;
  39. }
  40. /**
  41. * Retrieve TaxRule Model from registry given an id
  42. *
  43. * @param int $taxRuleId
  44. * @return TaxRuleModel
  45. * @throws NoSuchEntityException
  46. */
  47. public function retrieveTaxRule($taxRuleId)
  48. {
  49. if (isset($this->registry[$taxRuleId])) {
  50. return $this->registry[$taxRuleId];
  51. }
  52. $taxRuleModel = $this->taxRuleModelFactory->create()->load($taxRuleId);
  53. if (!$taxRuleModel->getId()) {
  54. // tax rule does not exist
  55. throw NoSuchEntityException::singleField('taxRuleId', $taxRuleId);
  56. }
  57. $this->registry[$taxRuleModel->getId()] = $taxRuleModel;
  58. return $taxRuleModel;
  59. }
  60. /**
  61. * Remove an instance of the TaxRule Model from the registry
  62. *
  63. * @param int $taxRuleId
  64. * @return void
  65. */
  66. public function removeTaxRule($taxRuleId)
  67. {
  68. unset($this->registry[$taxRuleId]);
  69. }
  70. }