Skip to content

Commit add0d49

Browse files
authored
[BUGFIX] Dependency Injection via Services.php (#757)
Rename CurrencyTranslationLoader to CurrencyTranslationService and move class back to Services because this isn't a Configuration Loader.
1 parent e861f90 commit add0d49

11 files changed

Lines changed: 71 additions & 54 deletions

File tree

Classes/Domain/Model/Cart/Cart.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* LICENSE file that was distributed with this source code.
1212
*/
1313

14-
use Extcode\Cart\Configuration\Loader\CurrencyTranslationLoaderInterface;
14+
use Extcode\Cart\Service\CurrencyTranslationServiceInterface;
1515
use InvalidArgumentException;
1616
use LogicException;
1717
use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -71,7 +71,7 @@ class Cart implements AdditionalDataInterface
7171

7272
protected string $shippingCountry = '';
7373

74-
private ?CurrencyTranslationLoaderInterface $currencyTranslationLoader = null;
74+
private ?CurrencyTranslationServiceInterface $currencyTranslationLoader = null;
7575

7676
public function __construct(
7777
protected array $taxClasses,
@@ -80,7 +80,7 @@ public function __construct(
8080
protected string $currencySign = '',
8181
protected float $currencyTranslation = 1.00
8282
) {
83-
$this->currencyTranslationLoader = GeneralUtility::makeInstance(CurrencyTranslationLoaderInterface::class);
83+
$this->currencyTranslationLoader = GeneralUtility::makeInstance(CurrencyTranslationServiceInterface::class);
8484

8585
$this->net = 0.0;
8686
$this->gross = 0.0;
@@ -983,7 +983,7 @@ public function setCurrencySign(string $currencySign): void
983983
public function translatePrice(?float $price = null): ?float
984984
{
985985
if (is_null($this->currencyTranslationLoader)) {
986-
$this->currencyTranslationLoader = GeneralUtility::makeInstance(CurrencyTranslationLoaderInterface::class);
986+
$this->currencyTranslationLoader = GeneralUtility::makeInstance(CurrencyTranslationServiceInterface::class);
987987
}
988988

989989
return $this->currencyTranslationLoader->translatePrice($this->getCurrencyTranslation(), $price);

Classes/Configuration/Loader/CurrencyTranslationLoader.php renamed to Classes/Service/CurrencyTranslationService.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\Configuration\Loader;
5+
namespace Extcode\Cart\Service;
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 CurrencyTranslationLoader implements CurrencyTranslationLoaderInterface
14+
class CurrencyTranslationService implements CurrencyTranslationServiceInterface
1515
{
1616
public function translatePrice(float $factor, ?float $price = null): ?float
1717
{

Classes/Configuration/Loader/CurrencyTranslationLoaderInterface.php renamed to Classes/Service/CurrencyTranslationServiceInterface.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\Configuration\Loader;
5+
namespace Extcode\Cart\Service;
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 CurrencyTranslationLoaderInterface
17+
interface CurrencyTranslationServiceInterface
1818
{
1919
public function translatePrice(float $factor, ?float $price = null): ?float;
2020
}

Configuration/Services.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Extcode\Cart\Configuration;
66

77
use Extcode\Cart\Hooks\ItemsProcFunc;
8+
use Extcode\Cart\Service\CurrencyTranslationService;
9+
use Extcode\Cart\Service\CurrencyTranslationServiceInterface;
810
use Symfony\Component\DependencyInjection\ContainerBuilder;
911
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1012
use TYPO3\CMS\Dashboard\Widgets\BarChartWidget;
@@ -23,17 +25,43 @@
2325
$containerConfigurator->import('Backend/Widgets/TurnoverPerDayWidget.php');
2426
}
2527

28+
$services = $containerConfigurator
29+
->services()
30+
->defaults()
31+
->autowire()
32+
->autoconfigure()
33+
;
34+
35+
$services
36+
->load(
37+
'Extcode\\Cart\\',
38+
'../Classes/*'
39+
)
40+
->exclude(
41+
[
42+
'../Classes/Widgets/*',
43+
'../Classes/Command/*',
44+
]
45+
)
46+
;
47+
48+
$services
49+
->alias(
50+
CurrencyTranslationServiceInterface::class,
51+
CurrencyTranslationService::class
52+
)
53+
->public()
54+
;
55+
2656
if (
2757
$containerBuilder->hasDefinition(ConfigurationManager::class)
2858
&& $containerBuilder->hasDefinition(FormPersistenceManager::class)
2959
) {
30-
$services = $containerConfigurator->services();
31-
3260
$services->set(ItemsProcFunc::class)
3361
->public()
3462
;
3563
}
3664

37-
$containerConfigurator->import('Services/Configuration.php');
65+
$containerConfigurator->import('Services/ConfigurationLoader.php');
3866
$containerConfigurator->import('Services/ConsoleCommands.php');
3967
};

Configuration/Services.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ services:
44
autoconfigure: true
55
public: false
66

7-
Extcode\Cart\:
8-
resource: '../Classes/*'
9-
exclude:
10-
- '../Classes/Widgets/*'
11-
- '../Classes/Command/*'
12-
137
Extcode\Cart\EventListener\Template\Components\ModifyButtonBar:
148
tags:
159
- name: event.listener

Configuration/Services/Configuration.php renamed to Configuration/Services/ConfigurationLoader.php

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

33
declare(strict_types=1);
44

5-
use Extcode\Cart\Configuration\Loader\CurrencyTranslationLoader;
6-
use Extcode\Cart\Configuration\Loader\CurrencyTranslationLoaderInterface;
75
use Extcode\Cart\Configuration\Loader\PaymentMethodsLoaderInterface;
86
use Extcode\Cart\Configuration\Loader\ShippingMethodsLoaderInterface;
97
use Extcode\Cart\Configuration\Loader\SiteSets\PaymentMethodsLoader as SiteSetsPaymentMethodsLoader;
@@ -19,20 +17,17 @@
1917
return static function (ContainerConfigurator $containerConfigurator): void {
2018
$services = $containerConfigurator
2119
->services()
22-
;
23-
24-
$services
25-
->alias(
26-
CurrencyTranslationLoaderInterface::class,
27-
CurrencyTranslationLoader::class
28-
)
20+
->defaults()
21+
->autowire()
22+
->autoconfigure()
2923
;
3024

3125
$services
3226
->alias(
3327
PaymentMethodsLoaderInterface::class,
3428
TypoScriptPaymentMethodsLoader::class
3529
)
30+
->public()
3631
;
3732

3833
$services
@@ -50,6 +45,7 @@
5045
ShippingMethodsLoaderInterface::class,
5146
TypoScriptShippingMethodsLoader::class
5247
)
48+
->public()
5349
;
5450

5551
$services
@@ -72,6 +68,7 @@
7268
TaxClassLoaderInterface::class,
7369
TypoScriptTaxClassLoader::class
7470
)
71+
->public()
7572
;
7673

7774
$services

Documentation/Changelog/12.0/Breaking-756-AddSiteSetConfigurationLoader.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ moved to `Configuration/Loader`.
1515

1616
The interfaces was moved form `Service` to `Configuration`:
1717

18-
`Classes/Service/CurrencyTranslationServiceInterface.php` => `Classes/Configuration/Loader/CurrencyTranslationLoaderInterface.php`
1918
`Classes/Service/TaxClassServiceInterface.php` => `Classes/Configuration/Loader/TaxClassLoaderInterface.php`
2019
`Classes/Service/PaymentMethodsServiceInterface.php` => `Classes/Configuration/Loader/PaymentMethodsLoaderInterface.php`
2120
`Classes/Service/ShippingMethodsServiceInterface.php` => `Classes/Configuration/Loader/ShippingMethodsLoaderInterface.php`
2221
`Classes/Service/SpecialOptionsServiceInterface.php` => `Classes/Configuration/Loader/SpecialOptionsLoaderInterface.php`
2322

2423
The classes was moved form `Service` to `Configuration` or `Configuration/TypoScript`:
2524

26-
`Classes/Service/CurrencyTranslationService.php` => `Classes/Configuration/Loader/CurrencyTranslationLoader.php`
2725
`Classes/Service/PaymentMethodsFromTypoScriptService.php` => `Classes/Configuration/Loader/TypoScript/PaymentMethodsLoader.php`
2826
`Classes/Service/ShippingMethodsFromTypoScriptService.php` => `Classes/Configuration/Loader/TypoScript/ShippingMethodsLoader.php`
2927
`Classes/Service/SpecialOptionsFromTypoScriptService.php` => `Classes/Configuration/Loader/TypoScript/SpecialOptionsLoader.php`

Tests/Unit/Domain/Model/Cart/CartCouponFixTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
* LICENSE file that was distributed with this source code.
1212
*/
1313

14-
use Extcode\Cart\Configuration\Loader\CurrencyTranslationLoader;
15-
use Extcode\Cart\Configuration\Loader\CurrencyTranslationLoaderInterface;
1614
use Extcode\Cart\Domain\Model\Cart\Cart;
1715
use Extcode\Cart\Domain\Model\Cart\CartCouponFix;
1816
use Extcode\Cart\Domain\Model\Cart\TaxClass;
17+
use Extcode\Cart\Service\CurrencyTranslationService;
18+
use Extcode\Cart\Service\CurrencyTranslationServiceInterface;
1919
use PHPUnit\Framework\Attributes\CoversClass;
2020
use PHPUnit\Framework\Attributes\Test;
2121
use PHPUnit\Framework\MockObject\MockObject;
@@ -240,8 +240,8 @@ public function isUsableReturnsFalseIfCartMinPriceIsGreaterToGivenPrice(): void
240240
private function createCartMock(array $methods = ['getGross']): Cart|MockObject
241241
{
242242
GeneralUtility::addInstance(
243-
CurrencyTranslationLoaderInterface::class,
244-
new CurrencyTranslationLoader()
243+
CurrencyTranslationServiceInterface::class,
244+
new CurrencyTranslationService()
245245
);
246246

247247
return $this->getMockBuilder(Cart::class)

Tests/Unit/Domain/Model/Cart/CartCouponPercentageTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
* LICENSE file that was distributed with this source code.
1212
*/
1313

14-
use Extcode\Cart\Configuration\Loader\CurrencyTranslationLoader;
15-
use Extcode\Cart\Configuration\Loader\CurrencyTranslationLoaderInterface;
1614
use Extcode\Cart\Domain\Model\Cart\Cart;
1715
use Extcode\Cart\Domain\Model\Cart\CartCouponPercentage;
1816
use Extcode\Cart\Domain\Model\Cart\TaxClass;
17+
use Extcode\Cart\Service\CurrencyTranslationService;
18+
use Extcode\Cart\Service\CurrencyTranslationServiceInterface;
1919
use PHPUnit\Framework\Attributes\CoversClass;
2020
use PHPUnit\Framework\Attributes\Test;
2121
use PHPUnit\Framework\MockObject\MockObject;
@@ -270,8 +270,8 @@ public function isUsableReturnsFalseIfCartMinPriceIsGreaterToGivenPrice(): void
270270
private function createCartMock(array $methods = ['getGross']): Cart|MockObject
271271
{
272272
GeneralUtility::addInstance(
273-
CurrencyTranslationLoaderInterface::class,
274-
new CurrencyTranslationLoader()
273+
CurrencyTranslationServiceInterface::class,
274+
new CurrencyTranslationService()
275275
);
276276

277277
return $this->getMockBuilder(Cart::class)

Tests/Unit/Domain/Model/Cart/CartTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
* LICENSE file that was distributed with this source code.
1212
*/
1313

14-
use Extcode\Cart\Configuration\Loader\CurrencyTranslationLoader;
15-
use Extcode\Cart\Configuration\Loader\CurrencyTranslationLoaderInterface;
1614
use Extcode\Cart\Domain\Model\Cart\Cart;
1715
use Extcode\Cart\Domain\Model\Cart\CartCouponFix;
1816
use Extcode\Cart\Domain\Model\Cart\ProductFactory;
1917
use Extcode\Cart\Domain\Model\Cart\ProductFactoryInterface;
2018
use Extcode\Cart\Domain\Model\Cart\TaxClass;
19+
use Extcode\Cart\Service\CurrencyTranslationService;
20+
use Extcode\Cart\Service\CurrencyTranslationServiceInterface;
2121
use LogicException;
2222
use PHPUnit\Framework\Attributes\CoversClass;
2323
use PHPUnit\Framework\Attributes\Test;
@@ -1315,8 +1315,8 @@ public function getSubtotalGrossReturnsSubtotalGross(): void
13151315
$couponGross = 10.00;
13161316

13171317
GeneralUtility::addInstance(
1318-
CurrencyTranslationLoaderInterface::class,
1319-
new CurrencyTranslationLoader()
1318+
CurrencyTranslationServiceInterface::class,
1319+
new CurrencyTranslationService()
13201320
);
13211321
$cart = $this->getMockBuilder(Cart::class)
13221322
->onlyMethods(['getCouponGross', 'getCurrencyTranslation'])
@@ -1353,8 +1353,8 @@ public function getSubtotalNetReturnsSubtotalNet(): void
13531353
$couponNet = $couponGross / 1.19;
13541354

13551355
GeneralUtility::addInstance(
1356-
CurrencyTranslationLoaderInterface::class,
1357-
new CurrencyTranslationLoader()
1356+
CurrencyTranslationServiceInterface::class,
1357+
new CurrencyTranslationService()
13581358
);
13591359
$cart = $this->getMockBuilder(Cart::class)
13601360
->onlyMethods(['getCouponNet', 'getCurrencyTranslation'])
@@ -1402,8 +1402,8 @@ public function getCurrencyCodeInitiallyReturnsString(): void
14021402
public function constructorSetsCurrencyCode(): void
14031403
{
14041404
GeneralUtility::addInstance(
1405-
CurrencyTranslationLoaderInterface::class,
1406-
new CurrencyTranslationLoader()
1405+
CurrencyTranslationServiceInterface::class,
1406+
new CurrencyTranslationService()
14071407
);
14081408
$cart = new Cart(
14091409
$this->taxClasses,
@@ -1455,8 +1455,8 @@ public function getCurrencySignInitiallyReturnsString(): void
14551455
public function constructorSetsCurrencySign(): void
14561456
{
14571457
GeneralUtility::addInstance(
1458-
CurrencyTranslationLoaderInterface::class,
1459-
new CurrencyTranslationLoader()
1458+
CurrencyTranslationServiceInterface::class,
1459+
new CurrencyTranslationService()
14601460
);
14611461
$cart = new Cart(
14621462
$this->taxClasses,
@@ -1508,8 +1508,8 @@ public function getCurrencyTranslationInitiallyReturnsFloat(): void
15081508
public function constructorSetsCurrencyTranslation(): void
15091509
{
15101510
GeneralUtility::addInstance(
1511-
CurrencyTranslationLoaderInterface::class,
1512-
new CurrencyTranslationLoader()
1511+
CurrencyTranslationServiceInterface::class,
1512+
new CurrencyTranslationService()
15131513
);
15141514
$cart = new Cart(
15151515
$this->taxClasses,
@@ -1603,8 +1603,8 @@ protected function addFirstProductToCarts(): void
16031603
private function createCart(bool $isNetCart): Cart
16041604
{
16051605
GeneralUtility::addInstance(
1606-
CurrencyTranslationLoaderInterface::class,
1607-
new CurrencyTranslationLoader()
1606+
CurrencyTranslationServiceInterface::class,
1607+
new CurrencyTranslationService()
16081608
);
16091609

16101610
return new Cart($this->taxClasses, $isNetCart);

0 commit comments

Comments
 (0)