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