SetShippingInformationAction   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 6
dl 0
loc 87
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 1
A __invoke() 0 35 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\Controller\Cart;
14
15
use BitBag\SyliusVueStorefrontPlugin\Command\Cart\SetShippingInformation;
16
use BitBag\SyliusVueStorefrontPlugin\Factory\Cart\ShippingInformationViewFactoryInterface;
17
use BitBag\SyliusVueStorefrontPlugin\Factory\GenericSuccessViewFactoryInterface;
18
use BitBag\SyliusVueStorefrontPlugin\Factory\ValidationErrorViewFactoryInterface;
19
use BitBag\SyliusVueStorefrontPlugin\Processor\RequestProcessorInterface;
20
use BitBag\SyliusVueStorefrontPlugin\Sylius\Provider\ChannelProviderInterface;
21
use FOS\RestBundle\View\View;
22
use FOS\RestBundle\View\ViewHandlerInterface;
23
use Sylius\Component\Core\Model\ChannelInterface;
24
use Sylius\Component\Core\Model\OrderInterface;
25
use Sylius\Component\Core\Model\PaymentMethodInterface;
26
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
27
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpFoundation\Response;
30
use Symfony\Component\Messenger\MessageBusInterface;
31
32
final class SetShippingInformationAction
33
{
34
    /** @var RequestProcessorInterface */
35
    private $setShippingInformationRequestProcessor;
36
37
    /** @var MessageBusInterface */
38
    private $bus;
39
40
    /** @var ViewHandlerInterface */
41
    private $viewHandler;
42
43
    /** @var ValidationErrorViewFactoryInterface */
44
    private $validationErrorViewFactory;
45
46
    /** @var OrderRepositoryInterface */
47
    private $orderRepository;
48
49
    /** @var PaymentMethodRepositoryInterface */
50
    private $paymentMethodRepository;
51
52
    /** @var ChannelProviderInterface */
53
    private $channelProvider;
54
55
    /** @var GenericSuccessViewFactoryInterface */
56
    private $genericSuccessViewFactory;
57
58
    /** @var ShippingInformationViewFactoryInterface */
59
    private $shippingInformationViewFactory;
60
61
    public function __construct(
62
        RequestProcessorInterface $setShippingInformationRequestProcessor,
63
        MessageBusInterface $bus,
64
        ViewHandlerInterface $viewHandler,
65
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
66
        OrderRepositoryInterface $orderRepository,
67
        PaymentMethodRepositoryInterface $paymentMethodRepository,
68
        ChannelProviderInterface $channelProvider,
69
        GenericSuccessViewFactoryInterface $genericSuccessViewFactory,
70
        ShippingInformationViewFactoryInterface $shippingInformationViewFactory
71
    ) {
72
        $this->setShippingInformationRequestProcessor = $setShippingInformationRequestProcessor;
73
        $this->bus = $bus;
74
        $this->viewHandler = $viewHandler;
75
        $this->validationErrorViewFactory = $validationErrorViewFactory;
76
        $this->orderRepository = $orderRepository;
77
        $this->paymentMethodRepository = $paymentMethodRepository;
78
        $this->channelProvider = $channelProvider;
79
        $this->genericSuccessViewFactory = $genericSuccessViewFactory;
80
        $this->shippingInformationViewFactory = $shippingInformationViewFactory;
81
    }
82
83
    public function __invoke(Request $request): Response
84
    {
85
        $validationResults = $this->setShippingInformationRequestProcessor->validate($request);
86
87
        if (0 !== count($validationResults)) {
88
            return $this->viewHandler->handle(View::create(
89
                $this->validationErrorViewFactory->create($validationResults),
90
                Response::HTTP_BAD_REQUEST
91
            ));
92
        }
93
94
        /** @var SetShippingInformation $setShippingInformationCommand */
95
        $setShippingInformationCommand = $this->setShippingInformationRequestProcessor->getCommand($request);
96
97
        $this->bus->dispatch($setShippingInformationCommand);
98
99
        /** @var OrderInterface $cart */
100
        $cart = $this->orderRepository->findOneBy([
101
            'tokenValue' => $setShippingInformationCommand->cartId(),
102
            'shippingState' => OrderInterface::STATE_CART,
103
        ]);
104
105
        /** @var ChannelInterface $channel */
106
        $channel = $this->channelProvider->provide();
107
108
        /** @var PaymentMethodInterface[] $paymentMethods */
109
        $paymentMethods = $this->paymentMethodRepository->findEnabledForChannel($channel);
110
111
        return $this->viewHandler->handle(View::create(
112
            $this->genericSuccessViewFactory->create(
113
                $this->shippingInformationViewFactory->create($paymentMethods, $cart)
114
            ),
115
            Response::HTTP_OK
116
        ));
117
    }
118
}
119