* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ namespace PrestaShop\PrestaShop\Adapter\Tax\CommandHandler; use PrestaShop\PrestaShop\Adapter\Tax\AbstractTaxHandler; use PrestaShop\PrestaShop\Core\Domain\Tax\Command\EditTaxCommand; use PrestaShop\PrestaShop\Core\Domain\Tax\CommandHandler\EditTaxHandlerInterface; use PrestaShop\PrestaShop\Core\Domain\Tax\Exception\TaxException; use PrestaShopException; use Tax; /** * Handles command which is responsible for tax editing */ final class EditTaxHandler extends AbstractTaxHandler implements EditTaxHandlerInterface { /** * {@inheritdoc} * * @throws TaxException */ public function handle(EditTaxCommand $command) { $tax = $this->getTax($command->getTaxId()); if (null !== $command->getLocalizedNames()) { $tax->name = $command->getLocalizedNames(); } if (null !== $command->getRate()) { $tax->rate = $command->getRate(); } if (null !== $command->isEnabled()) { $tax->active = $command->isEnabled(); } try { if (false === $tax->validateFields(false) || false === $tax->validateFieldsLang(false)) { throw new TaxException('Tax contains invalid field values'); } if (!$tax->update()) { throw new TaxException(sprintf('Cannot update tax with id "%s"', $tax->id)); } } catch (PrestaShopException $e) { throw new TaxException(sprintf('Cannot update tax with id "%s"', $tax->id)); } } }