Skip to content

Commit

Permalink
Extract tax methods into TaxUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
jkniest committed Jun 3, 2024
1 parent 8fa0481 commit dc169ab
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 32 deletions.
11 changes: 11 additions & 0 deletions src/FixtureHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -28,6 +29,7 @@ public function __construct(
private DatabaseUtils $databaseUtils,
private LanguageAndLocaleUtils $languageAndLocaleUtils,
private CurrencyUtils $currencyUtils,
private TaxUtils $taxUtils,
) {
}

Expand Down Expand Up @@ -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.
Expand Down
32 changes: 0 additions & 32 deletions src/Utils/SalesChannelUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,11 +25,9 @@
{
/**
* @param EntityRepository<SalesChannelCollection> $salesChannelRepository
* @param EntityRepository<TaxCollection> $taxRepository
*/
public function __construct(
private EntityRepository $salesChannelRepository,
private EntityRepository $taxRepository,
) {
}

Expand Down Expand Up @@ -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;
}
}
60 changes: 60 additions & 0 deletions src/Utils/TaxUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Basecom\FixturePlugin\Utils;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\Tax\TaxCollection;
use Shopware\Core\System\Tax\TaxEntity;

/**
* This class provides utility methods to work with taxes. It has build in caching
* to prevent multiple database queries for the same data within one command execution / request.
*
* This class is designed to be used through the FixtureHelper, using:
* ```php
* $this->helper->Tax()->……();
* ```
*/
class TaxUtils
{
/**
* @param EntityRepository<TaxCollection> $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;
});
}
}

0 comments on commit dc169ab

Please sign in to comment.