CartController::clearAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GGGGino\SkuskuCartBundle\Controller;
4
5
use GGGGino\SkuskuCartBundle\Entity\CartForm;
6
use GGGGino\SkuskuCartBundle\Form\CartFlow;
7
use GGGGino\SkuskuCartBundle\Model\SkuskuCart;
8
use GGGGino\SkuskuCartBundle\Model\SkuskuCartProduct;
9
use GGGGino\SkuskuCartBundle\Model\SkuskuCartProductInterface;
10
use GGGGino\SkuskuCartBundle\Service\CartManager;
11
use GGGGino\SkuskuCartBundle\Event\TokenNotFoundCartEvent;
12
use Payum\Core\Gateway;
13
use Payum\Core\Request\GetHumanStatus;
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\Routing\Annotation\Route;
20
use GGGGino\SkuskuCartBundle\Model\SkuskuPayment;
21
use Payum\Core\Reply\HttpRedirect;
22
use Payum\Core\Reply\HttpResponse;
23
use Payum\Core\Request\Capture;
24
25
/**
26
 * Class CartController
27
 * @package GGGGino\SkuskuCartBundle\Controller
28
 */
29
class CartController extends Controller
30
{
31
    /**
32
     * Cart page
33
     *
34
     * @Route("/cart", name="cart_page")
35
     */
36
    public function cartAction()
37
    {
38
39
        $cartMode = $this->getParameter('ggggino_skuskucart.cart_mode');
0 ignored issues
show
Unused Code introduced by
The assignment to $cartMode is dead and can be removed.
Loading history...
40
41
        /** @var CartManager $cartManager */
42
        $cartManager = $this->get(CartManager::class);
43
44
        /** @var string $cartFlowClass */
45
        $cartFlowClass = $this->getParameter('ggggino_skuskucart.stepform_class');
46
47
        /** @var string $cartTemplate */
48
        $cartTemplate = $this->getParameter('ggggino_skuskucart.templates.cart_layout');
49
50
        /** @var SkuskuCart $cart */
51
        $cart = $cartManager->getCartFromCustomer();
52
53
        $formData = new CartForm($cart);
54
55
        /** @var CartFlow $flow */
56
        $flow = $this->get($cartFlowClass); // must match the flow's service id
57
        $flow->bind($formData);
58
59
        // form of the current step
60
        $form = $flow->createForm();
61
62
        $response = $flow->handleSubmit($form, $formData);
63
64
        if ( $response instanceof RedirectResponse ) {
0 ignored issues
show
introduced by
$response is always a sub-type of Symfony\Component\HttpFoundation\RedirectResponse.
Loading history...
65
            return $response;
66
        }
67
68
        return $this->render($cartTemplate, array(
69
            'form' => $form->createView(),
70
            'flow' => $flow,
71
            'additional_fields' => $this->getParameter('ggggino_skuskucart.additional_fields'),
72
            'cart_mode' => $this->getParameter('ggggino_skuskucart.cart_mode')
73
        ));
74
    }
75
76
    /**
77
     * Cart page
78
     *
79
     * @Route("/remove_item/{product}", name="remove_item_from_cart")
80
     */
81
    public function removeItemFromCart(SkuskuCartProduct $product)
82
    {
83
        $em = $this->getDoctrine()->getManager();
84
        $em->remove($product);
85
        $em->flush();
86
87
        return new Response('ok');
88
    }
89
90
    /**
91
     * Action temporanea per i test
92
     *
93
     * @Route("/cart/clear", name="clear_cart")
94
     */
95
    public function clearAction()
96
    {
97
        /** @var CartManager $cartManager */
98
        $cartManager = $this->get(CartManager::class);
99
        $cartManager->clearCart();
100
101
        return new Response('ok');
102
    }
103
104
    /**
105
     * Action temporanea per i test
106
     *
107
     * @Route("/done", name="done")
108
     */
109
    public function doneAction(Request $request)
110
    {
111
        /** @var string $cartFlowClass */
112
        $cartFlowClass = $this->getParameter('ggggino_skuskucart.stepform_class');
113
114
        /** @var CartFlow $flow */
115
        $flow = $this->get($cartFlowClass);
116
117
        try {
118
            $token = $this->get('payum')->getHttpRequestVerifier()->verify($request);
119
        } catch (\Exception $tokenNotFound) {
120
            // To remove this if because everithing is managed in the event listener
121
            if(null !== $this->getParameter('ggggino_skuskucart.redirect_after_done_route')) {
122
                return $this->redirectToRoute($redirectRoute);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $redirectRoute seems to be never defined.
Loading history...
123
            }
124
125
            $event = new TokenNotFoundCartEvent();
126
            return $this->container->get('event_dispatcher')->dispatch(CartFlow::TOKEN_NOT_FOUND, $event)->getResponse();
127
128
        }        
129
130
        $gateway = $this->get('payum')->getGateway($token->getGatewayName());
131
132
        $this->get('payum')->getHttpRequestVerifier()->invalidate($token);
133
134
        $gateway->execute($status = new GetHumanStatus($token));
135
        $payment = $status->getFirstModel();
136
137
        $flow->handleDone($payment, $status);
138
139
        if ( $this->container->hasParameter('ggggino_skuskucart.templates.done_layout') ) {
140
            return $this->render($this->container->getParameter('ggggino_skuskucart.templates.done_layout'), array(
141
                'status' => $status,
142
                'payment' => $payment
143
            ));
144
        }
145
146
        return new JsonResponse(array(
147
            'status' => $status->getValue(),
148
            'payment' => array(
149
                'total_amount' => $payment->getTotalAmount(),
150
                'currency_code' => $payment->getCurrencyCode(),
151
                'details' => $payment->getDetails(),
152
            )
153
        ));
154
    }
155
}
156