Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
418 changes: 188 additions & 230 deletions Build/phpstan-baseline.neon

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Extcode\Cart;
namespace Extcode\Cart\Configuration;

/*
* This file is part of the package extcode/cart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Extcode\Cart\Service;
namespace Extcode\Cart\Configuration\Loader;

/*
* This file is part of the package extcode/cart.
Expand All @@ -11,7 +11,7 @@
* LICENSE file that was distributed with this source code.
*/

class CurrencyTranslationService implements CurrencyTranslationServiceInterface
class CurrencyTranslationLoader implements CurrencyTranslationLoaderInterface
{
public function translatePrice(float $factor, ?float $price = null): ?float
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Extcode\Cart\Service;
namespace Extcode\Cart\Configuration\Loader;

/*
* This file is part of the package extcode/cart.
Expand All @@ -14,7 +14,7 @@
/**
* @internal This class is marked internal and is not considered part of the public API. The interface will change in the next major version (v12.0.0).
*/
interface CurrencyTranslationServiceInterface
interface CurrencyTranslationLoaderInterface
{
public function translatePrice(float $factor, ?float $price = null): ?float;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Extcode\Cart\Service;
namespace Extcode\Cart\Configuration\Loader;

/*
* This file is part of the package extcode/cart.
Expand All @@ -14,7 +14,7 @@
use Extcode\Cart\Domain\Model\Cart\Cart;
use Extcode\Cart\Domain\Model\Cart\ServiceInterface;

interface PaymentMethodsServiceInterface
interface PaymentMethodsLoaderInterface
{
/**
* @return ServiceInterface[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Extcode\Cart\Service;
namespace Extcode\Cart\Configuration\Loader;

/*
* This file is part of the package extcode/cart.
Expand All @@ -14,7 +14,7 @@
use Extcode\Cart\Domain\Model\Cart\Cart;
use Extcode\Cart\Domain\Model\Cart\ServiceInterface;

interface ShippingMethodsServiceInterface
interface ShippingMethodsLoaderInterface
{
/**
* @return ServiceInterface[]
Expand Down
136 changes: 136 additions & 0 deletions Classes/Configuration/Loader/SiteSets/AbstractConfigurationLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Configuration\Loader\SiteSets;

/*
* 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 Extcode\Cart\Domain\Model\Cart\Cart;
use Extcode\Cart\Domain\Model\Cart\ServiceFactory;
use Extcode\Cart\Domain\Model\Cart\ServiceInterface;
use Extcode\Cart\Utility\Typo3GlobalsUtility;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Utility\GeneralUtility;

abstract readonly class AbstractConfigurationLoader
{
private Site $site;

public function __construct(
private ServiceFactory $serviceFactory
) {
$this->site = Typo3GlobalsUtility::getSiteFromTypo3Request();
}

/**
* @param array<int, ServiceInterface> $services
*
* @return array<int, ServiceInterface>
*/
public function getServices(?array $configurations, array $services, Cart $cart): array
{
if (is_array($configurations['options'] ?? null) === false) {
return $services;
}

foreach ($configurations['options'] as $serviceKey => $serviceConfig) {
if (is_numeric($serviceKey) === false
|| is_array($serviceConfig) === false
) {
continue;
}

$service = $this->serviceFactory->getService(
(int) $serviceKey,
$serviceConfig,
$configurations['preset'] == $serviceKey
);
$service->setCart($cart);

$services[$serviceKey] = $service;
}

return $services;
}

public function getConfigurationsForType(string $configurationType, ?string $country = null): ?array
{
$configurations = $this->site->getSettings()->getAll();
if (is_array($configurations['extcode']) === false
|| is_array($configurations['extcode'][$configurationType]) === false
) {
return null;
}

$configuration = $configurations['extcode'][$configurationType];

if (is_null($country)) {
$country = self::getCountryCodeFromPreset($configuration);
}
if (is_null($country)) {
return null;
}

if (is_array($configuration['countries'])
&& is_array($configuration['countries'][$country])
) {
return $configuration['countries'][$country];
}

if (!empty($configuration['zones'])
&& is_array($configuration['zones'])
) {
$zoneSetting = $this->getTypeZonesPluginSettings($configuration['zones'], $country);
if (!empty($zoneSetting)) {
return $zoneSetting;
}
}

return $configuration;
}

protected function getTypeZonesPluginSettings(array $zoneSettings, string $country): array
{
foreach ($zoneSettings as $zoneSetting) {
if (is_array($zoneSetting) === false
|| is_string($zoneSetting['countries']) === false
) {
continue;
}
$countriesInZones = GeneralUtility::trimExplode(',', $zoneSetting['countries'], true);

if (in_array($country, $countriesInZones)) {
return $zoneSetting;
}
}

return [];
}

private static function getCountryCodeFromPreset(array $configuration): ?string
{
if (is_array($configuration['settings']) === false
|| is_array($configuration['settings']['countries']) === false
|| is_numeric($configuration['settings']['countries']['preset']) === false
) {
return null;
}

$preset = (int) $configuration['settings']['countries']['preset'];

if (is_array($configuration['settings']['countries']['options']) === false
|| is_array($configuration['settings']['countries']['options'][$preset]) === false
|| is_string($configuration['settings']['countries']['options'][$preset]['code']) === false
) {
return null;
}

return $configuration['settings']['countries']['options'][$preset]['code'];
}
}
27 changes: 27 additions & 0 deletions Classes/Configuration/Loader/SiteSets/PaymentMethodsLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Configuration\Loader\SiteSets;

/*
* 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 Extcode\Cart\Configuration\Loader\PaymentMethodsLoaderInterface;
use Extcode\Cart\Domain\Model\Cart\Cart;

final readonly class PaymentMethodsLoader extends AbstractConfigurationLoader implements PaymentMethodsLoaderInterface
{
public function getPaymentMethods(Cart $cart): array
{
$services = [];

$configurations = $this->getConfigurationsForType('cart-payments', $cart->getBillingCountry());

return $this->getServices($configurations, $services, $cart);
}
}
27 changes: 27 additions & 0 deletions Classes/Configuration/Loader/SiteSets/ShippingMethodsLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Configuration\Loader\SiteSets;

/*
* 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 Extcode\Cart\Configuration\Loader\ShippingMethodsLoaderInterface;
use Extcode\Cart\Domain\Model\Cart\Cart;

final readonly class ShippingMethodsLoader extends AbstractConfigurationLoader implements ShippingMethodsLoaderInterface
{
public function getShippingMethods(Cart $cart): array
{
$services = [];

$configurations = $this->getConfigurationsForType('cart-shippings', $cart->getBillingCountry());

return $this->getServices($configurations, $services, $cart);
}
}
72 changes: 72 additions & 0 deletions Classes/Configuration/Loader/SiteSets/TaxClassLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Configuration\Loader\SiteSets;

/*
* 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 Extcode\Cart\Configuration\Loader\TaxClassLoaderInterface;
use Extcode\Cart\Domain\Model\Cart\TaxClass;
use Extcode\Cart\Domain\Model\Cart\TaxClassFactoryInterface;
use Extcode\Cart\Domain\Model\Cart\TaxClassInterface;
use Extcode\Cart\Utility\Typo3GlobalsUtility;
use TYPO3\CMS\Core\Site\Entity\Site;

final readonly class TaxClassLoader implements TaxClassLoaderInterface
{
private Site $site;

public function __construct(
private TaxClassFactoryInterface $taxClassFactory,
) {
$this->site = Typo3GlobalsUtility::getSiteFromTypo3Request();
}

/**
* @return TaxClass[]
*/
public function getTaxClasses(?string $countryCode = null): array
{
$taxClasses = [];

$configuration = $this->site->getSettings()->getAll();
if (is_array($configuration['extcode']) === false
|| is_array($configuration['extcode']['taxclasses']) === false
) {
return [];
}

$taxClassSettings = $configuration['extcode']['taxclasses'];
if (
is_string($countryCode)
&& array_key_exists($countryCode, $taxClassSettings)
&& is_array($taxClassSettings[$countryCode])
) {
$taxClassSettings = $taxClassSettings[$countryCode];
} elseif (
array_key_exists('fallback', $taxClassSettings)
&& is_array($taxClassSettings['fallback'])
) {
$taxClassSettings = $taxClassSettings['fallback'];
}

foreach ($taxClassSettings as $taxClassKey => $taxClassValue) {
if (is_array($taxClassValue) === false) {
continue;
}
$taxClass = $this->taxClassFactory->getTaxClass($taxClassKey, $taxClassValue);

if ($taxClass instanceof TaxClassInterface) {
$taxClasses[$taxClassKey] = $taxClass;
}
}

return $taxClasses;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Extcode\Cart\Service;
namespace Extcode\Cart\Configuration\Loader;

/*
* This file is part of the package extcode/cart.
Expand All @@ -13,7 +13,7 @@

use Extcode\Cart\Domain\Model\Cart\Cart;

interface SpecialOptionsServiceInterface
interface SpecialOptionsLoaderInterface
{
public function getSpecialOptions(Cart $cart): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Extcode\Cart\Service;
namespace Extcode\Cart\Configuration\Loader;

/*
* This file is part of the package extcode/cart.
Expand All @@ -13,7 +13,7 @@

use Extcode\Cart\Domain\Model\Cart\TaxClass;

interface TaxClassServiceInterface
interface TaxClassLoaderInterface
{
/**
* @return TaxClass[]
Expand Down
Loading
Loading