From dc169ab47b984f6c93ec43f79cc9009683dae01d Mon Sep 17 00:00:00 2001 From: Jordan Kniest Date: Mon, 3 Jun 2024 19:37:00 +0200 Subject: [PATCH] Extract tax methods into TaxUtils --- src/FixtureHelper.php | 11 ++++++ src/Utils/SalesChannelUtils.php | 32 ------------------ src/Utils/TaxUtils.php | 60 +++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 32 deletions(-) create mode 100644 src/Utils/TaxUtils.php diff --git a/src/FixtureHelper.php b/src/FixtureHelper.php index 10d3ca6..5336da3 100644 --- a/src/FixtureHelper.php +++ b/src/FixtureHelper.php @@ -14,6 +14,7 @@ use Basecom\FixturePlugin\Utils\SalesChannelUtils; use Basecom\FixturePlugin\Utils\SalutationUtils; use Basecom\FixturePlugin\Utils\ShippingMethodUtils; +use Basecom\FixturePlugin\Utils\TaxUtils; readonly class FixtureHelper { @@ -28,6 +29,7 @@ public function __construct( private DatabaseUtils $databaseUtils, private LanguageAndLocaleUtils $languageAndLocaleUtils, private CurrencyUtils $currencyUtils, + private TaxUtils $taxUtils, ) { } @@ -112,6 +114,15 @@ public function Currency(): CurrencyUtils return $this->currencyUtils; } + /** + * Use this to access the tax related features + * of the fixture helper class. + */ + public function Tax(): TaxUtils + { + return $this->taxUtils; + } + /** * Use this to access the general database helper functions * of the fixture helper class. diff --git a/src/Utils/SalesChannelUtils.php b/src/Utils/SalesChannelUtils.php index dcc3b12..a11ad6a 100644 --- a/src/Utils/SalesChannelUtils.php +++ b/src/Utils/SalesChannelUtils.php @@ -11,8 +11,6 @@ use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; use Shopware\Core\System\SalesChannel\SalesChannelCollection; use Shopware\Core\System\SalesChannel\SalesChannelEntity; -use Shopware\Core\System\Tax\TaxCollection; -use Shopware\Core\System\Tax\TaxEntity; /** * This class provides utility methods to work with sales channels. It has build in caching to prevent @@ -27,11 +25,9 @@ { /** * @param EntityRepository $salesChannelRepository - * @param EntityRepository $taxRepository */ public function __construct( private EntityRepository $salesChannelRepository, - private EntityRepository $taxRepository, ) { } @@ -75,32 +71,4 @@ public function getSalesChannelByType(string $salesChannelType): ?SalesChannelEn return $salesChannel instanceof SalesChannelEntity ? $salesChannel : null; }); } - - // TODO: Move to TaxUtils - public function getTax19(): ?TaxEntity - { - $criteria = (new Criteria()) - ->addFilter(new EqualsFilter('taxRate', 19)) - ->setLimit(1); - - $tax = $this->taxRepository - ->search($criteria, Context::createDefaultContext()) - ->first(); - - return $tax instanceof TaxEntity ? $tax : null; - } - - // TODO: Move to TaxUtils - public function getTax(float $taxValue): ?TaxEntity - { - $criteria = (new Criteria()) - ->addFilter(new EqualsFilter('taxRate', $taxValue)) - ->setLimit(1); - - $tax = $this->taxRepository - ->search($criteria, Context::createDefaultContext()) - ->first(); - - return $tax instanceof TaxEntity ? $tax : null; - } } diff --git a/src/Utils/TaxUtils.php b/src/Utils/TaxUtils.php new file mode 100644 index 0000000..a8388ee --- /dev/null +++ b/src/Utils/TaxUtils.php @@ -0,0 +1,60 @@ +helper->Tax()->……(); + * ``` + */ +class TaxUtils +{ + /** + * @param EntityRepository $taxRepository + */ + public function __construct( + private EntityRepository $taxRepository, + ) { + } + + /** + * Return the tax entity with a tax rate of 19% or null if none exists. + */ + public function getTax19(): ?TaxEntity + { + return $this->getTax(19); + } + + /** + * Return a tax entity with a specific tax rate or null if none exists. + */ + public function getTax(float $taxRate): ?TaxEntity + { + return once(function () use ($taxRate): ?TaxEntity { + $criteria = (new Criteria()) + ->addFilter(new EqualsFilter('taxRate', $taxRate)) + ->setLimit(1); + + $criteria->setTitle(sprintf('%s::%s()', __CLASS__, __FUNCTION__)); + + $tax = $this->taxRepository + ->search($criteria, Context::createDefaultContext()) + ->first(); + + return $tax instanceof TaxEntity ? $tax : null; + }); + } +}