Completed
Pull Request — master (#136)
by Łukasz
03:19
created

CartController::removeItemAction()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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