Skip to content

Commit 25c55a4

Browse files
committed
[TASK] Add SiteSets as default configuration for cart
1 parent 7237470 commit 25c55a4

52 files changed

Lines changed: 950 additions & 457 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Build/phpstan-baseline.neon

Lines changed: 188 additions & 230 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Extcode\Cart;
5+
namespace Extcode\Cart\Configuration;
66

77
/*
88
* This file is part of the package extcode/cart

Classes/Service/CurrencyTranslationService.php renamed to Classes/Configuration/Loader/CurrencyTranslationLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Extcode\Cart\Service;
5+
namespace Extcode\Cart\Configuration\Loader;
66

77
/*
88
* This file is part of the package extcode/cart.
@@ -11,7 +11,7 @@
1111
* LICENSE file that was distributed with this source code.
1212
*/
1313

14-
class CurrencyTranslationService implements CurrencyTranslationServiceInterface
14+
class CurrencyTranslationLoader implements CurrencyTranslationLoaderInterface
1515
{
1616
public function translatePrice(float $factor, ?float $price = null): ?float
1717
{

Classes/Service/CurrencyTranslationServiceInterface.php renamed to Classes/Configuration/Loader/CurrencyTranslationLoaderInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Extcode\Cart\Service;
5+
namespace Extcode\Cart\Configuration\Loader;
66

77
/*
88
* This file is part of the package extcode/cart.
@@ -14,7 +14,7 @@
1414
/**
1515
* @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).
1616
*/
17-
interface CurrencyTranslationServiceInterface
17+
interface CurrencyTranslationLoaderInterface
1818
{
1919
public function translatePrice(float $factor, ?float $price = null): ?float;
2020
}

Classes/Service/PaymentMethodsServiceInterface.php renamed to Classes/Configuration/Loader/PaymentMethodsLoaderInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Extcode\Cart\Service;
5+
namespace Extcode\Cart\Configuration\Loader;
66

77
/*
88
* This file is part of the package extcode/cart.
@@ -14,7 +14,7 @@
1414
use Extcode\Cart\Domain\Model\Cart\Cart;
1515
use Extcode\Cart\Domain\Model\Cart\ServiceInterface;
1616

17-
interface PaymentMethodsServiceInterface
17+
interface PaymentMethodsLoaderInterface
1818
{
1919
/**
2020
* @return ServiceInterface[]

Classes/Service/ShippingMethodsServiceInterface.php renamed to Classes/Configuration/Loader/ShippingMethodsLoaderInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Extcode\Cart\Service;
5+
namespace Extcode\Cart\Configuration\Loader;
66

77
/*
88
* This file is part of the package extcode/cart.
@@ -14,7 +14,7 @@
1414
use Extcode\Cart\Domain\Model\Cart\Cart;
1515
use Extcode\Cart\Domain\Model\Cart\ServiceInterface;
1616

17-
interface ShippingMethodsServiceInterface
17+
interface ShippingMethodsLoaderInterface
1818
{
1919
/**
2020
* @return ServiceInterface[]
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Extcode\Cart\Configuration\Loader\SiteSets;
6+
7+
/*
8+
* This file is part of the package extcode/cart.
9+
*
10+
* For the full copyright and license information, please read the
11+
* LICENSE file that was distributed with this source code.
12+
*/
13+
14+
use Extcode\Cart\Domain\Model\Cart\Cart;
15+
use Extcode\Cart\Domain\Model\Cart\ServiceFactory;
16+
use Extcode\Cart\Domain\Model\Cart\ServiceInterface;
17+
use Extcode\Cart\Utility\Typo3GlobalsUtility;
18+
use TYPO3\CMS\Core\Site\Entity\Site;
19+
use TYPO3\CMS\Core\Utility\GeneralUtility;
20+
21+
abstract readonly class AbstractConfigurationLoader
22+
{
23+
private Site $site;
24+
25+
public function __construct(
26+
private ServiceFactory $serviceFactory
27+
) {
28+
$this->site = Typo3GlobalsUtility::getSiteFromTypo3Request();
29+
}
30+
31+
/**
32+
* @param array<int, ServiceInterface> $services
33+
*
34+
* @return array<int, ServiceInterface>
35+
*/
36+
public function getServices(?array $configurations, array $services, Cart $cart): array
37+
{
38+
if (is_array($configurations['options'] ?? null) === false) {
39+
return $services;
40+
}
41+
42+
foreach ($configurations['options'] as $serviceKey => $serviceConfig) {
43+
if (is_numeric($serviceKey) === false
44+
|| is_array($serviceConfig) === false
45+
) {
46+
continue;
47+
}
48+
49+
$service = $this->serviceFactory->getService(
50+
(int) $serviceKey,
51+
$serviceConfig,
52+
$configurations['preset'] == $serviceKey
53+
);
54+
$service->setCart($cart);
55+
56+
$services[$serviceKey] = $service;
57+
}
58+
59+
return $services;
60+
}
61+
62+
public function getConfigurationsForType(string $configurationType, ?string $country = null): ?array
63+
{
64+
$configurations = $this->site->getSettings()->getAll();
65+
if (is_array($configurations['extcode']) === false
66+
|| is_array($configurations['extcode'][$configurationType]) === false
67+
) {
68+
return null;
69+
}
70+
71+
$configuration = $configurations['extcode'][$configurationType];
72+
73+
if (is_null($country)) {
74+
$country = self::getCountryCodeFromPreset($configuration);
75+
}
76+
if (is_null($country)) {
77+
return null;
78+
}
79+
80+
if (is_array($configuration['countries'])
81+
&& is_array($configuration['countries'][$country])
82+
) {
83+
return $configuration['countries'][$country];
84+
}
85+
86+
if (!empty($configuration['zones'])
87+
&& is_array($configuration['zones'])
88+
) {
89+
$zoneSetting = $this->getTypeZonesPluginSettings($configuration['zones'], $country);
90+
if (!empty($zoneSetting)) {
91+
return $zoneSetting;
92+
}
93+
}
94+
95+
return $configuration;
96+
}
97+
98+
protected function getTypeZonesPluginSettings(array $zoneSettings, string $country): array
99+
{
100+
foreach ($zoneSettings as $zoneSetting) {
101+
if (is_array($zoneSetting) === false
102+
|| is_string($zoneSetting['countries']) === false
103+
) {
104+
continue;
105+
}
106+
$countriesInZones = GeneralUtility::trimExplode(',', $zoneSetting['countries'], true);
107+
108+
if (in_array($country, $countriesInZones)) {
109+
return $zoneSetting;
110+
}
111+
}
112+
113+
return [];
114+
}
115+
116+
private static function getCountryCodeFromPreset(array $configuration): ?string
117+
{
118+
if (is_array($configuration['settings']) === false
119+
|| is_array($configuration['settings']['countries']) === false
120+
|| is_numeric($configuration['settings']['countries']['preset']) === false
121+
) {
122+
return null;
123+
}
124+
125+
$preset = (int) $configuration['settings']['countries']['preset'];
126+
127+
if (is_array($configuration['settings']['countries']['options']) === false
128+
|| is_array($configuration['settings']['countries']['options'][$preset]) === false
129+
|| is_string($configuration['settings']['countries']['options'][$preset]['code']) === false
130+
) {
131+
return null;
132+
}
133+
134+
return $configuration['settings']['countries']['options'][$preset]['code'];
135+
}
136+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Extcode\Cart\Configuration\Loader\SiteSets;
6+
7+
/*
8+
* This file is part of the package extcode/cart.
9+
*
10+
* For the full copyright and license information, please read the
11+
* LICENSE file that was distributed with this source code.
12+
*/
13+
14+
use Extcode\Cart\Configuration\Loader\PaymentMethodsLoaderInterface;
15+
use Extcode\Cart\Domain\Model\Cart\Cart;
16+
17+
final readonly class PaymentMethodsLoader extends AbstractConfigurationLoader implements PaymentMethodsLoaderInterface
18+
{
19+
public function getPaymentMethods(Cart $cart): array
20+
{
21+
$services = [];
22+
23+
$configurations = $this->getConfigurationsForType('cart-payments', $cart->getBillingCountry());
24+
25+
return $this->getServices($configurations, $services, $cart);
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Extcode\Cart\Configuration\Loader\SiteSets;
6+
7+
/*
8+
* This file is part of the package extcode/cart.
9+
*
10+
* For the full copyright and license information, please read the
11+
* LICENSE file that was distributed with this source code.
12+
*/
13+
14+
use Extcode\Cart\Configuration\Loader\ShippingMethodsLoaderInterface;
15+
use Extcode\Cart\Domain\Model\Cart\Cart;
16+
17+
final readonly class ShippingMethodsLoader extends AbstractConfigurationLoader implements ShippingMethodsLoaderInterface
18+
{
19+
public function getShippingMethods(Cart $cart): array
20+
{
21+
$services = [];
22+
23+
$configurations = $this->getConfigurationsForType('cart-shippings', $cart->getBillingCountry());
24+
25+
return $this->getServices($configurations, $services, $cart);
26+
}
27+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Extcode\Cart\Configuration\Loader\SiteSets;
6+
7+
/*
8+
* This file is part of the package extcode/cart.
9+
*
10+
* For the full copyright and license information, please read the
11+
* LICENSE file that was distributed with this source code.
12+
*/
13+
14+
use Extcode\Cart\Configuration\Loader\TaxClassLoaderInterface;
15+
use Extcode\Cart\Domain\Model\Cart\TaxClass;
16+
use Extcode\Cart\Domain\Model\Cart\TaxClassFactoryInterface;
17+
use Extcode\Cart\Domain\Model\Cart\TaxClassInterface;
18+
use Extcode\Cart\Utility\Typo3GlobalsUtility;
19+
use TYPO3\CMS\Core\Site\Entity\Site;
20+
21+
final readonly class TaxClassLoader implements TaxClassLoaderInterface
22+
{
23+
private Site $site;
24+
25+
public function __construct(
26+
private TaxClassFactoryInterface $taxClassFactory,
27+
) {
28+
$this->site = Typo3GlobalsUtility::getSiteFromTypo3Request();
29+
}
30+
31+
/**
32+
* @return TaxClass[]
33+
*/
34+
public function getTaxClasses(?string $countryCode = null): array
35+
{
36+
$taxClasses = [];
37+
38+
$configuration = $this->site->getSettings()->getAll();
39+
if (is_array($configuration['extcode']) === false
40+
|| is_array($configuration['extcode']['taxclasses']) === false
41+
) {
42+
return [];
43+
}
44+
45+
$taxClassSettings = $configuration['extcode']['taxclasses'];
46+
if (
47+
is_string($countryCode)
48+
&& array_key_exists($countryCode, $taxClassSettings)
49+
&& is_array($taxClassSettings[$countryCode])
50+
) {
51+
$taxClassSettings = $taxClassSettings[$countryCode];
52+
} elseif (
53+
array_key_exists('fallback', $taxClassSettings)
54+
&& is_array($taxClassSettings['fallback'])
55+
) {
56+
$taxClassSettings = $taxClassSettings['fallback'];
57+
}
58+
59+
foreach ($taxClassSettings as $taxClassKey => $taxClassValue) {
60+
if (is_array($taxClassValue) === false) {
61+
continue;
62+
}
63+
$taxClass = $this->taxClassFactory->getTaxClass($taxClassKey, $taxClassValue);
64+
65+
if ($taxClass instanceof TaxClassInterface) {
66+
$taxClasses[$taxClassKey] = $taxClass;
67+
}
68+
}
69+
70+
return $taxClasses;
71+
}
72+
}

0 commit comments

Comments
 (0)