SetShippingInformationHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A __invoke() 0 25 1
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\CommandHandler\Cart;
14
15
use BitBag\SyliusVueStorefrontPlugin\Command\Cart\SetShippingInformation;
16
use BitBag\SyliusVueStorefrontPlugin\Sylius\Handler\ShipmentHandlerInterface;
17
use Sylius\Component\Core\Model\OrderInterface;
18
use Sylius\Component\Core\Model\ShippingMethodInterface;
19
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
20
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
21
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
22
use Webmozart\Assert\Assert;
23
24
final class SetShippingInformationHandler implements MessageHandlerInterface
25
{
26
    /** @var OrderRepositoryInterface */
27
    private $orderRepository;
28
29
    /** @var ShippingMethodRepositoryInterface $shippingMethodRepository */
30
    private $shippingMethodRepository;
31
32
    /** @var ShipmentHandlerInterface */
33
    private $shipmentHandler;
34
35
    public function __construct(
36
        OrderRepositoryInterface $orderRepository,
37
        ShippingMethodRepositoryInterface $shippingMethodRepository,
38
        ShipmentHandlerInterface $shipmentHandler
39
    ) {
40
        $this->orderRepository = $orderRepository;
41
        $this->shippingMethodRepository = $shippingMethodRepository;
42
        $this->shipmentHandler = $shipmentHandler;
43
    }
44
45
    public function __invoke(SetShippingInformation $setShippingInformation): void
46
    {
47
        /** @var OrderInterface $cart */
48
        $cart = $this->orderRepository->findOneBy([
49
            'tokenValue' => $setShippingInformation->cartId(),
50
            'shippingState' => OrderInterface::STATE_CART,
51
        ]);
52
53
        Assert::notNull($cart, sprintf('Cart with token value of %s has not been found.', $setShippingInformation->cartId()));
54
55
        /** @var ShippingMethodInterface $shippingMethod */
56
        $shippingMethod = $this->shippingMethodRepository->findOneBy([
57
            'code' => $setShippingInformation->addressInformation()->getShippingCarrierCode(),
58
            'enabled' => 1,
59
        ]);
60
61
        Assert::notNull(
62
            $shippingMethod,
63
            sprintf('Shipping method with code value of %s has not been found.',
64
                $setShippingInformation->addressInformation()->getShippingCarrierCode()
65
            )
66
        );
67
68
        $this->shipmentHandler->handle($cart, $shippingMethod);
69
    }
70
}
71