1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Controller; |
4
|
|
|
|
5
|
|
|
use FOS\RestBundle\View\View; |
6
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
7
|
|
|
use Sylius\Component\Core\Factory\AddressFactoryInterface; |
8
|
|
|
use Sylius\Component\Core\Model\AddressInterface; |
9
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
10
|
|
|
use Sylius\Component\Core\Model\ShipmentInterface; |
11
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
12
|
|
|
use Sylius\Component\Order\Repository\OrderItemRepositoryInterface; |
13
|
|
|
use Sylius\Component\Registry\ServiceRegistryInterface; |
14
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
15
|
|
|
use Sylius\Component\Shipping\Exception\UnresolvedDefaultShippingMethodException; |
16
|
|
|
use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface; |
17
|
|
|
use Sylius\ShopApiPlugin\Factory\PriceViewFactoryInterface; |
18
|
|
|
use Sylius\ShopApiPlugin\View\EstimatedShippingCostView; |
19
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
22
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
23
|
|
|
|
24
|
|
|
final class CartController extends Controller |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param Request $request |
28
|
|
|
* |
29
|
|
|
* @return Response |
30
|
|
|
*/ |
31
|
|
|
public function removeItemAction(Request $request) |
32
|
|
|
{ |
33
|
|
|
/** @var OrderRepositoryInterface $cartRepository */ |
34
|
|
|
$cartRepository = $this->get('sylius.repository.order'); |
35
|
|
|
/** @var ViewHandlerInterface $viewHandler */ |
36
|
|
|
$viewHandler = $this->get('fos_rest.view_handler'); |
37
|
|
|
/** @var OrderItemRepositoryInterface $orderItemRepository */ |
38
|
|
|
$cartItemRepository = $this->get('sylius.repository.order_item'); |
39
|
|
|
|
40
|
|
|
$cart = $cartRepository->findOneBy(['tokenValue' => $request->attributes->get('token')]); |
41
|
|
|
|
42
|
|
|
if (null === $cart) { |
43
|
|
|
throw new NotFoundHttpException('Cart with given id does not exists'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** @var OrderInterface $cart */ |
47
|
|
|
$cartItem = $cartItemRepository->find($request->attributes->get('id')); |
48
|
|
|
|
49
|
|
|
if (null === $cartItem || !$cart->hasItem($cartItem)) { |
50
|
|
|
throw new NotFoundHttpException('Cart item with given id does not exists'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$cart->removeItem($cartItem); |
54
|
|
|
$cartItemRepository->remove($cartItem); |
55
|
|
|
|
56
|
|
|
return $viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Request $request |
61
|
|
|
* |
62
|
|
|
* @return Response |
63
|
|
|
*/ |
64
|
|
|
public function estimateShippingCostAction(Request $request) |
65
|
|
|
{ |
66
|
|
|
/** @var OrderRepositoryInterface $cartRepository */ |
67
|
|
|
$cartRepository = $this->get('sylius.repository.order'); |
68
|
|
|
/** @var ViewHandlerInterface $viewHandler */ |
69
|
|
|
$viewHandler = $this->get('fos_rest.view_handler'); |
70
|
|
|
/** @var ShippingMethodsResolverInterface $shippingMethodResolver */ |
71
|
|
|
$shippingMethodResolver = $this->get('sylius.shipping_methods_resolver'); |
72
|
|
|
/** @var AddressFactoryInterface $addressFactory */ |
73
|
|
|
$addressFactory = $this->get('sylius.factory.address'); |
74
|
|
|
/** @var FactoryInterface $shipmentFactory */ |
75
|
|
|
$shipmentFactory = $this->get('sylius.factory.shipment'); |
76
|
|
|
/** @var ServiceRegistryInterface $calculators */ |
77
|
|
|
$calculators = $this->get('sylius.registry.shipping_calculator'); |
78
|
|
|
/** @var PriceViewFactoryInterface $priceViewFactory */ |
79
|
|
|
$priceViewFactory = $this->get('sylius.shop_api_plugin.factory.price_view_factory'); |
80
|
|
|
|
81
|
|
|
/** @var OrderInterface $cart */ |
82
|
|
|
$cart = $cartRepository->findOneBy(['tokenValue' => $request->attributes->get('token')]); |
83
|
|
|
|
84
|
|
|
if (null === $cart) { |
85
|
|
|
throw new NotFoundHttpException('Cart with given id does not exists'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** @var AddressInterface $address */ |
89
|
|
|
$address = $addressFactory->createNew(); |
90
|
|
|
$address->setCountryCode($request->query->get('countryCode')); |
91
|
|
|
$address->setProvinceCode($request->query->get('provinceCode')); |
92
|
|
|
$cart->setShippingAddress($address); |
93
|
|
|
|
94
|
|
|
/** @var ShipmentInterface $shipment */ |
95
|
|
|
$shipment = $shipmentFactory->createNew(); |
96
|
|
|
$shipment->setOrder($cart); |
97
|
|
|
|
98
|
|
|
$shippingMethods = $shippingMethodResolver->getSupportedMethods($shipment); |
99
|
|
|
|
100
|
|
|
if (empty($shippingMethods)) { |
101
|
|
|
throw new UnresolvedDefaultShippingMethodException(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$shippingMethod = $shippingMethods[0]; |
105
|
|
|
|
106
|
|
|
$estimatedShippingCostView = new EstimatedShippingCostView(); |
107
|
|
|
$calculator = $calculators->get($shippingMethod->getCalculator()); |
108
|
|
|
|
109
|
|
|
$estimatedShippingCostView->price = $priceViewFactory->create($calculator->calculate($shipment, $shippingMethod->getConfiguration())); |
110
|
|
|
|
111
|
|
|
return $viewHandler->handle(View::create($estimatedShippingCostView, Response::HTTP_OK)); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|