Skip to content

Commit

Permalink
refactor tax rate provider
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp authored and GSadee committed Mar 16, 2021
1 parent c547939 commit e7f9af0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderItemUnitInterface;
use Sylius\RefundPlugin\Entity\AdjustmentInterface;
use Sylius\RefundPlugin\Provider\TaxRateProviderInterface;

final class LabelBasedTaxRateProviderSpec extends ObjectBehavior
final class TaxRateProviderSpec extends ObjectBehavior
{
function it_implements_tax_rate_provider_interface(): void
{
$this->shouldImplement(TaxRateProviderInterface::class);
}

function it_provides_a_tax_rate_from_tax_adjustment_label(
function it_provides_a_tax_rate_from_tax_adjustment_details(
OrderItemUnitInterface $orderItemUnit,
AdjustmentInterface $taxAdjustment
): void {
Expand All @@ -26,32 +26,33 @@ function it_provides_a_tax_rate_from_tax_adjustment_label(
->willReturn(new ArrayCollection([$taxAdjustment->getWrappedObject()]))
;

$taxAdjustment->getLabel()->willReturn('VAT (20%)');
$taxAdjustment->getDetails()->willReturn(['taxRateAmount' => 0.2]);

$this->provide($orderItemUnit)->shouldReturn('20%');
}

function it_provides_a_tax_adjustment_label_if_the_value_does_not_match_the_pattern(
OrderItemUnitInterface $orderItemUnit,
AdjustmentInterface $taxAdjustment
): void {
function it_returns_null_if_there_is_no_tax_adjustment(OrderItemUnitInterface $orderItemUnit): void
{
$orderItemUnit
->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT)
->willReturn(new ArrayCollection([$taxAdjustment->getWrappedObject()]))
->willReturn(new ArrayCollection([]))
;

$taxAdjustment->getLabel()->willReturn('20%');

$this->provide($orderItemUnit)->shouldReturn('20%');
$this->provide($orderItemUnit)->shouldReturn(null);
}

function it_returns_null_if_there_is_no_tax_adjustment(OrderItemUnitInterface $orderItemUnit): void
{
function it_returns_null_if_there_is_no_adjustment_with_details_with_tax_rate_amount(
OrderItemUnitInterface $orderItemUnit,
AdjustmentInterface $taxAdjustment

): void {
$orderItemUnit
->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT)
->willReturn(new ArrayCollection([]))
->willReturn(new ArrayCollection([$taxAdjustment->getWrappedObject()]))
;

$taxAdjustment->getDetails()->willReturn([]);

$this->provide($orderItemUnit)->shouldReturn(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,20 @@
namespace Sylius\RefundPlugin\Provider;

use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderItemUnitInterface;
use Sylius\RefundPlugin\Entity\AdjustmentInterface;

final class LabelBasedTaxRateProvider implements TaxRateProviderInterface
final class TaxRateProvider implements TaxRateProviderInterface
{
public function provide(OrderItemUnitInterface $orderItemUnit): ?string
{
/** @var Collection|AdjustmentInterface[] $taxAdjustments */
$taxAdjustments = $orderItemUnit->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT);

if ($taxAdjustments->isEmpty()) {
if ($taxAdjustments->isEmpty() || !key_exists('taxRateAmount', $taxAdjustments->first()->getDetails())) {
return null;
}

$label = $taxAdjustments->first()->getLabel();

if (preg_match('/\((.*?)\)/', $label, $matches)) {
return end($matches); // returns percent tax rate from tax adjustment label
}

return $label;
return $taxAdjustments->first()->getDetails()['taxRateAmount'] * 100 . '%';
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/services/provider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

<service
id="Sylius\RefundPlugin\Provider\TaxRateProviderInterface"
class="Sylius\RefundPlugin\Provider\LabelBasedTaxRateProvider"
class="Sylius\RefundPlugin\Provider\TaxRateProvider"
/>
</services>
</container>

0 comments on commit e7f9af0

Please sign in to comment.