Completed
Push — master ( fcfa74...4ea0bb )
by Paweł
20s
created

OrderItemController::getEventDispatcher()   A

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
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\Common\Persistence\ObjectRepository;
15
use Doctrine\ORM\EntityManagerInterface;
16
use FOS\RestBundle\View\View;
17
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
18
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
19
use Sylius\Component\Order\CartActions;
20
use Sylius\Component\Order\Context\CartContextInterface;
21
use Sylius\Component\Order\Model\OrderInterface;
22
use Sylius\Component\Order\Model\OrderItemInterface;
23
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
24
use Sylius\Component\Order\Modifier\OrderModifierInterface;
25
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
26
use Symfony\Component\Form\FormFactoryInterface;
27
use Symfony\Component\HttpFoundation\RedirectResponse;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpFoundation\Response;
30
use Symfony\Component\HttpKernel\Exception\HttpException;
31
32
/**
33
 * @author Paweł Jędrzejewski <[email protected]>
34
 */
35
class OrderItemController extends ResourceController
36
{
37
    /**
38
     * @param Request $request
39
     *
40
     * @return Response
41
     */
42
    public function addAction(Request $request)
43
    {
44
        $cart = $this->getCurrentCart();
45
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
46
47
        $this->isGrantedOr403($configuration, CartActions::ADD);
48
        /** @var OrderItemInterface $orderItem */
49
        $orderItem = $this->newResourceFactory->create($configuration, $this->factory);
50
51
        $this->getQuantityModifier()->modify($orderItem, 1);
52
53
        $form = $this->getFormFactory()->create(
54
            $configuration->getFormType(),
55
            $this->createAddToCartCommand($cart, $orderItem),
56
            $configuration->getFormOptions()
57
        );
58
59
        if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
60
            /** @var AddToCartCommandInterface $addCartItemCommand */
61
            $addToCartCommand = $form->getData();
62
63
            $event = $this->eventDispatcher->dispatchPreEvent(CartActions::ADD, $configuration, $orderItem);
64
65
            if ($event->isStopped() && !$configuration->isHtmlRequest()) {
66
                throw new HttpException($event->getErrorCode(), $event->getMessage());
67
            }
68
            if ($event->isStopped()) {
69
                $this->flashHelper->addFlashFromEvent($configuration, $event);
70
71
                return $this->redirectHandler->redirectToIndex($configuration, $orderItem);
72
            }
73
74
            $this->getOrderModifier()->addToOrder($addToCartCommand->getCart(), $addToCartCommand->getCartItem());
75
76
            $cartManager = $this->getCartManager();
77
            $cartManager->persist($cart);
78
            $cartManager->flush();
79
80
            $this->eventDispatcher->dispatchPostEvent(CartActions::ADD, $configuration, $orderItem);
81
82
            $this->flashHelper->addSuccessFlash($configuration, CartActions::ADD, $orderItem);
83
84
            if ($request->isXmlHttpRequest()) {
85
                return $this->viewHandler->handle($configuration, View::create([], Response::HTTP_CREATED));
86
            }
87
88
            return $this->redirectHandler->redirectToResource($configuration, $orderItem);
89
        }
90
91
        if (!$configuration->isHtmlRequest()) {
92
            return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST));
93
        }
94
95
        $view = View::create()
96
            ->setData([
97
                'configuration' => $configuration,
98
                $this->metadata->getName() => $orderItem,
99
                'form' => $form->createView(),
100
            ])
101
            ->setTemplate($configuration->getTemplate(CartActions::ADD . '.html'))
102
        ;
103
104
        return $this->viewHandler->handle($configuration, $view);
105
    }
106
107
    /**
108
     * @param Request $request
109
     *
110
     * @return Response
111
     */
112
    public function removeAction(Request $request)
113
    {
114
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
115
116
        $this->isGrantedOr403($configuration, CartActions::REMOVE);
117
        $orderItem = $this->findOr404($configuration);
118
119
        $event = $this->eventDispatcher->dispatchPreEvent(CartActions::REMOVE, $configuration, $orderItem);
120
121
        if ($event->isStopped() && !$configuration->isHtmlRequest()) {
122
            throw new HttpException($event->getErrorCode(), $event->getMessage());
123
        }
124
        if ($event->isStopped()) {
125
            $this->flashHelper->addFlashFromEvent($configuration, $event);
126
127
            return $this->redirectHandler->redirectToIndex($configuration, $orderItem);
128
        }
129
130
        $cart = $this->getCurrentCart();
131
        $this->getOrderModifier()->removeFromOrder($cart, $orderItem);
0 ignored issues
show
Compatibility introduced by
$orderItem of type object<Sylius\Component\...odel\ResourceInterface> is not a sub-type of object<Sylius\Component\...del\OrderItemInterface>. It seems like you assume a child interface of the interface Sylius\Component\Resource\Model\ResourceInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
132
133
        $this->repository->remove($orderItem);
134
135
        $cartManager = $this->getCartManager();
136
        $cartManager->persist($cart);
137
        $cartManager->flush();
138
139
        $this->eventDispatcher->dispatchPostEvent(CartActions::REMOVE, $configuration, $orderItem);
140
141
        if (!$configuration->isHtmlRequest()) {
142
            return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT));
143
        }
144
145
        $this->flashHelper->addSuccessFlash($configuration, CartActions::REMOVE, $orderItem);
146
147
        return $this->redirectHandler->redirectToIndex($configuration, $orderItem);
148
    }
149
150
    /**
151
     * @return ObjectRepository
152
     */
153
    protected function getOrderRepository()
154
    {
155
        return $this->get('sylius.repository.order');
156
    }
157
158
    /**
159
     * @param RequestConfiguration $configuration
160
     *
161
     * @return RedirectResponse
162
     */
163
    protected function redirectToCartSummary(RequestConfiguration $configuration)
164
    {
165
        if (null === $configuration->getParameters()->get('redirect')) {
166
            return $this->redirectHandler->redirectToRoute($configuration, $this->getCartSummaryRoute());
167
        }
168
169
        return $this->redirectHandler->redirectToRoute($configuration, $configuration->getParameters()->get('redirect'));
170
    }
171
172
    /**
173
     * @return string
174
     */
175
    protected function getCartSummaryRoute()
176
    {
177
        return 'sylius_cart_summary';
178
    }
179
180
    /**
181
     * @return OrderInterface
182
     */
183
    protected function getCurrentCart()
184
    {
185
        return $this->getContext()->getCart();
186
    }
187
188
    /**
189
     * @return CartContextInterface
190
     */
191
    protected function getContext()
192
    {
193
        return $this->get('sylius.context.cart');
194
    }
195
196
    /**
197
     * @param OrderInterface $cart
198
     * @param OrderItemInterface $cartItem
199
     *
200
     * @return AddToCartCommandInterface
201
     */
202
    protected function createAddToCartCommand(OrderInterface $cart, OrderItemInterface $cartItem)
203
    {
204
        return $this->get('sylius.factory.add_to_cart_command')->createWithCartAndCartItem($cart, $cartItem);
205
    }
206
207
    /**
208
     * @return FormFactoryInterface
209
     */
210
    protected function getFormFactory()
211
    {
212
        return $this->get('form.factory');
213
    }
214
215
    /**
216
     * @return OrderItemQuantityModifierInterface
217
     */
218
    protected function getQuantityModifier()
219
    {
220
        return $this->get('sylius.order_item_quantity_modifier');
221
    }
222
223
    /**
224
     * @return OrderModifierInterface
225
     */
226
    protected function getOrderModifier()
227
    {
228
        return $this->get('sylius.order_modifier');
229
    }
230
231
    /**
232
     * @return EntityManagerInterface
233
     */
234
    protected function getCartManager()
235
    {
236
        return $this->get('sylius.manager.order');
237
    }
238
}
239