1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Tax\Model\Calculation;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Tax\Model\Calculation\Rate as TaxRateModel;
- use Magento\Tax\Model\Calculation\RateFactory as TaxRateModelFactory;
- class RateRegistry
- {
- /**
- * Tax rate model factory
- *
- * @var TaxRateModelFactory
- */
- private $taxRateModelFactory;
- /**
- * Tax rate models
- *
- * @var TaxRateModel[]
- */
- private $taxRateRegistryById = [];
- /**
- * Constructor
- *
- * @param TaxRateModelFactory $taxModelRateFactory
- */
- public function __construct(
- TaxRateModelFactory $taxModelRateFactory
- ) {
- $this->taxRateModelFactory = $taxModelRateFactory;
- }
- /**
- * Register TaxRate Model to registry
- *
- * @param TaxRateModel $taxRateModel
- * @return void
- */
- public function registerTaxRate(TaxRateModel $taxRateModel)
- {
- $this->taxRateRegistryById[$taxRateModel->getId()] = $taxRateModel;
- }
- /**
- * Retrieve TaxRate Model from registry given an id
- *
- * @param int $taxRateId
- * @return TaxRateModel
- * @throws NoSuchEntityException
- */
- public function retrieveTaxRate($taxRateId)
- {
- if (isset($this->taxRateRegistryById[$taxRateId])) {
- return $this->taxRateRegistryById[$taxRateId];
- }
- /** @var TaxRateModel $taxRateModel */
- $taxRateModel = $this->taxRateModelFactory->create()->load($taxRateId);
- if (!$taxRateModel->getId()) {
- // tax rate does not exist
- throw NoSuchEntityException::singleField('taxRateId', $taxRateId);
- }
- $this->taxRateRegistryById[$taxRateModel->getId()] = $taxRateModel;
- return $taxRateModel;
- }
- /**
- * Remove an instance of the TaxRate Model from the registry
- *
- * @param int $taxRateId
- * @return void
- */
- public function removeTaxRate($taxRateId)
- {
- unset($this->taxRateRegistryById[$taxRateId]);
- }
- }
|