GetShippingMethodsAction::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Factory\Cart\ShippingMethodsViewFactoryInterface;
16
use BitBag\SyliusVueStorefrontPlugin\Factory\GenericSuccessViewFactoryInterface;
17
use BitBag\SyliusVueStorefrontPlugin\Factory\ValidationErrorViewFactoryInterface;
18
use BitBag\SyliusVueStorefrontPlugin\Processor\RequestProcessorInterface;
19
use BitBag\SyliusVueStorefrontPlugin\Sylius\Matcher\ZoneMatcher;
20
use BitBag\SyliusVueStorefrontPlugin\Sylius\Provider\ChannelProviderInterface;
21
use FOS\RestBundle\View\View;
22
use FOS\RestBundle\View\ViewHandlerInterface;
23
use Sylius\Component\Addressing\Model\ZoneInterface;
24
use Sylius\Component\Core\Model\OrderInterface;
25
use Sylius\Component\Core\Model\ShippingMethodInterface;
26
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
27
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
28
use Sylius\Component\Registry\ServiceRegistry;
29
use Sylius\Component\Shipping\Calculator\CalculatorInterface;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\HttpFoundation\Response;
32
33
final class GetShippingMethodsAction
34
{
35
    /** @var RequestProcessorInterface */
36
    private $getShippingMethodsRequestProcessor;
37
38
    /** @var ViewHandlerInterface */
39
    private $viewHandler;
40
41
    /** @var ValidationErrorViewFactoryInterface */
42
    private $validationErrorViewFactory;
43
44
    /** @var OrderRepositoryInterface */
45
    private $orderRepository;
46
47
    /** @var ChannelProviderInterface */
48
    private $channelProvider;
49
50
    /** @var ZoneMatcher */
51
    private $zoneMatcher;
52
53
    /** @var ShippingMethodRepositoryInterface */
54
    private $shippingMethodRepository;
55
56
    /** @var ServiceRegistry */
57
    private $serviceRegistry;
58
59
    /** @var GenericSuccessViewFactoryInterface */
60
    private $genericSuccessViewFactory;
61
62
    /** @var ShippingMethodsViewFactoryInterface */
63
    private $shippingMethodsViewFactory;
64
65
    public function __construct(
66
        RequestProcessorInterface $getShippingMethodsRequestProcessor,
67
        ViewHandlerInterface $viewHandler,
68
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
69
        OrderRepositoryInterface $orderRepository,
70
        ChannelProviderInterface $channelProvider,
71
        ZoneMatcher $zoneMatcher,
72
        ShippingMethodRepositoryInterface $shippingMethodRepository,
73
        ServiceRegistry $serviceRegistry,
74
        GenericSuccessViewFactoryInterface $genericSuccessViewFactory,
75
        ShippingMethodsViewFactoryInterface $shippingMethodsViewFactory
76
    ) {
77
        $this->getShippingMethodsRequestProcessor = $getShippingMethodsRequestProcessor;
78
        $this->viewHandler = $viewHandler;
79
        $this->validationErrorViewFactory = $validationErrorViewFactory;
80
        $this->orderRepository = $orderRepository;
81
        $this->channelProvider = $channelProvider;
82
        $this->zoneMatcher = $zoneMatcher;
83
        $this->shippingMethodRepository = $shippingMethodRepository;
84
        $this->serviceRegistry = $serviceRegistry;
85
        $this->genericSuccessViewFactory = $genericSuccessViewFactory;
86
        $this->shippingMethodsViewFactory = $shippingMethodsViewFactory;
87
    }
88
89
    public function __invoke(Request $request): Response
90
    {
91
        $validationResults = $this->getShippingMethodsRequestProcessor->validate($request);
92
93
        if (0 !== count($validationResults)) {
94
            return $this->viewHandler->handle(
95
                View::create(
96
                    $this->validationErrorViewFactory->create($validationResults),
97
                    Response::HTTP_BAD_REQUEST
98
                )
99
            );
100
        }
101
102
        $query = $this->getShippingMethodsRequestProcessor->getQuery($request);
103
104
        /** @var OrderInterface $cart */
105
        $cart = $this->orderRepository->findOneBy(
106
            [
107
                'tokenValue' => $query->cartId(),
108
                'shippingState' => OrderInterface::STATE_CART,
109
            ]
110
        );
111
112
        $channel = $this->channelProvider->provide();
113
114
        $zone = $this->zoneMatcher->match($query->address()->country_id, ZoneInterface::TYPE_COUNTRY);
115
116
        /** @var ShippingMethodInterface[] $shippingMethods */
117
        $shippingMethods = $this->shippingMethodRepository->findEnabledForZonesAndChannel([$zone], $channel);
118
119
        foreach ($shippingMethods as $shippingMethod) {
120
            if (!$cart->getShipments()->first()) {
121
                continue;
122
            }
123
124
            /** @var CalculatorInterface $calculator */
125
            $calculator = $this->serviceRegistry->get($shippingMethod->getCalculator());
126
            $calculator->calculate($cart->getShipments()->first(), $shippingMethod->getConfiguration());
127
        }
128
129
        return $this->viewHandler->handle(
130
            View::create(
131
                $this->genericSuccessViewFactory->create(
132
                    $this->shippingMethodsViewFactory->createList(...$shippingMethods)
133
                ),
134
                Response::HTTP_OK
135
            )
136
        );
137
    }
138
}
139