Completed
Pull Request — master (#132)
by Łukasz
04:54
created

CartController   C

Complexity

Total Complexity 13

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 21

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 21
dl 0
loc 155
rs 6.1111
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A dropAction() 0 17 2
B changeItemQuantityAction() 0 36 4
B removeItemAction() 0 27 4
A estimateShippingCostAction() 0 49 3
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 changeItemQuantityAction(Request $request)
64
    {
65
        /** @var ObjectManager $cartManager */
66
        $cartManager = $this->get('sylius.manager.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
        /** @var OrderItemQuantityModifierInterface $orderItemModifier */
72
        $orderItemModifier = $this->get('sylius.order_item_quantity_modifier');
73
        /** @var OrderProcessorInterface $orderProcessor */
74
        $orderProcessor = $this->get('sylius.order_processing.order_processor');
75
        /** @var OrderRepositoryInterface $cartRepository */
76
        $cartRepository = $this->get('sylius.repository.order');
77
78
        $cart = $cartRepository->findOneBy(['tokenValue' => $request->attributes->get('token')]);
79
80
        if (null === $cart) {
81
            throw new NotFoundHttpException('Cart with given id does not exists');
82
        }
83
84
        /** @var OrderInterface $cart */
85
        $cartItem = $cartItemRepository->find($request->attributes->get('id'));
86
87
        if (null === $cartItem || !$cart->hasItem($cartItem)) {
88
            throw new NotFoundHttpException('Cart item with given id does not exists');
89
        }
90
91
        $orderItemModifier->modify($cartItem, $request->request->getInt('quantity'));
92
93
        $orderProcessor->process($cart);
94
95
        $cartManager->flush();
96
97
        return $viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
98
    }
99
100
    /**
101
     * @param Request $request
102
     *
103
     * @return Response
104
     */
105
    public function removeItemAction(Request $request)
106
    {
107
        /** @var OrderRepositoryInterface $cartRepository */
108
        $cartRepository = $this->get('sylius.repository.order');
109
        /** @var ViewHandlerInterface $viewHandler */
110
        $viewHandler = $this->get('fos_rest.view_handler');
111
        /** @var OrderItemRepositoryInterface $orderItemRepository */
112
        $cartItemRepository = $this->get('sylius.repository.order_item');
113
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 OrderInterface $cart */
121
        $cartItem = $cartItemRepository->find($request->attributes->get('id'));
122
123
        if (null === $cartItem || !$cart->hasItem($cartItem)) {
124
            throw new NotFoundHttpException('Cart item with given id does not exists');
125
        }
126
127
        $cart->removeItem($cartItem);
128
        $cartItemRepository->remove($cartItem);
129
130
        return $viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
131
    }
132
133
    /**
134
     * @param Request $request
135
     *
136
     * @return Response
137
     */
138
    public function estimateShippingCostAction(Request $request)
139
    {
140
        /** @var OrderRepositoryInterface $cartRepository */
141
        $cartRepository = $this->get('sylius.repository.order');
142
        /** @var ViewHandlerInterface $viewHandler */
143
        $viewHandler = $this->get('fos_rest.view_handler');
144
        /** @var ShippingMethodsResolverInterface $shippingMethodResolver */
145
        $shippingMethodResolver = $this->get('sylius.shipping_methods_resolver');
146
        /** @var AddressFactoryInterface $addressFactory */
147
        $addressFactory = $this->get('sylius.factory.address');
148
        /** @var FactoryInterface $shipmentFactory */
149
        $shipmentFactory = $this->get('sylius.factory.shipment');
150
        /** @var ServiceRegistryInterface $calculators */
151
        $calculators = $this->get('sylius.registry.shipping_calculator');
152
        /** @var PriceViewFactoryInterface $priceViewFactory */
153
        $priceViewFactory = $this->get('sylius.shop_api_plugin.factory.price_view_factory');
154
155
        /** @var OrderInterface $cart */
156
        $cart = $cartRepository->findOneBy(['tokenValue' => $request->attributes->get('token')]);
157
158
        if (null === $cart) {
159
            throw new NotFoundHttpException('Cart with given id does not exists');
160
        }
161
162
        /** @var AddressInterface $address */
163
        $address = $addressFactory->createNew();
164
        $address->setCountryCode($request->query->get('countryCode'));
165
        $address->setProvinceCode($request->query->get('provinceCode'));
166
        $cart->setShippingAddress($address);
167
168
        /** @var ShipmentInterface $shipment */
169
        $shipment = $shipmentFactory->createNew();
170
        $shipment->setOrder($cart);
171
172
        $shippingMethods = $shippingMethodResolver->getSupportedMethods($shipment);
173
174
        if (empty($shippingMethods)) {
175
            throw new UnresolvedDefaultShippingMethodException();
176
        }
177
178
        $shippingMethod = $shippingMethods[0];
179
180
        $estimatedShippingCostView = new EstimatedShippingCostView();
181
        $calculator = $calculators->get($shippingMethod->getCalculator());
182
183
        $estimatedShippingCostView->price = $priceViewFactory->create($calculator->calculate($shipment, $shippingMethod->getConfiguration()));
184
185
        return $viewHandler->handle(View::create($estimatedShippingCostView, Response::HTTP_OK));
186
    }
187
}
188