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

OrderItemUnitsTaxesApplicator::addAdjustment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 1
nc 1
cc 1
eloc 3
nop 4
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\Addressing\Model\ZoneInterface;
15
use Sylius\Component\Core\Model\AdjustmentInterface;
16
use Sylius\Component\Core\Model\OrderInterface;
17
use Sylius\Component\Core\Model\OrderItemUnitInterface;
18
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface;
19
use Sylius\Component\Taxation\Calculator\CalculatorInterface;
20
use Sylius\Component\Taxation\Resolver\TaxRateResolverInterface;
21
22
/**
23
 * @author Mark McKelvie <[email protected]>
24
 */
25
class OrderItemUnitsTaxesApplicator implements OrderTaxesApplicatorInterface
26
{
27
    /**
28
     * @var CalculatorInterface
29
     */
30
    private $calculator;
31
32
    /**
33
     * @var AdjustmentFactoryInterface
34
     */
35
    private $adjustmentFactory;
36
37
    /**
38
     * @var TaxRateResolverInterface
39
     */
40
    private $taxRateResolver;
41
42
    /**
43
     * @param CalculatorInterface $calculator
44
     * @param AdjustmentFactoryInterface $adjustmentFactory
45
     * @param TaxRateResolverInterface $taxRateResolver
46
     */
47
    public function __construct(CalculatorInterface $calculator, AdjustmentFactoryInterface $adjustmentFactory, TaxRateResolverInterface $taxRateResolver)
48
    {
49
        $this->calculator = $calculator;
50
        $this->adjustmentFactory = $adjustmentFactory;
51
        $this->taxRateResolver = $taxRateResolver;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function apply(OrderInterface $order, ZoneInterface $zone)
58
    {
59
        foreach ($order->getItems() as $item) {
60
            $quantity = $item->getQuantity();
61
            if (0 === $quantity) {
62
                continue;
63
            }
64
65
            $taxRate = $this->taxRateResolver->resolve($item->getVariant(), ['zone' => $zone]);
66
67
            if (null === $taxRate) {
68
                continue;
69
            }
70
71
            foreach ($item->getUnits() as $unit) {
72
                $taxAmount = $this->calculator->calculate($unit->getTotal(), $taxRate);
73
                if (0 === $taxAmount) {
74
                    continue;
75
                }
76
77
                $this->addAdjustment($unit, $taxAmount, $taxRate->getLabel(), $taxRate->isIncludedInPrice());
78
            }
79
        }
80
    }
81
82
    /**
83
     * @param OrderItemUnitInterface $unit
84
     * @param int $taxAmount
85
     * @param string $label
86
     * @param bool $included
87
     */
88
    private function addAdjustment(OrderItemUnitInterface $unit, $taxAmount, $label, $included)
89
    {
90
        $unitTaxAdjustment = $this->adjustmentFactory->createWithData(AdjustmentInterface::TAX_ADJUSTMENT, $label, $taxAmount, $included);
91
        $unit->addAdjustment($unitTaxAdjustment);
92
    }
93
}
94