Completed
Branch master (734dbe)
by G
12:13
created

OrderDepositProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gweb\SyliusProductDepositPlugin\Services;
6
7
use Sylius\Component\Addressing\Matcher\ZoneMatcherInterface;
8
use Sylius\Component\Addressing\Model\ZoneInterface;
9
use Sylius\Component\Core\Model\Scope;
10
use Sylius\Component\Core\Provider\ZoneProviderInterface;
11
use Sylius\Component\Order\Model\OrderInterface;
12
use Sylius\Component\Order\Processor\OrderProcessorInterface;
13
14
/**
15
 * Recalculates the order item unit price inclusive deposit
16
 *
17
 * @author Gerd Weitenberg <[email protected]>
18
 */
19
final class OrderDepositProcessor implements OrderProcessorInterface
20
{
21
    /**
22
     * @var ZoneProviderInterface
23
     */
24
    private $defaultTaxZoneProvider;
25
26
    /**
27
     * @var ZoneMatcherInterface
28
     */
29
    private $zoneMatcher;
30
    /**
31
     * @var OrderDepositTaxesApplicator
32
     */
33
    private $orderDepositTaxesApplicator;
34
35
    /**
36
     * @param ZoneProviderInterface $defaultTaxZoneProvider
37
     * @param ZoneMatcherInterface $zoneMatcher
38
     * @param OrderDepositTaxesApplicator $orderDepositTaxesApplicator
39
     */
40
    public function __construct(
41
        ZoneProviderInterface $defaultTaxZoneProvider,
42
        ZoneMatcherInterface $zoneMatcher,
43
        OrderDepositTaxesApplicator $orderDepositTaxesApplicator
44
    ) {
45
        $this->defaultTaxZoneProvider = $defaultTaxZoneProvider;
46
        $this->zoneMatcher = $zoneMatcher;
47
        $this->orderDepositTaxesApplicator = $orderDepositTaxesApplicator;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function process(OrderInterface $order): void
54
    {
55
        $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

55
        /** @scrutinizer ignore-call */ 
56
        $channel = $order->getChannel();
Loading history...
56
57
        foreach ($order->getItems() as $item) {
58
59
            if (!$item->getVariant()->hasChannelDepositForChannel($channel)) {
60
                continue;
61
            }
62
63
            $depositPrice = $item->getVariant()->getChannelDepositForChannel($channel)->getPrice();
64
65
            $item->setUnitPrice($item->getUnitPrice() + $depositPrice);
66
        }
67
68
        // apply deposit taxes
69
        $this->orderDepositTaxesApplicator->apply($order, $this->getTaxZone($order));
70
    }
71
72
    /**
73
     * @param OrderInterface $order
74
     *
75
     * @return ZoneInterface|null
76
     */
77
    private function getTaxZone(OrderInterface $order): ?ZoneInterface
78
    {
79
        $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

79
        /** @scrutinizer ignore-call */ 
80
        $shippingAddress = $order->getShippingAddress();
Loading history...
80
        $zone = null;
81
82
        if (null !== $shippingAddress) {
83
            $zone = $this->zoneMatcher->match($shippingAddress, Scope::TAX);
84
        }
85
86
        return $zone ?: $this->defaultTaxZoneProvider->getZone($order);
87
    }
88
89
}
90