diff --git a/Classes/Domain/Model/Cart/TaxClass.php b/Classes/Domain/Model/Cart/TaxClass.php index 20db512e..f43ec0fa 100644 --- a/Classes/Domain/Model/Cart/TaxClass.php +++ b/Classes/Domain/Model/Cart/TaxClass.php @@ -11,27 +11,14 @@ * LICENSE file that was distributed with this source code. */ -class TaxClass +final class TaxClass implements TaxClassInterface { - protected int $id; - - protected string $value; - - protected float $calc; - - protected string $title; - public function __construct( - int $id, - string $value, - float $calc, - string $title - ) { - $this->id = $id; - $this->value = $value; - $this->calc = $calc; - $this->title = $title; - } + private readonly int $id, + private readonly string $value, + private readonly float $calc, + private readonly string $title + ) {} public function getId(): int { diff --git a/Classes/Domain/Model/Cart/TaxClassFactory.php b/Classes/Domain/Model/Cart/TaxClassFactory.php new file mode 100644 index 00000000..87547156 --- /dev/null +++ b/Classes/Domain/Model/Cart/TaxClassFactory.php @@ -0,0 +1,50 @@ +isValidTaxClassConfig($taxClassKey, $taxClassValue)) { + return new TaxClass( + $taxClassKey, + $taxClassValue['value'], + (float)$taxClassValue['calc'], + $taxClassValue['name'] + ); + } + + return null; + } + + private function isValidTaxClassConfig(int $key, array $value): bool + { + if (empty($value) || + empty($value['name']) || + !isset($value['calc']) || + !is_numeric($value['calc']) + ) { + $this->logger->error('Can\'t create tax class object for the configuration with the index=' . $key . '.', []); + + return false; + } + + return true; + } +} diff --git a/Classes/Domain/Model/Cart/TaxClassFactoryInterface.php b/Classes/Domain/Model/Cart/TaxClassFactoryInterface.php new file mode 100644 index 00000000..e9daabc7 --- /dev/null +++ b/Classes/Domain/Model/Cart/TaxClassFactoryInterface.php @@ -0,0 +1,17 @@ +settings = $this->configurationManager->getConfiguration( ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, @@ -50,33 +51,13 @@ public function getTaxClasses(string $countryCode = null): array } foreach ($taxClassSettings as $taxClassKey => $taxClassValue) { - if ($this->isValidTaxClassConfig($taxClassKey, $taxClassValue)) { - $taxClasses[$taxClassKey] = GeneralUtility::makeInstance( - TaxClass::class, - (int)$taxClassKey, - $taxClassValue['value'], - (float)$taxClassValue['calc'], - $taxClassValue['name'] - ); + $taxClass = $this->taxClassFactory->getTaxClass($taxClassKey, $taxClassValue); + + if ($taxClass instanceof TaxClassInterface) { + $taxClasses[$taxClassKey] = $taxClass; } } return $taxClasses; } - - protected function isValidTaxClassConfig(int $key, array $value): bool - { - if (empty($value) || - empty($value['name']) || - !isset($value['calc']) || - (isset($value['calc']) && !is_numeric($value['calc'])) - ) { - $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__); - $logger->error('Can\'t create tax class object for the configuration with the index=' . $key . '.', []); - - return false; - } - - return true; - } } diff --git a/Documentation/Changelog/9.0/Breaking-505-ReplaceParserUtilityParseTaxByDI.rst b/Documentation/Changelog/9.0/Breaking-505-ReplaceParserUtilityParseTaxByDI.rst new file mode 100644 index 00000000..32019d5e --- /dev/null +++ b/Documentation/Changelog/9.0/Breaking-505-ReplaceParserUtilityParseTaxByDI.rst @@ -0,0 +1,31 @@ +.. include:: ../../Includes.txt + +====================================================== +Breaking: #505 - Replace ParserUtility::parseTax by DI +====================================================== + +See :issue:`480` + +Description +=========== + +The existing `\Extcode\Cart\Utility\ParserUtility::parseTax()` uses a TaxClassService +which could be configured by a TypoScript Configuration. The change remove the +method from the class and injecting the `Extcode\Cart\Service\TaxClassServiceInterface` +by Dependency Injection. + +Affected Installations +====================== + +All installations where `plugin.tx_cart.taxClasses.className` is used to replace +the default TaxClassService. + +Migration +========= + +Remove the old configuration from TypoScript. +Add an entry to your `Services.yaml` or `Services.php` and configure your +implementation of the `Extcode\Cart\Service\TaxClassServiceInterface` for the +`$taxClassService` constructor argument. + +.. index:: Backend, Dependency Injection