OrderDepositProcessor   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 27 6
A __construct() 0 10 1
A getTaxZone() 0 10 2
A addAdjustment() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewebe\SyliusProductDepositPlugin\OrderProcessing;
6
7
use Gewebe\SyliusProductDepositPlugin\Entity\AdjustmentInterface;
8
use Gewebe\SyliusProductDepositPlugin\Entity\ProductVariantInterface;
9
use Sylius\Component\Addressing\Matcher\ZoneMatcherInterface;
10
use Sylius\Component\Addressing\Model\ZoneInterface;
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\Factory\AdjustmentFactoryInterface;
16
use Sylius\Component\Order\Model\OrderInterface as BaseOrderInterface;
17
use Sylius\Component\Order\Model\OrderItemUnitInterface;
18
use Sylius\Component\Order\Processor\OrderProcessorInterface;
19
use Webmozart\Assert\Assert;
20
21
/**
22
 * Apply deposit adjustment to order item units
23
 */
24
final class OrderDepositProcessor implements OrderProcessorInterface
25
{
26
    /** @var AdjustmentFactoryInterface */
27
    private $adjustmentFactory;
28
29
    /** @var OrderTaxesApplicatorInterface */
30
    private $orderDepositTaxesApplicator;
31
32
    /** @var ZoneMatcherInterface */
33
    private $zoneMatcher;
34
35
    /** @var ZoneProviderInterface */
36
    private $defaultTaxZoneProvider;
37
38
    public function __construct(
39
        AdjustmentFactoryInterface $adjustmentFactory,
40
        OrderTaxesApplicatorInterface $orderDepositTaxesApplicator,
41
        ZoneMatcherInterface $zoneMatcher,
42
        ZoneProviderInterface $defaultTaxZoneProvider
43
    ) {
44
        $this->adjustmentFactory = $adjustmentFactory;
45
        $this->orderDepositTaxesApplicator = $orderDepositTaxesApplicator;
46
        $this->zoneMatcher = $zoneMatcher;
47
        $this->defaultTaxZoneProvider = $defaultTaxZoneProvider;
48
    }
49
50
    public function process(BaseOrderInterface $order): void
51
    {
52
        Assert::isInstanceOf($order, OrderInterface::class);
53
54
        $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

54
        /** @scrutinizer ignore-call */ 
55
        $channel = $order->getChannel();
Loading history...
55
        if (null === $channel) {
56
            return;
57
        }
58
59
        foreach ($order->getItems() as $item) {
60
            /** @var ProductVariantInterface $variant */
61
            $variant = $item->getVariant();
62
63
            $depositPrice = $variant->getDepositPriceByChannel($channel);
64
            if (null === $depositPrice) {
65
                continue;
66
            }
67
68
            foreach ($item->getUnits() as $unit) {
69
                $this->addAdjustment($unit, $depositPrice);
70
            }
71
        }
72
73
        // apply deposit taxes
74
        $zone = $this->getTaxZone($order);
75
        if (null !== $zone) {
76
            $this->orderDepositTaxesApplicator->apply($order, $zone);
77
        }
78
    }
79
80
81
    private function addAdjustment(OrderItemUnitInterface $unit, int $amount): void
82
    {
83
        $adjustment = $this->adjustmentFactory->createWithData(
84
            AdjustmentInterface::DEPOSIT_ADJUSTMENT,
85
            'Deposit',
86
            $amount
87
        );
88
89
        $unit->addAdjustment($adjustment);
90
    }
91
92
    private function getTaxZone(OrderInterface $order): ?ZoneInterface
93
    {
94
        $shippingAddress = $order->getShippingAddress();
95
        $zone = null;
96
97
        if (null !== $shippingAddress) {
98
            $zone = $this->zoneMatcher->match($shippingAddress, Scope::TAX);
99
        }
100
101
        return $zone ?? $this->defaultTaxZoneProvider->getZone($order);
102
    }
103
}
104