Completed
Push — master ( 52d945...1f454c )
by Paweł
18:12 queued 07:41
created

OrderController::widgetAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

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 9
nc 2
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\OrderBundle\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
     * @param Request $request
62
     *
63
     * @return Response
64
     */
65
    public function widgetAction(Request $request)
66
    {
67
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
68
69
        $cart = $this->getCurrentCart();
70
71
        if (!$configuration->isHtmlRequest()) {
72
            return $this->viewHandler->handle($configuration, View::create($cart));
73
        }
74
75
        $view = View::create()
76
            ->setTemplate($configuration->getTemplate('summary.html'))
77
            ->setData(['cart' => $cart])
78
        ;
79
80
        return $this->viewHandler->handle($configuration, $view);
81
    }
82
83
    /**
84
     * @param Request $request
85
     *
86
     * @return Response
87
     */
88
    public function saveAction(Request $request)
89
    {
90
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
91
92
        $this->isGrantedOr403($configuration, ResourceActions::UPDATE);
93
        $resource = $this->getCurrentCart();
94
95
        $form = $this->resourceFormFactory->create($configuration, $resource);
96
97
        if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true) && $form->handleRequest($request)->isValid()) {
98
            $resource = $form->getData();
99
100
            $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::UPDATE, $configuration, $resource);
101
102
            if ($event->isStopped() && !$configuration->isHtmlRequest()) {
103
                throw new HttpException($event->getErrorCode(), $event->getMessage());
104
            }
105
            if ($event->isStopped()) {
106
                $this->flashHelper->addFlashFromEvent($configuration, $event);
107
108
                return $this->redirectHandler->redirectToResource($configuration, $resource);
109
            }
110
111
            if ($configuration->hasStateMachine()) {
112
                $this->stateMachine->apply($configuration, $resource);
113
            }
114
115
            $this->eventDispatcher->dispatchPostEvent(ResourceActions::UPDATE, $configuration, $resource);
116
117
            if (!$configuration->isHtmlRequest()) {
118
                return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT));
119
            }
120
121
            $this->getEventDispatcher()->dispatch(SyliusCartEvents::CART_CHANGE, new GenericEvent($resource));
122
            $this->manager->flush();
123
124
            $this->flashHelper->addSuccessFlash($configuration, ResourceActions::UPDATE, $resource);
125
126
            return $this->redirectHandler->redirectToResource($configuration, $resource);
127
        }
128
129
        if (!$configuration->isHtmlRequest()) {
130
            return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST));
131
        }
132
133
        $view = View::create()
134
            ->setData([
135
                'configuration' => $configuration,
136
                $this->metadata->getName() => $resource,
137
                'form' => $form->createView(),
138
                'cart' => $resource,
139
            ])
140
            ->setTemplate($configuration->getTemplate(ResourceActions::UPDATE . '.html'))
141
        ;
142
143
        return $this->viewHandler->handle($configuration, $view);
144
    }
145
146
    /**
147
     * @param Request $request
148
     *
149
     * @return Response
150
     */
151
    public function clearAction(Request $request)
152
    {
153
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
154
155
        $this->isGrantedOr403($configuration, ResourceActions::DELETE);
156
        $resource = $this->getCurrentCart();
157
158
        if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid($resource->getId(), $request->get('_csrf_token'))) {
159
            throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.');
160
        }
161
162
        $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::DELETE, $configuration, $resource);
163
164
        if ($event->isStopped() && !$configuration->isHtmlRequest()) {
165
            throw new HttpException($event->getErrorCode(), $event->getMessage());
166
        }
167
        if ($event->isStopped()) {
168
            $this->flashHelper->addFlashFromEvent($configuration, $event);
169
170
            return $this->redirectHandler->redirectToIndex($configuration, $resource);
171
        }
172
173
        $this->repository->remove($resource);
174
        $this->eventDispatcher->dispatchPostEvent(ResourceActions::DELETE, $configuration, $resource);
175
176
        if (!$configuration->isHtmlRequest()) {
177
            return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT));
178
        }
179
180
        $this->flashHelper->addSuccessFlash($configuration, ResourceActions::DELETE, $resource);
181
182
        return $this->redirectHandler->redirectToIndex($configuration, $resource);
183
    }
184
185
    /**
186
     * @param RequestConfiguration $configuration
187
     *
188
     * @return RedirectResponse
189
     */
190
    protected function redirectToCartSummary(RequestConfiguration $configuration)
191
    {
192
        if (null === $configuration->getParameters()->get('redirect')) {
193
            return $this->redirectHandler->redirectToRoute($configuration, $this->getCartSummaryRoute());
194
        }
195
196
        return $this->redirectHandler->redirectToRoute($configuration, $configuration->getParameters()->get('redirect'));
197
    }
198
199
    /**
200
     * @return string
201
     */
202
    protected function getCartSummaryRoute()
203
    {
204
        return 'sylius_cart_summary';
205
    }
206
207
    /**
208
     * @return OrderInterface
209
     */
210
    protected function getCurrentCart()
211
    {
212
        return $this->getContext()->getCart();
213
    }
214
215
    /**
216
     * @return CartContextInterface
217
     */
218
    protected function getContext()
219
    {
220
        return $this->get('sylius.context.cart');
221
    }
222
223
    /**
224
     * @return EventDispatcherInterface
225
     */
226
    protected function getEventDispatcher()
227
    {
228
        return $this->container->get('event_dispatcher');
229
    }
230
}
231