RateRegistry.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Rate as TaxRateModel;
  9. use Magento\Tax\Model\Calculation\RateFactory as TaxRateModelFactory;
  10. class RateRegistry
  11. {
  12. /**
  13. * Tax rate model factory
  14. *
  15. * @var TaxRateModelFactory
  16. */
  17. private $taxRateModelFactory;
  18. /**
  19. * Tax rate models
  20. *
  21. * @var TaxRateModel[]
  22. */
  23. private $taxRateRegistryById = [];
  24. /**
  25. * Constructor
  26. *
  27. * @param TaxRateModelFactory $taxModelRateFactory
  28. */
  29. public function __construct(
  30. TaxRateModelFactory $taxModelRateFactory
  31. ) {
  32. $this->taxRateModelFactory = $taxModelRateFactory;
  33. }
  34. /**
  35. * Register TaxRate Model to registry
  36. *
  37. * @param TaxRateModel $taxRateModel
  38. * @return void
  39. */
  40. public function registerTaxRate(TaxRateModel $taxRateModel)
  41. {
  42. $this->taxRateRegistryById[$taxRateModel->getId()] = $taxRateModel;
  43. }
  44. /**
  45. * Retrieve TaxRate Model from registry given an id
  46. *
  47. * @param int $taxRateId
  48. * @return TaxRateModel
  49. * @throws NoSuchEntityException
  50. */
  51. public function retrieveTaxRate($taxRateId)
  52. {
  53. if (isset($this->taxRateRegistryById[$taxRateId])) {
  54. return $this->taxRateRegistryById[$taxRateId];
  55. }
  56. /** @var TaxRateModel $taxRateModel */
  57. $taxRateModel = $this->taxRateModelFactory->create()->load($taxRateId);
  58. if (!$taxRateModel->getId()) {
  59. // tax rate does not exist
  60. throw NoSuchEntityException::singleField('taxRateId', $taxRateId);
  61. }
  62. $this->taxRateRegistryById[$taxRateModel->getId()] = $taxRateModel;
  63. return $taxRateModel;
  64. }
  65. /**
  66. * Remove an instance of the TaxRate Model from the registry
  67. *
  68. * @param int $taxRateId
  69. * @return void
  70. */
  71. public function removeTaxRate($taxRateId)
  72. {
  73. unset($this->taxRateRegistryById[$taxRateId]);
  74. }
  75. }