Completed
Push — master ( b6acfe...4d9bc8 )
by Paweł
03:29
created

CartController::dropAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
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 changeItemQuantityAction(Request $request)
41
    {
42
        /** @var ObjectManager $cartManager */
43
        $cartManager = $this->get('sylius.manager.order');
44
        /** @var ViewHandlerInterface $viewHandler */
45
        $viewHandler = $this->get('fos_rest.view_handler');
46
        /** @var OrderItemRepositoryInterface $orderItemRepository */
47
        $cartItemRepository = $this->get('sylius.repository.order_item');
48
        /** @var OrderItemQuantityModifierInterface $orderItemModifier */
49
        $orderItemModifier = $this->get('sylius.order_item_quantity_modifier');
50
        /** @var OrderProcessorInterface $orderProcessor */
51
        $orderProcessor = $this->get('sylius.order_processing.order_processor');
52
        /** @var OrderRepositoryInterface $cartRepository */
53
        $cartRepository = $this->get('sylius.repository.order');
54
55
        $cart = $cartRepository->findOneBy(['tokenValue' => $request->attributes->get('token')]);
56
57
        if (null === $cart) {
58
            throw new NotFoundHttpException('Cart with given id does not exists');
59
        }
60
61
        /** @var OrderInterface $cart */
62
        $cartItem = $cartItemRepository->find($request->attributes->get('id'));
63
64
        if (null === $cartItem || !$cart->hasItem($cartItem)) {
65
            throw new NotFoundHttpException('Cart item with given id does not exists');
66
        }
67
68
        $orderItemModifier->modify($cartItem, $request->request->getInt('quantity'));
69
70
        $orderProcessor->process($cart);
71
72
        $cartManager->flush();
73
74
        return $viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
75
    }
76
77
    /**
78
     * @param Request $request
79
     *
80
     * @return Response
81
     */
82
    public function removeItemAction(Request $request)
83
    {
84
        /** @var OrderRepositoryInterface $cartRepository */
85
        $cartRepository = $this->get('sylius.repository.order');
86
        /** @var ViewHandlerInterface $viewHandler */
87
        $viewHandler = $this->get('fos_rest.view_handler');
88
        /** @var OrderItemRepositoryInterface $orderItemRepository */
89
        $cartItemRepository = $this->get('sylius.repository.order_item');
90
91
        $cart = $cartRepository->findOneBy(['tokenValue' => $request->attributes->get('token')]);
92
93
        if (null === $cart) {
94
            throw new NotFoundHttpException('Cart with given id does not exists');
95
        }
96
97
        /** @var OrderInterface $cart */
98
        $cartItem = $cartItemRepository->find($request->attributes->get('id'));
99
100
        if (null === $cartItem || !$cart->hasItem($cartItem)) {
101
            throw new NotFoundHttpException('Cart item with given id does not exists');
102
        }
103
104
        $cart->removeItem($cartItem);
105
        $cartItemRepository->remove($cartItem);
106
107
        return $viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
108
    }
109
110
    /**
111
     * @param Request $request
112
     *
113
     * @return Response
114
     */
115
    public function estimateShippingCostAction(Request $request)
116
    {
117
        /** @var OrderRepositoryInterface $cartRepository */
118
        $cartRepository = $this->get('sylius.repository.order');
119
        /** @var ViewHandlerInterface $viewHandler */
120
        $viewHandler = $this->get('fos_rest.view_handler');
121
        /** @var ShippingMethodsResolverInterface $shippingMethodResolver */
122
        $shippingMethodResolver = $this->get('sylius.shipping_methods_resolver');
123
        /** @var AddressFactoryInterface $addressFactory */
124
        $addressFactory = $this->get('sylius.factory.address');
125
        /** @var FactoryInterface $shipmentFactory */
126
        $shipmentFactory = $this->get('sylius.factory.shipment');
127
        /** @var ServiceRegistryInterface $calculators */
128
        $calculators = $this->get('sylius.registry.shipping_calculator');
129
        /** @var PriceViewFactoryInterface $priceViewFactory */
130
        $priceViewFactory = $this->get('sylius.shop_api_plugin.factory.price_view_factory');
131
132
        /** @var OrderInterface $cart */
133
        $cart = $cartRepository->findOneBy(['tokenValue' => $request->attributes->get('token')]);
134
135
        if (null === $cart) {
136
            throw new NotFoundHttpException('Cart with given id does not exists');
137
        }
138
139
        /** @var AddressInterface $address */
140
        $address = $addressFactory->createNew();
141
        $address->setCountryCode($request->query->get('countryCode'));
142
        $address->setProvinceCode($request->query->get('provinceCode'));
143
        $cart->setShippingAddress($address);
144
145
        /** @var ShipmentInterface $shipment */
146
        $shipment = $shipmentFactory->createNew();
147
        $shipment->setOrder($cart);
148
149
        $shippingMethods = $shippingMethodResolver->getSupportedMethods($shipment);
150
151
        if (empty($shippingMethods)) {
152
            throw new UnresolvedDefaultShippingMethodException();
153
        }
154
155
        $shippingMethod = $shippingMethods[0];
156
157
        $estimatedShippingCostView = new EstimatedShippingCostView();
158
        $calculator = $calculators->get($shippingMethod->getCalculator());
159
160
        $estimatedShippingCostView->price = $priceViewFactory->create($calculator->calculate($shipment, $shippingMethod->getConfiguration()));
161
162
        return $viewHandler->handle(View::create($estimatedShippingCostView, Response::HTTP_OK));
163
    }
164
}
165