Skip to content

Commit

Permalink
Replace ParserUtility::parseTaxClasses by DI (#507)
Browse files Browse the repository at this point in the history
* [BUGFIX] Add countries to test configuration

Relates: #504

* [TASK] Replace ParserUtility::parseTaxClasses by DI

Relates: #505

* [TASK] Create TaxClasses by TaxClassFactory

Relates: #505

* [TASK] Use setUp to set testExtensionsToLoad
  • Loading branch information
extcode authored Jun 3, 2024
1 parent 6aefce0 commit 96d2919
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 107 deletions.
7 changes: 6 additions & 1 deletion Classes/Controller/Cart/CountryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Extcode\Cart\Controller\Cart;

use Extcode\Cart\Service\TaxClassServiceInterface;
use Psr\Http\Message\ResponseInterface;

/*
Expand All @@ -14,6 +15,10 @@
*/
class CountryController extends ActionController
{
public function __construct(
protected TaxClassServiceInterface $taxClassService
) {}

public function updateAction(): ResponseInterface
{
//ToDo check country is allowed by TypoScript
Expand All @@ -22,7 +27,7 @@ public function updateAction(): ResponseInterface

$this->restoreSession();

$taxClasses = $this->parserUtility->parseTaxClasses($this->configurations, $this->cart->getBillingCountry());
$taxClasses = $this->taxClassService->getTaxClasses($this->cart->getBillingCountry());

$this->cart->setTaxClasses($taxClasses);
$this->cart->reCalc();
Expand Down
25 changes: 6 additions & 19 deletions Classes/Domain/Model/Cart/TaxClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
50 changes: 50 additions & 0 deletions Classes/Domain/Model/Cart/TaxClassFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Domain\Model\Cart;

/*
* This file is part of the package extcode/cart.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use Psr\Log\LoggerInterface;

final class TaxClassFactory implements TaxClassFactoryInterface
{
public function __construct(
private readonly LoggerInterface $logger
) {}

public function getTaxClass(int $taxClassKey, array $taxClassValue): ?TaxClass
{
if ($this->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;
}
}
17 changes: 17 additions & 0 deletions Classes/Domain/Model/Cart/TaxClassFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Domain\Model\Cart;

/*
* This file is part of the package extcode/cart.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

interface TaxClassFactoryInterface
{
public function getTaxClass(int $taxClassKey, array $taxClassValue): ?TaxClass;
}
23 changes: 23 additions & 0 deletions Classes/Domain/Model/Cart/TaxClassInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Domain\Model\Cart;

/*
* This file is part of the package extcode/cart.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

interface TaxClassInterface
{
public function getId(): int;

public function getValue(): string;

public function getCalc(): float;

public function getTitle(): string;
}
48 changes: 15 additions & 33 deletions Classes/Service/TaxClassService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@
*/

use Extcode\Cart\Domain\Model\Cart\TaxClass;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use Extcode\Cart\Domain\Model\Cart\TaxClassFactoryInterface;
use Extcode\Cart\Domain\Model\Cart\TaxClassInterface;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;

class TaxClassService implements TaxClassServiceInterface
final class TaxClassService implements TaxClassServiceInterface
{
protected array $settings;
private array $settings;

public function __construct(protected ConfigurationManager $configurationManager)
{
public function __construct(
private readonly ConfigurationManagerInterface $configurationManager,
private readonly TaxClassFactoryInterface $taxClassFactory
) {
$this->settings = $this->configurationManager->getConfiguration(
ConfigurationManager::CONFIGURATION_TYPE_FRAMEWORK,
ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
'Cart'
);
}

/**
* @inheritDoc
* @return TaxClass[]
*/
public function getTaxClasses(string $countryCode = null): array
{
Expand All @@ -49,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;
}
}
2 changes: 1 addition & 1 deletion Classes/Service/TaxClassServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
interface TaxClassServiceInterface
{
/**
* @return array<TaxClass>
* @return TaxClass[]
*/
public function getTaxClasses(string $countryCode = null): array;
}
22 changes: 7 additions & 15 deletions Classes/Utility/CartUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,19 @@
use Extcode\Cart\Domain\Model\Cart\Service;
use Extcode\Cart\Event\Cart\UpdateCountryEvent;
use Extcode\Cart\Service\SessionHandler;
use Extcode\Cart\Service\TaxClassServiceInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Request;

class CartUtility
{
protected EventDispatcherInterface $eventDispatcher;

protected SessionHandler $sessionHandler;

protected ParserUtility $parserUtility;

public function __construct(
EventDispatcherInterface $eventDispatcher,
ParserUtility $parserUtility,
SessionHandler $sessionHandler
) {
$this->eventDispatcher = $eventDispatcher;
$this->parserUtility = $parserUtility;
$this->sessionHandler = $sessionHandler;
}
protected EventDispatcherInterface $eventDispatcher,
protected ParserUtility $parserUtility,
protected TaxClassServiceInterface $taxClassService,
protected SessionHandler $sessionHandler
) {}

public function getServiceById(array $services, int $serviceId): mixed
{
Expand Down Expand Up @@ -104,7 +96,7 @@ public function getNewCart(array $configurations): Cart

$defaultCountry = $configurations['settings']['countries']['options'][$configurations['settings']['countries']['preset']]['code'];

$taxClasses = $this->parserUtility->parseTaxClasses($configurations, $defaultCountry);
$taxClasses = $this->taxClassService->getTaxClasses($defaultCountry);

/** @var Cart $cart */
$cart = GeneralUtility::makeInstance(
Expand Down
16 changes: 0 additions & 16 deletions Classes/Utility/ParserUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,10 @@
use Extcode\Cart\Domain\Model\Cart\Cart;
use Extcode\Cart\Domain\Model\Cart\Service;
use Extcode\Cart\Domain\Model\Cart\ServiceInterface;
use Extcode\Cart\Service\TaxClassService;
use Extcode\Cart\Service\TaxClassServiceInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class ParserUtility
{
public function parseTaxClasses(array $pluginSettings, string $countryCode = null): array
{
$className = $pluginSettings['taxClasses']['className'] ?? TaxClassService::class;

$service = GeneralUtility::makeInstance(
$className
);
if (!$service instanceof TaxClassServiceInterface) {
throw new \UnexpectedValueException($className . ' must implement interface ' . TaxClassServiceInterface::class, 123);
}

return $service->getTaxClasses($countryCode);
}

/**
* Parse Services
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
14 changes: 3 additions & 11 deletions Tests/Functional/Service/TaxClassServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,12 @@

class TaxClassServiceTest extends FunctionalTestCase
{
/**
* @var TaxClassService
*/
protected $taxClassService;

/**
* @var non-empty-string[]
*/
protected array $testExtensionsToLoad = [
'typo3conf/ext/cart',
];
protected TaxClassService $taxClassService;

public function setUp(): void
{
$this->testExtensionsToLoad[] = 'extcode/cart';

parent::setUp();

$this->taxClassService = GeneralUtility::makeInstance(
Expand Down
14 changes: 3 additions & 11 deletions Tests/Functional/Utility/ParserUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,12 @@

class ParserUtilityTest extends FunctionalTestCase
{
/**
* @var ParserUtility
*/
protected $parserUtility;

/**
* @var non-empty-string[]
*/
protected array $testExtensionsToLoad = [
'typo3conf/ext/cart',
];
protected ParserUtility $parserUtility;

public function setUp(): void
{
$this->testExtensionsToLoad[] = 'extcode/cart';

parent::setUp();

$this->parserUtility = GeneralUtility::makeInstance(
Expand Down

0 comments on commit 96d2919

Please sign in to comment.