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

OrderShipmentTaxesApplicator::apply()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 25
rs 8.439
c 1
b 0
f 1
nc 5
cc 5
eloc 15
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\Addressing\Model\ZoneInterface;
15
use Sylius\Component\Core\Model\AdjustmentInterface;
16
use Sylius\Component\Core\Model\OrderInterface;
17
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface;
18
use Sylius\Component\Taxation\Calculator\CalculatorInterface;
19
use Sylius\Component\Taxation\Resolver\TaxRateResolverInterface;
20
21
/**
22
 * @author Mateusz Zalewski <[email protected]>
23
 * @author Mark McKelvie <[email protected]>
24
 */
25
class OrderShipmentTaxesApplicator 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
        $lastShipment = $order->getLastShipment();
60
        if (!$lastShipment) {
61
            return;
62
        }
63
64
        $shippingAdjustments = $order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT);
65
        if ($shippingAdjustments->isEmpty()) {
66
            return;
67
        }
68
69
        $taxRate = $this->taxRateResolver->resolve($lastShipment->getMethod(), ['zone' => $zone]);
70
        if (null === $taxRate) {
71
            return;
72
        }
73
74
        $lastShippingAdjustment = $shippingAdjustments->last();
75
        $taxAmount = $this->calculator->calculate($lastShippingAdjustment->getAmount(), $taxRate);
76
        if (0 === $taxAmount) {
77
            return;
78
        }
79
80
        $this->addAdjustment($order, $taxAmount, $taxRate->getLabel(), $taxRate->isIncludedInPrice());
81
    }
82
83
    /**
84
     * @param OrderInterface $order
85
     * @param int $taxAmount
86
     * @param string $label
87
     * @param bool $included
88
     */
89
    private function addAdjustment($order, $taxAmount, $label, $included)
90
    {
91
        /** @var AdjustmentInterface $shippingTaxAdjustment */
92
        $shippingTaxAdjustment = $this->adjustmentFactory->createWithData(AdjustmentInterface::TAX_ADJUSTMENT, $label, $taxAmount, $included);
93
        $order->addAdjustment($shippingTaxAdjustment);
94
    }
95
}
96