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

OrderTaxesApplicator::processTaxes()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 13
nc 5
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;
13
14
use Sylius\Component\Addressing\Matcher\ZoneMatcherInterface;
15
use Sylius\Component\Addressing\Model\AddressInterface;
16
use Sylius\Component\Addressing\Model\ZoneInterface;
17
use Sylius\Component\Core\Model\AdjustmentInterface;
18
use Sylius\Component\Core\Model\OrderInterface;
19
use Sylius\Component\Core\Provider\ZoneProviderInterface;
20
21
/**
22
 * @author Paweł Jędrzejewski <[email protected]>
23
 * @author Mateusz Zalewski <[email protected]>
24
 */
25
class OrderTaxesApplicator implements OrderTaxesApplicatorInterface
26
{
27
    /**
28
     * @var ZoneProviderInterface
29
     */
30
    protected $defaultTaxZoneProvider;
31
32
    /**
33
     * @var OrderShipmentTaxesByZoneApplicatorInterface
34
     */
35
    protected $orderShipmentTaxesApplicator;
36
37
    /**
38
     * @var OrderItemsTaxesByZoneApplicatorInterface
39
     */
40
    protected $orderItemsTaxesApplicator;
41
42
    /**
43
     * @var ZoneMatcherInterface
44
     */
45
    protected $zoneMatcher;
46
47
    /**
48
     * @param ZoneProviderInterface $defaultTaxZoneProvider
49
     * @param OrderShipmentTaxesByZoneApplicatorInterface $orderShipmentTaxesApplicator
50
     * @param OrderItemsTaxesByZoneApplicatorInterface $orderItemsTaxesApplicator
51
     * @param ZoneMatcherInterface $zoneMatcher
52
     */
53
    public function __construct(
54
        ZoneProviderInterface $defaultTaxZoneProvider,
55
        OrderShipmentTaxesByZoneApplicatorInterface $orderShipmentTaxesApplicator,
56
        OrderItemsTaxesByZoneApplicatorInterface $orderItemsTaxesApplicator,
57
        ZoneMatcherInterface $zoneMatcher
58
    ) {
59
        $this->defaultTaxZoneProvider = $defaultTaxZoneProvider;
60
        $this->orderShipmentTaxesApplicator = $orderShipmentTaxesApplicator;
61
        $this->orderItemsTaxesApplicator = $orderItemsTaxesApplicator;
62
        $this->zoneMatcher = $zoneMatcher;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function apply(OrderInterface $order)
69
    {
70
        $this->clearTaxes($order);
71
        if ($order->isEmpty()) {
72
            return;
73
        }
74
75
        $zone = $this->getTaxZone($order->getShippingAddress());
76
77
        if (null === $zone) {
78
            return;
79
        }
80
81
        $this->orderItemsTaxesApplicator->apply($order, $zone);
82
        $this->orderShipmentTaxesApplicator->apply($order, $zone);
83
    }
84
85
    /**
86
     * @param AddressInterface|null $shippingAddress
87
     *
88
     * @return ZoneInterface|null
89
     */
90
    private function getTaxZone(AddressInterface $shippingAddress = null)
91
    {
92
        $zone = null;
93
        if (null !== $shippingAddress) {
94
            $zone = $this->zoneMatcher->match($shippingAddress);
95
        }
96
97
        return $zone ?: $this->defaultTaxZoneProvider->getZone();
98
    }
99
100
    /**
101
     * @param OrderInterface $order
102
     */
103
    private function clearTaxes(OrderInterface $order)
104
    {
105
        $order->removeAdjustments(AdjustmentInterface::TAX_ADJUSTMENT);
106
        foreach ($order->getItems() as $item) {
107
            $item->removeAdjustmentsRecursively(AdjustmentInterface::TAX_ADJUSTMENT);
108
        }
109
    }
110
}
111