AdjustmentModifier   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A modify() 0 19 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.io and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusVueStorefrontPlugin\Sylius\Modifier;
14
15
use BitBag\SyliusVueStorefrontPlugin\Sylius\Provider\AdjustmentProviderInterface;
16
use BitBag\SyliusVueStorefrontPlugin\Sylius\Provider\ChannelProviderInterface;
17
use Sylius\Component\Core\Model\AdjustmentInterface;
18
use Sylius\Component\Core\Model\OrderInterface;
19
use Sylius\Component\Core\Model\ShippingMethodInterface;
20
use Webmozart\Assert\Assert;
21
22
final class AdjustmentModifier implements AdjustmentModifierInterface
23
{
24
    /** @var ChannelProviderInterface */
25
    private $channelProvider;
26
27
    /** @var AdjustmentProviderInterface */
28
    private $adjustmentProvider;
29
30
    public function __construct(
31
        ChannelProviderInterface $channelProvider,
32
        AdjustmentProviderInterface $adjustmentProvider
33
    ) {
34
        $this->channelProvider = $channelProvider;
35
        $this->adjustmentProvider = $adjustmentProvider;
36
    }
37
38
    public function modify(OrderInterface $cart, ShippingMethodInterface $shippingMethod): void
39
    {
40
        Assert::lessThanEq($cart->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->count(), 1, sprintf('More than one shipping adjustment is applied to the cart. Only one shipment is currently supported.'));
41
42
        $adjustment = $cart->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->first();
43
44
        if (null === $adjustment) {
45
            $adjustment = $this->adjustmentProvider->provide($shippingMethod);
46
            $cart->addAdjustment($adjustment);
47
        }
48
49
        $adjustment->setType(AdjustmentInterface::SHIPPING_ADJUSTMENT);
50
        $adjustment->setLabel($shippingMethod->getName());
51
52
        $channelCode = $this->channelProvider->provide()->getCode();
53
        $configuration = $shippingMethod->getConfiguration();
54
55
        $adjustment->setAmount((int) $configuration[$channelCode]['amount']);
56
    }
57
}
58