Completed
Push — master ( 0f74a6...eca3e7 )
by Kamil
20:39
created

OrderItemsTaxesApplicator::apply()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 28
rs 8.439
c 1
b 0
f 1
nc 6
cc 6
eloc 16
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Core\Taxation\Applicator;
13
14
use Sylius\Component\Core\Distributor\IntegerDistributorInterface;
15
use Sylius\Component\Addressing\Model\ZoneInterface;
16
use Sylius\Component\Core\Model\AdjustmentInterface;
17
use Sylius\Component\Core\Model\OrderInterface;
18
use Sylius\Component\Core\Model\OrderItemUnitInterface;
19
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface;
20
use Sylius\Component\Taxation\Calculator\CalculatorInterface;
21
use Sylius\Component\Taxation\Resolver\TaxRateResolverInterface;
22
23
/**
24
 * @author Mateusz Zalewski <[email protected]>
25
 * @author Mark McKelvie <[email protected]>
26
 */
27
class OrderItemsTaxesApplicator implements OrderTaxesApplicatorInterface
28
{
29
    /**
30
     * @var CalculatorInterface
31
     */
32
    private $calculator;
33
34
    /**
35
     * @var AdjustmentFactoryInterface
36
     */
37
    private $adjustmentFactory;
38
39
    /**
40
     * @var IntegerDistributorInterface
41
     */
42
    private $distributor;
43
44
    /**
45
     * @var TaxRateResolverInterface
46
     */
47
    private $taxRateResolver;
48
49
    /**
50
     * @param CalculatorInterface $calculator
51
     * @param AdjustmentFactoryInterface $adjustmentFactory
52
     * @param IntegerDistributorInterface $distributor
53
     * @param TaxRateResolverInterface $taxRateResolver
54
     */
55
    public function __construct(CalculatorInterface $calculator, AdjustmentFactoryInterface $adjustmentFactory, IntegerDistributorInterface $distributor, TaxRateResolverInterface $taxRateResolver)
56
    {
57
        $this->calculator = $calculator;
58
        $this->adjustmentFactory = $adjustmentFactory;
59
        $this->distributor = $distributor;
60
        $this->taxRateResolver = $taxRateResolver;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function apply(OrderInterface $order, ZoneInterface $zone)
67
    {
68
        foreach ($order->getItems() as $item) {
69
            $quantity = $item->getQuantity();
70
            if (0 === $quantity) {
71
                continue;
72
            }
73
74
            $taxRate = $this->taxRateResolver->resolve($item->getVariant(), ['zone' => $zone]);
75
76
            if (null === $taxRate) {
77
                continue;
78
            }
79
80
            $totalTaxAmount = $this->calculator->calculate($item->getTotal(), $taxRate);
81
            $splitTaxes = $this->distributor->distribute($totalTaxAmount, $quantity);
82
83
            $i = 0;
84
            foreach ($item->getUnits() as $unit) {
85
                if (0 === $splitTaxes[$i]) {
86
                    continue;
87
                }
88
89
                $this->addAdjustment($unit, $splitTaxes[$i], $taxRate->getLabel(), $taxRate->isIncludedInPrice());
90
                $i++;
91
            }
92
        }
93
    }
94
95
    /**
96
     * @param OrderItemUnitInterface $unit
97
     * @param int $taxAmount
98
     * @param string $label
99
     * @param bool $included
100
     */
101
    private function addAdjustment(OrderItemUnitInterface $unit, $taxAmount, $label, $included)
102
    {
103
        $unitTaxAdjustment = $this->adjustmentFactory->createWithData(AdjustmentInterface::TAX_ADJUSTMENT, $label, $taxAmount, $included);
104
        $unit->addAdjustment($unitTaxAdjustment);
105
    }
106
}
107