Passed
Push — master ( 9d9530...9d60e1 )
by G
07:54
created

OrderDepositProcessor::process()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 9
nop 1
dl 0
loc 32
rs 9.1111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewebe\SyliusProductDepositPlugin\OrderProcessing;
6
7
use Gewebe\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\Core\Model\OrderInterface;
15
use Sylius\Component\Order\Model\OrderInterface as BaseOrderInterface;
16
use Sylius\Component\Order\Processor\OrderProcessorInterface;
17
use Webmozart\Assert\Assert;
18
19
/**
20
 * Recalculates the order item unit price inclusive deposit
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
    public function process(BaseOrderInterface $order): void
55
    {
56
        Assert::isInstanceOf($order, OrderInterface::class);
57
58
        $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

58
        /** @scrutinizer ignore-call */ 
59
        $channel = $order->getChannel();
Loading history...
59
        if (null === $channel) {
60
            return;
61
        }
62
63
        /** @var OrderItemInterface $item */
64
        foreach ($order->getItems() as $item) {
65
66
            /** @var ProductVariantInterface $variant */
67
            $variant = $item->getVariant();
68
69
            $channelDeposit = $variant->getChannelDepositForChannel($channel);
70
            if (null === $channelDeposit) {
71
                continue;
72
            }
73
74
            $depositPrice = $channelDeposit->getPrice();
75
            if (null === $depositPrice) {
76
                continue;
77
            }
78
79
            $item->setUnitPrice($item->getUnitPrice() + $depositPrice);
80
        }
81
82
        // apply deposit taxes
83
        $zone = $this->getTaxZone($order);
84
        if (null !== $zone) {
85
            $this->orderDepositTaxesApplicator->apply($order, $zone);
86
        }
87
    }
88
89
    /**
90
     * @param \Sylius\Component\Core\Model\OrderInterface $order
91
     *
92
     * @return ZoneInterface|null
93
     */
94
    private function getTaxZone(OrderInterface $order): ?ZoneInterface
95
    {
96
        $shippingAddress = $order->getShippingAddress();
97
        $zone = null;
98
99
        if (null !== $shippingAddress) {
100
            $zone = $this->zoneMatcher->match($shippingAddress, Scope::TAX);
101
        }
102
103
        return $zone ?? $this->defaultTaxZoneProvider->getZone($order);
104
    }
105
}
106