Skip to content

Commit

Permalink
[TASK] Create TaxClasses by TaxClassFactory
Browse files Browse the repository at this point in the history
Relates: #505
  • Loading branch information
extcode committed May 31, 2024
1 parent c3033b7 commit f1c08ae
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 48 deletions.
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;
}
39 changes: 10 additions & 29 deletions Classes/Service/TaxClassService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
*/

use Extcode\Cart\Domain\Model\Cart\TaxClass;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
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 ConfigurationManagerInterface $configurationManager
private readonly ConfigurationManagerInterface $configurationManager,
private readonly TaxClassFactoryInterface $taxClassFactory
) {
$this->settings = $this->configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
Expand Down Expand Up @@ -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;
}
}

0 comments on commit f1c08ae

Please sign in to comment.