Completed
Push — symfony3-core ( 6ddaba...73f033 )
by Kamil
60:25 queued 41:49
created

OrderController::clearAction()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\CoreBundle\Controller;
13
14
use Doctrine\ORM\EntityManager;
15
use FOS\RestBundle\View\View;
16
use Payum\Core\Registry\RegistryInterface;
17
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
18
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
19
use Sylius\Component\Order\Context\CartContextInterface;
20
use Sylius\Component\Order\Model\OrderInterface;
21
use Sylius\Component\Order\SyliusCartEvents;
22
use Sylius\Component\Resource\ResourceActions;
23
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24
use Symfony\Component\EventDispatcher\GenericEvent;
25
use Symfony\Component\HttpFoundation\RedirectResponse;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Response;
28
use Symfony\Component\HttpKernel\Exception\HttpException;
29
use Webmozart\Assert\Assert;
30
31
class OrderController extends ResourceController
32
{
33
    /**
34
     * @param Request $request
35
     *
36
     * @return Response
37
     */
38
    public function summaryAction(Request $request)
39
    {
40
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
41
42
        $cart = $this->getCurrentCart();
43
44
        if (!$configuration->isHtmlRequest()) {
45
            return $this->viewHandler->handle($configuration, View::create($cart));
46
        }
47
48
        $form = $this->resourceFormFactory->create($configuration, $cart);
49
50
        $view = View::create()
51
            ->setTemplate($configuration->getTemplate('summary.html'))
52
            ->setData([
53
                'cart' => $cart,
54
                'form' => $form->createView(),
55
            ])
56
        ;
57
58
        return $this->viewHandler->handle($configuration, $view);
59
    }
60
61
    /**
62
     * @param Request $request
63
     *
64
     * @return Response
65
     */
66
    public function saveAction(Request $request)
67
    {
68
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
69
70
        $this->isGrantedOr403($configuration, ResourceActions::UPDATE);
71
        $resource = $this->getCurrentCart();
72
73
        $form = $this->resourceFormFactory->create($configuration, $resource);
74
75
        if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true) && $form->handleRequest($request)->isValid()) {
76
            $resource = $form->getData();
77
78
            $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource);
79
80
            if ($event->isStopped() && !$configuration->isHtmlRequest()) {
81
                throw new HttpException($event->getErrorCode(), $event->getMessage());
82
            }
83
            if ($event->isStopped()) {
84
                $this->flashHelper->addFlashFromEvent($configuration, $event);
85
86
                return $this->redirectHandler->redirectToResource($configuration, $resource);
87
            }
88
89
            if ($configuration->hasStateMachine()) {
90
                $this->stateMachine->apply($configuration, $resource);
91
            }
92
93
            $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource);
94
95
            if (!$configuration->isHtmlRequest()) {
96
                return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT));
97
            }
98
99
            $this->getEventDispatcher()->dispatch(SyliusCartEvents::CART_CHANGE, new GenericEvent($resource));
100
            $this->manager->flush();
101
102
            $this->flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource);
103
104
            return $this->redirectHandler->redirectToResource($configuration, $resource);
105
        }
106
107
        if (!$configuration->isHtmlRequest()) {
108
            return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST));
109
        }
110
111
        $view = View::create()
112
            ->setData([
113
                'configuration' => $configuration,
114
                $this->metadata->getName() => $resource,
115
                'form' => $form->createView(),
116
                'cart' => $resource,
117
            ])
118
            ->setTemplate($configuration->getTemplate(ResourceActions::UPDATE . '.html'))
119
        ;
120
121
        return $this->viewHandler->handle($configuration, $view);
122
    }
123
124
    /**
125
     * @param Request $request
126
     *
127
     * @return Response
128
     */
129
    public function thankYouAction(Request $request)
130
    {
131
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
132
133
        $orderId = $request->getSession()->get('sylius_order_id', null);
134
135
        if (null === $orderId) {
136
            $options = $configuration->getParameters()->get('after_failure');
137
138
            return $this->redirectHandler->redirectToRoute(
139
                $configuration,
140
                isset($options['route']) ? $options['route'] : 'sylius_shop_homepage',
141
                isset($options['parameters']) ? $options['parameters'] : []
142
            );
143
        }
144
145
        $request->getSession()->remove('sylius_order_id');
146
        $order = $this->repository->find($orderId);
147
        Assert::notNull($order);
148
149
        $view = View::create()
150
            ->setData([
151
                'order' => $order
152
            ])
153
            ->setTemplate($configuration->getParameters()->get('template'))
154
        ;
155
156
        return $this->viewHandler->handle($configuration, $view);
157
    }
158
159
    /**
160
     * @param Request $request
161
     *
162
     * @return Response
163
     */
164
    public function clearAction(Request $request)
165
    {
166
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
167
168
        $this->isGrantedOr403($configuration, ResourceActions::DELETE);
169
        $resource = $this->getCurrentCart();
170
171
        if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid($resource->getId(), $request->get('_csrf_token'))) {
172
            throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.');
173
        }
174
175
        $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource);
176
177
        if ($event->isStopped() && !$configuration->isHtmlRequest()) {
178
            throw new HttpException($event->getErrorCode(), $event->getMessage());
179
        }
180
        if ($event->isStopped()) {
181
            $this->flashHelper->addFlashFromEvent($configuration, $event);
182
183
            return $this->redirectHandler->redirectToIndex($configuration, $resource);
184
        }
185
186
        $this->repository->remove($resource);
187
        $this->eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource);
188
189
        if (!$configuration->isHtmlRequest()) {
190
            return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT));
191
        }
192
193
        $this->flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource);
194
195
        return $this->redirectHandler->redirectToIndex($configuration, $resource);
196
    }
197
198
    /**
199
     * @param RequestConfiguration $configuration
200
     *
201
     * @return RedirectResponse
202
     */
203
    protected function redirectToCartSummary(RequestConfiguration $configuration)
204
    {
205
        if (null === $configuration->getParameters()->get('redirect')) {
206
            return $this->redirectHandler->redirectToRoute($configuration, $this->getCartSummaryRoute());
207
        }
208
209
        return $this->redirectHandler->redirectToRoute($configuration, $configuration->getParameters()->get('redirect'));
210
    }
211
212
    /**
213
     * @return string
214
     */
215
    protected function getCartSummaryRoute()
216
    {
217
        return 'sylius_cart_summary';
218
    }
219
220
    /**
221
     * @return OrderInterface
222
     */
223
    protected function getCurrentCart()
224
    {
225
        return $this->getContext()->getCart();
226
    }
227
228
    /**
229
     * @return CartContextInterface
230
     */
231
    protected function getContext()
232
    {
233
        return $this->get('sylius.context.cart');
234
    }
235
236
    /**
237
     * @return EventDispatcherInterface
238
     */
239
    protected function getEventDispatcher()
240
    {
241
        return $this->container->get('event_dispatcher');
242
    }
243
}
244