Completed
Push — master ( 1bdfd1...c133b5 )
by G
03:55
created

OrderDepositProcessor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 74
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTaxZone() 0 10 3
A process() 0 22 3
A __construct() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gweb\SyliusProductDepositPlugin\OrderProcessing;
6
7
use Gweb\SyliusProductDepositPlugin\Entity\ProductVariantInterface;
8
use Sylius\Component\Addressing\Matcher\ZoneMatcherInterface;
9
use Sylius\Component\Addressing\Model\ZoneInterface;
10
use Sylius\Component\Core\Model\OrderItemInterface;
11
use Sylius\Component\Core\Model\Scope;
12
use Sylius\Component\Core\Provider\ZoneProviderInterface;
13
use Sylius\Component\Core\Taxation\Applicator\OrderTaxesApplicatorInterface;
14
use Sylius\Component\Order\Model\OrderInterface;
15
use Sylius\Component\Order\Processor\OrderProcessorInterface;
16
17
/**
18
 * Recalculates the order item unit price inclusive deposit
19
 *
20
 * @author Gerd Weitenberg <[email protected]>
21
 */
22
final class OrderDepositProcessor implements OrderProcessorInterface
23
{
24
    /**
25
     * @var ZoneProviderInterface
26
     */
27
    private $defaultTaxZoneProvider;
28
29
    /**
30
     * @var ZoneMatcherInterface
31
     */
32
    private $zoneMatcher;
33
34
    /**
35
     * @var OrderTaxesApplicatorInterface
36
     */
37
    private $orderDepositTaxesApplicator;
38
39
    /**
40
     * @param ZoneProviderInterface $defaultTaxZoneProvider
41
     * @param ZoneMatcherInterface $zoneMatcher
42
     * @param OrderTaxesApplicatorInterface $orderDepositTaxesApplicator
43
     */
44
    public function __construct(
45
        ZoneProviderInterface $defaultTaxZoneProvider,
46
        ZoneMatcherInterface $zoneMatcher,
47
        OrderTaxesApplicatorInterface $orderDepositTaxesApplicator
48
    ) {
49
        $this->defaultTaxZoneProvider = $defaultTaxZoneProvider;
50
        $this->zoneMatcher = $zoneMatcher;
51
        $this->orderDepositTaxesApplicator = $orderDepositTaxesApplicator;
52
    }
53
54
    /**
55
     * @param \Sylius\Component\Core\Model\OrderInterface $order
56
     */
57
    public function process(OrderInterface $order): void
58
    {
59
        $channel = $order->getChannel();
0 ignored issues
show
Bug introduced by
The method getChannel() does not exist on Sylius\Component\Order\Model\OrderInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sylius\Component\Order\Model\Order. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        /** @scrutinizer ignore-call */ 
60
        $channel = $order->getChannel();
Loading history...
60
61
        /** @var OrderItemInterface $item */
62
        foreach ($order->getItems() as $item) {
63
64
            /** @var ProductVariantInterface $variant */
65
            $variant = $item->getVariant();
66
67
            $channelDeposit = $variant->getChannelDepositForChannel($channel);
68
            if (null == $channelDeposit) {
69
                continue;
70
            }
71
72
            $depositPrice = $channelDeposit->getPrice();
73
74
            $item->setUnitPrice($item->getUnitPrice() + $depositPrice);
75
        }
76
77
        // apply deposit taxes
78
        $this->orderDepositTaxesApplicator->apply($order, $this->getTaxZone($order));
79
    }
80
81
    /**
82
     * @param \Sylius\Component\Core\Model\OrderInterface $order
83
     *
84
     * @return ZoneInterface|null
85
     */
86
    private function getTaxZone(OrderInterface $order): ?ZoneInterface
87
    {
88
        $shippingAddress = $order->getShippingAddress();
0 ignored issues
show
Bug introduced by
The method getShippingAddress() does not exist on Sylius\Component\Order\Model\OrderInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sylius\Component\Order\Model\Order. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
        /** @scrutinizer ignore-call */ 
89
        $shippingAddress = $order->getShippingAddress();
Loading history...
89
        $zone = null;
90
91
        if (null !== $shippingAddress) {
92
            $zone = $this->zoneMatcher->match($shippingAddress, Scope::TAX);
93
        }
94
95
        return $zone ?: $this->defaultTaxZoneProvider->getZone($order);
96
    }
97
}
98