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 |
|
|
|
|
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); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $rawShippingMethods; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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.