Completed
Push — remove-content-bundle ( 1b4274...9f7fdd )
by Kamil
36:32 queued 17:56
created

OrderShipmentTaxesApplicator::getShippingMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 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\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\ShipmentInterface;
18
use Sylius\Component\Core\Model\ShippingMethodInterface;
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 OrderShipmentTaxesApplicator implements OrderTaxesApplicatorInterface
28
{
29
    /**
30
     * @var CalculatorInterface
31
     */
32
    private $calculator;
33
34
    /**
35
     * @var AdjustmentFactoryInterface
36
     */
37
    private $adjustmentFactory;
38
39
    /**
40
     * @var TaxRateResolverInterface
41
     */
42
    private $taxRateResolver;
43
44
    /**
45
     * @param CalculatorInterface $calculator
46
     * @param AdjustmentFactoryInterface $adjustmentFactory
47
     * @param TaxRateResolverInterface $taxRateResolver
48
     */
49
    public function __construct(
50
        CalculatorInterface $calculator,
51
        AdjustmentFactoryInterface $adjustmentFactory,
52
        TaxRateResolverInterface $taxRateResolver
53
    ) {
54
        $this->calculator = $calculator;
55
        $this->adjustmentFactory = $adjustmentFactory;
56
        $this->taxRateResolver = $taxRateResolver;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function apply(OrderInterface $order, ZoneInterface $zone)
63
    {
64
        $shippingTotal = $order->getShippingTotal();
65
        if (0 === $shippingTotal) {
66
            return;
67
        }
68
69
        $taxRate = $this->taxRateResolver->resolve($this->getShippingMethod($order), ['zone' => $zone]);
70
        if (null === $taxRate) {
71
            return;
72
        }
73
74
        $taxAmount = $this->calculator->calculate($shippingTotal, $taxRate);
75
        if (0 === $taxAmount) {
76
            return;
77
        }
78
79
        $this->addAdjustment($order, $taxAmount, $taxRate->getLabel(), $taxRate->isIncludedInPrice());
80
    }
81
82
    /**
83
     * @param OrderInterface $order
84
     * @param int $taxAmount
85
     * @param string $label
86
     * @param bool $included
87
     */
88
    private function addAdjustment(OrderInterface $order, $taxAmount, $label, $included)
89
    {
90
        /** @var AdjustmentInterface $shippingTaxAdjustment */
91
        $shippingTaxAdjustment = $this->adjustmentFactory
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $shippingTaxAdjustment exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
92
            ->createWithData(AdjustmentInterface::TAX_ADJUSTMENT, $label, $taxAmount, $included)
93
        ;
94
        $order->addAdjustment($shippingTaxAdjustment);
95
    }
96
97
    /**
98
     * @param OrderInterface $order
99
     *
100
     * @return ShippingMethodInterface
101
     */
102
    private function getShippingMethod(OrderInterface $order)
103
    {
104
        /** @var ShipmentInterface $shipment */
105
        $shipment = $order->getShipments()->first();
106
        if (false === $shipment) {
107
            throw new \LogicException('Order should have at least one shipment.');
108
        }
109
110
        return $shipment->getMethod();
111
    }
112
}
113