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

getCalculatedShippingMethods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 12
loc 12
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
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