CartController::cleanCart()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Cart\Cart;
6
use AppBundle\Cart\CartSerializer;
7
use AppBundle\Entity\Order;
8
use AppBundle\Event\OrderEvent;
9
use AppBundle\Event\OrderEvents;
10
use AppBundle\Form\Type\OrderType;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
12
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
16
class CartController extends Controller
17
{
18
    /**
19
     * @Route(path="/cart", methods="POST", name="cart_setMeal")
20
     */
21
    public function setMealAction(Request $request)
22
    {
23
        $mode = $request->request->get('mode', 'set');
24
25
        $mealId = $request->request->get('meal');
26
        $meal = $this->getDoctrine()->getRepository('AppBundle:Meal')->find($mealId);
27
        if (!$meal) {
28
            throw $this->createNotFoundException(sprintf(
29
                'No meal found with id "%s".', $mealId
30
            ));
31
        }
32
33
        $quantity = (int) $request->request->get('quantity');
34
        $quantity = max(0, min(1000, $quantity));
35
36
        $cart = $this->getCart($request);
37
38
        if ($mode == 'add') {
39
            $cart->addMeal($meal, $quantity);
40
        } elseif ($mode == 'set') {
41
            $cart->setMeal($meal, $quantity);
42
        }
43
44
        $this->saveCart($request, $cart);
45
46
        return $this->forward('AppBundle:Cart:panel');
47
    }
48
49
    /**
50
     * @Route(path="/cart", methods="GET", name="cart_show")
51
     */
52
    public function showAction(Request $request)
53
    {
54
        return $this->render('cart/show.html.twig', array(
55
            'cart' => $this->getCart($request)
56
        ));
57
    }
58
59
    /**
60
     * @Route(path="/order", methods="GET|POST", name="cart_order")
61
     */
62
    public function orderAction(Request $request)
63
    {
64
        $form = $this->createForm(OrderType::class);
65
66
        $cart = $this->getCart($request);
67
68
        if ($cart->isEmpty()) {
69
            return $this->redirectToRoute('cart_show');
70
        }
71
72
        $form->handleRequest($request);
73
74
        if ($form->isSubmitted() && $form->isValid()) {
75
            $order = $form->getData();
76
            $order->loadFromCart($this->getCart($request));
77
78
            $em = $this->getDoctrine()->getManagerForClass(Order::class);
79
            $em->persist($order);
80
            $em->flush();
81
82
            $this->get('event_dispatcher')->dispatch(
83
                OrderEvents::ORDER_CREATED,
84
                new OrderEvent($order)
85
            );
86
87
            $this->cleanCart($request);
88
89
            return $this->redirectToRoute('order_show', array('id' => $order->getId()));
90
        }
91
92
        return $this->render('cart/order.html.twig', array(
93
            'cart' => $cart,
94
            'form' => $form->createView(),
95
        ));
96
    }
97
98
    public function panelAction(Request $request)
99
    {
100
        return $this->render('cart/_panel.html.twig', array(
101
            'cart' => $this->getCart($request)
102
        ));
103
    }
104
105 View Code Duplication
    private function getCart(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        $cartData = $request->getSession()->get('cart');
108
        if (null === $cartData) {
109
            return new Cart();
110
        }
111
112
        $serializer = new CartSerializer($this->getDoctrine()->getManager());
113
114
        return $serializer->deserialize($cartData);
115
    }
116
117 View Code Duplication
    private function saveCart(Request $request, Cart $cart)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119
        $serializer = new CartSerializer($this->getDoctrine()->getManager());
120
        $data = $serializer->serialize($cart);
121
122
        $request->getSession()->set('cart', $data);
123
    }
124
125
    private function cleanCart(Request $request)
126
    {
127
        $request->getSession()->set('cart', null);
128
    }
129
}
130