Completed
Push — master ( 1bdfd1...c133b5 )
by G
03:55
created

OrderDepositTaxesApplicator::addTaxAdjustment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gweb\SyliusProductDepositPlugin\Taxation;
6
7
use Gweb\SyliusProductDepositPlugin\Entity\ProductVariantInterface;
8
use Sylius\Component\Addressing\Model\ZoneInterface;
9
use Sylius\Component\Core\Model\AdjustmentInterface;
10
use Sylius\Component\Core\Model\OrderInterface;
11
use Sylius\Component\Core\Model\TaxRate;
12
use Sylius\Component\Core\Model\OrderItemInterface;
13
use Sylius\Component\Order\Model\OrderItemUnitInterface;
14
use Sylius\Component\Core\Taxation\Applicator\OrderTaxesApplicatorInterface;
15
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface;
16
use Sylius\Component\Resource\Repository\RepositoryInterface;
17
use Sylius\Component\Taxation\Calculator\CalculatorInterface;
18
19
/**
20
 * Apply deposit tax to order item units
21
 *
22
 * @author Gerd Weitenberg <[email protected]>
23
 */
24
final class OrderDepositTaxesApplicator implements OrderTaxesApplicatorInterface
25
{
26
    /**
27
     * @var CalculatorInterface
28
     */
29
    private $calculator;
30
31
    /**
32
     * @var AdjustmentFactoryInterface
33
     */
34
    private $adjustmentFactory;
35
36
    /**
37
     * @var RepositoryInterface
38
     */
39
    private $taxRateRepository;
40
41
    /**
42
     * @param CalculatorInterface $calculator
43
     * @param AdjustmentFactoryInterface $adjustmentFactory
44
     * @param RepositoryInterface $taxRateRepository
45
     */
46
    public function __construct(
47
        CalculatorInterface $calculator,
48
        AdjustmentFactoryInterface $adjustmentFactory,
49
        RepositoryInterface $taxRateRepository
50
    ) {
51
        $this->calculator = $calculator;
52
        $this->adjustmentFactory = $adjustmentFactory;
53
        $this->taxRateRepository = $taxRateRepository;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function apply(OrderInterface $order, ZoneInterface $zone): void
60
    {
61
        /** @var OrderItemInterface $item */
62
        foreach ($order->getItems() as $item) {
63
64
            /** @var ProductVariantInterface $variant */
65
            $variant = $item->getVariant();
66
67
            $channelDeposit = $variant->getChannelDepositForChannel($order->getChannel());
0 ignored issues
show
Bug introduced by
It seems like $order->getChannel() can also be of type null; however, parameter $channel of Gweb\SyliusProductDeposi...nnelDepositForChannel() does only seem to accept Sylius\Component\Core\Model\ChannelInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
            $channelDeposit = $variant->getChannelDepositForChannel(/** @scrutinizer ignore-type */ $order->getChannel());
Loading history...
68
            if (null == $channelDeposit) {
69
                continue;
70
            }
71
72
            $depositPrice = $channelDeposit->getPrice();
73
74
            $taxCategory = $variant->getDepositTaxCategory();
75
76
            /** @var TaxRate $taxRate */
77
            $taxRate = $this->taxRateRepository->findOneBy(['category' => $taxCategory, 'zone' => $zone]);
78
            if (null == $taxRate) {
79
                continue;
80
            }
81
82
            foreach ($item->getUnits() as $unit) {
83
                $taxAmount = $this->calculator->calculate($depositPrice, $taxRate);
0 ignored issues
show
Bug introduced by
It seems like $depositPrice can also be of type null; however, parameter $base of Sylius\Component\Taxatio...rInterface::calculate() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
                $taxAmount = $this->calculator->calculate(/** @scrutinizer ignore-type */ $depositPrice, $taxRate);
Loading history...
84
                if (0.00 === $taxAmount) {
85
                    continue;
86
                }
87
88
                $this->addTaxAdjustment($unit, (int) $taxAmount, $taxRate->getLabel(), $taxRate->isIncludedInPrice());
89
            }
90
        }
91
    }
92
93
    /**
94
     * @param OrderItemUnitInterface $unit
95
     * @param int $taxAmount
96
     * @param string $label
97
     * @param bool $included
98
     */
99
    private function addTaxAdjustment(OrderItemUnitInterface $unit, int $taxAmount, string $label, bool $included): void
100
    {
101
        $unitTaxAdjustment = $this->adjustmentFactory->createWithData(
102
            AdjustmentInterface::TAX_ADJUSTMENT,
103
            $label,
104
            $taxAmount,
105
            $included
106
        );
107
        $unit->addAdjustment($unitTaxAdjustment);
108
    }
109
}
110