Completed
Push — master ( f9ba0c...336b1b )
by Kamil
23:42
created

OrderItemsTaxesByZoneApplicator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 8
c 1
b 1
f 0
lcom 1
cbo 7
dl 0
loc 79
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B apply() 0 27 6
A addAdjustment() 0 5 1
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;
13
14
use Sylius\Bundle\CoreBundle\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
 */
26
class OrderItemsTaxesByZoneApplicator implements OrderItemsTaxesByZoneApplicatorInterface
27
{
28
    /**
29
     * @var CalculatorInterface
30
     */
31
    private $calculator;
32
33
    /**
34
     * @var AdjustmentFactoryInterface
35
     */
36
    private $adjustmentFactory;
37
38
    /**
39
     * @var IntegerDistributorInterface
40
     */
41
    private $distributor;
42
43
    /**
44
     * @var TaxRateResolverInterface
45
     */
46
    private $taxRateResolver;
47
48
    /**
49
     * @param CalculatorInterface $calculator
50
     * @param AdjustmentFactoryInterface $adjustmentFactory
51
     * @param IntegerDistributorInterface $distributor
52
     * @param TaxRateResolverInterface $taxRateResolver
53
     */
54
    public function __construct(CalculatorInterface $calculator, AdjustmentFactoryInterface $adjustmentFactory, IntegerDistributorInterface $distributor, TaxRateResolverInterface $taxRateResolver)
55
    {
56
        $this->calculator = $calculator;
57
        $this->adjustmentFactory = $adjustmentFactory;
58
        $this->distributor = $distributor;
59
        $this->taxRateResolver = $taxRateResolver;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function apply(OrderInterface $order, ZoneInterface $zone)
66
    {
67
        foreach ($order->getItems() as $item) {
68
            $quantity =  $item->getQuantity();
69
            if (0 === $quantity) {
70
                continue;
71
            }
72
73
            $taxRate = $this->taxRateResolver->resolve($item->getProduct(), array('zone' => $zone));
74
75
            if (null === $taxRate) {
76
                continue;
77
            }
78
79
            $totalTaxAmount = $this->calculator->calculate($item->getTotal(), $taxRate);
80
            $splitTaxes = $this->distributor->distribute($totalTaxAmount, $quantity);
81
82
            $units = $item->getUnits();
83
            foreach ($splitTaxes as $key => $tax) {
84
                if (0 === $tax) {
85
                    continue;
86
                }
87
88
                $this->addAdjustment($units->get($key), $tax, $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 addAdjustment(OrderItemUnitInterface $unit, $taxAmount, $label, $included)
100
    {
101
        $unitTaxAdjustment = $this->adjustmentFactory->createWithData(AdjustmentInterface::TAX_ADJUSTMENT, $label, $taxAmount, $included);
102
        $unit->addAdjustment($unitTaxAdjustment);
103
    }
104
}
105