ShowAvailableShippingMethodsAction   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 15.29 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 13
loc 85
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A __invoke() 13 13 2
A getCalculatedShippingMethods() 0 19 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Controller\Checkout;
6
7
use FOS\RestBundle\View\View;
8
use FOS\RestBundle\View\ViewHandlerInterface;
9
use Sylius\Component\Core\Model\OrderInterface;
10
use Sylius\Component\Core\Model\ShipmentInterface;
11
use Sylius\Component\Core\Model\ShippingMethodInterface;
12
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
13
use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface;
14
use Sylius\ShopApiPlugin\Factory\ShippingMethodViewFactoryInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
18
final class ShowAvailableShippingMethodsAction
19
{
20
    /**
21
     * @var OrderRepositoryInterface
22
     */
23
    private $cartRepository;
24
25
    /**
26
     * @var ViewHandlerInterface
27
     */
28
    private $viewHandler;
29
30
    /**
31
     * @var ShippingMethodsResolverInterface
32
     */
33
    private $shippingMethodsResolver;
34
35
    /**
36
     * @var ShippingMethodViewFactoryInterface
37
     */
38
    private $shippingMethodViewFactory;
39
40
    /**
41
     * @param OrderRepositoryInterface $cartRepository
42
     * @param ViewHandlerInterface $viewHandler
43
     * @param ShippingMethodsResolverInterface $shippingMethodsResolver
44
     * @param ShippingMethodViewFactoryInterface $shippingMethodViewFactory
45
     */
46
    public function __construct(
47
        OrderRepositoryInterface $cartRepository,
48
        ViewHandlerInterface $viewHandler,
49
        ShippingMethodsResolverInterface $shippingMethodsResolver,
50
        ShippingMethodViewFactoryInterface $shippingMethodViewFactory
51
    ) {
52
        $this->cartRepository = $cartRepository;
53
        $this->viewHandler = $viewHandler;
54
        $this->shippingMethodsResolver = $shippingMethodsResolver;
55
        $this->shippingMethodViewFactory = $shippingMethodViewFactory;
56
    }
57
58
    /**
59
     * @param Request $request
60
     *
61
     * @return Response
62
     */
63 View Code Duplication
    public function __invoke(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        /** @var OrderInterface $cart */
66
        $cart = $this->cartRepository->findOneBy(['tokenValue' => $request->attributes->get('token')]);
67
68
        $shipments = [];
69
70
        foreach ($cart->getShipments() as $shipment) {
71
            $shipments['shipments'][] = $this->getCalculatedShippingMethods($shipment, $cart->getLocaleCode());
72
        }
73
74
        return $this->viewHandler->handle(View::create($shipments));
75
    }
76
77
    /**
78
     * @param ShipmentInterface $shipment
79
     * @param string $locale
80
     *
81
     * @return array
82
     */
83
    private function getCalculatedShippingMethods(ShipmentInterface $shipment, $locale)
84
    {
85
        $rawShippingMethods = [];
86
87
        /** @var ShippingMethodInterface $shippingMethod */
88
        foreach ($this->shippingMethodsResolver->getSupportedMethods($shipment) as $shippingMethod) {
89
            /** @var OrderInterface $order */
90
            $order = $shipment->getOrder();
91
92
            $rawShippingMethods['methods'][$shippingMethod->getCode()] = $this->shippingMethodViewFactory->createWithShippingMethod(
93
                $shipment,
94
                $shippingMethod,
95
                $locale,
96
                $order->getCurrencyCode()
97
            );
98
        }
99
100
        return $rawShippingMethods;
101
    }
102
}
103