Completed
Push — master ( 41948a...5bb6bb )
by Łukasz
17:08 queued 13:33
created

ShowAvailableShippingMethodsAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 9
dl 78
loc 78
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
A __invoke() 13 13 2
A getCalculatedShippingMethods() 12 12 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
namespace Sylius\ShopApiPlugin\Controller\Checkout;
4
5
use FOS\RestBundle\View\View;
6
use FOS\RestBundle\View\ViewHandlerInterface;
7
use Sylius\Component\Core\Model\OrderInterface;
8
use Sylius\Component\Core\Model\ShipmentInterface;
9
use Sylius\Component\Core\Model\ShippingMethodInterface;
10
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
11
use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface;
12
use Sylius\ShopApiPlugin\Factory\ShippingMethodViewFactory;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
16 View Code Duplication
final class ShowAvailableShippingMethodsAction
1 ignored issue
show
Duplication introduced by
This class 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...
17
{
18
    /**
19
     * @var OrderRepositoryInterface
20
     */
21
    private $cartRepository;
22
23
    /**
24
     * @var ViewHandlerInterface
25
     */
26
    private $viewHandler;
27
28
    /**
29
     * @var ShippingMethodsResolverInterface
30
     */
31
    private $shippingMethodsResolver;
32
33
    /**
34
     * @var ShippingMethodViewFactory
35
     */
36
    private $shippingMethodViewFactory;
37
38
    /**
39
     * @param OrderRepositoryInterface $cartRepository
40
     * @param ViewHandlerInterface $viewHandler
41
     * @param ShippingMethodsResolverInterface $shippingMethodsResolver
42
     * @param ShippingMethodViewFactory $shippingMethodViewFactory
43
     */
44
    public function __construct(
45
        OrderRepositoryInterface $cartRepository,
46
        ViewHandlerInterface $viewHandler,
47
        ShippingMethodsResolverInterface $shippingMethodsResolver,
48
        ShippingMethodViewFactory $shippingMethodViewFactory
49
    ) {
50
        $this->cartRepository = $cartRepository;
51
        $this->viewHandler = $viewHandler;
52
        $this->shippingMethodsResolver = $shippingMethodsResolver;
53
        $this->shippingMethodViewFactory = $shippingMethodViewFactory;
54
    }
55
56
    /**
57
     * @param Request $request
58
     *
59
     * @return Response
60
     */
61
    public function __invoke(Request $request)
62
    {
63
        /** @var OrderInterface $cart */
64
        $cart = $this->cartRepository->findOneBy(['tokenValue' => $request->attributes->get('token')]);
65
66
        $shipments = [];
67
68
        foreach ($cart->getShipments() as $shipment) {
69
            $shipments['shipments'][] = $this->getCalculatedShippingMethods($shipment, $cart->getLocaleCode());
70
        }
71
72
        return $this->viewHandler->handle(View::create($shipments));
73
    }
74
75
    /**
76
     * @param ShipmentInterface $shipment
77
     * @param string $locale
78
     *
79
     * @return array
80
     */
81
    private function getCalculatedShippingMethods(ShipmentInterface $shipment, $locale)
82
    {
83
        $rawShippingMethods = [];
84
85
        /** @var ShippingMethodInterface $shippingMethod */
86
        foreach ($this->shippingMethodsResolver->getSupportedMethods($shipment) as $shippingMethod) {
87
88
            $rawShippingMethods['methods'][$shippingMethod->getCode()] = $this->shippingMethodViewFactory->createWithShippingMethod($shipment, $shippingMethod, $locale);
0 ignored issues
show
Compatibility introduced by
$shippingMethod of type object<Sylius\Component\...hippingMethodInterface> is not a sub-type of object<Sylius\Component\...hippingMethodInterface>. It seems like you assume a child interface of the interface Sylius\Component\Shippin...ShippingMethodInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
89
        }
90
91
        return $rawShippingMethods;
92
    }
93
}
94