Completed
Push — master ( 7c0075...645752 )
by Kamil
18:52
created

OrderItemController::createNew()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 8
nc 3
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\OrderModifierInterface;
24
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25
use Symfony\Component\Form\FormFactoryInterface;
26
use Symfony\Component\HttpFoundation\RedirectResponse;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpFoundation\Response;
29
use Symfony\Component\HttpKernel\Exception\HttpException;
30
31
/**
32
 * @author Paweł Jędrzejewski <[email protected]>
33
 */
34
class OrderItemController extends ResourceController
35
{
36
    /**
37
     * @param Request $request
38
     *
39
     * @return Response
40
     */
41
    public function addAction(Request $request)
42
    {
43
        $cart = $this->getCurrentCart();
44
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
45
46
        $this->isGrantedOr403($configuration, CartActions::ADD);
47
        /** @var OrderItemInterface $orderItem */
48
        $orderItem = $this->newResourceFactory->create($configuration, $this->factory);
49
50
        $form = $this->getFormFactory()->create(
51
            $configuration->getFormType(),
52
            $this->createAddToCartCommand($cart, $orderItem),
53
            $configuration->getFormOptions()
54
        );
55
56
        if ($request->isMethod('POST') && $form->submit($request)->isValid()) {
57
            /** @var AddToCartCommandInterface $addCartItemCommand */
58
            $addToCartCommand = $form->getData();
59
60
            $event = $this->eventDispatcher->dispatchPreEvent(CartActions::ADD, $configuration, $orderItem);
61
62
            if ($event->isStopped() && !$configuration->isHtmlRequest()) {
63
                throw new HttpException($event->getErrorCode(), $event->getMessage());
64
            }
65
            if ($event->isStopped()) {
66
                $this->flashHelper->addFlashFromEvent($configuration, $event);
67
68
                return $this->redirectHandler->redirectToIndex($configuration, $orderItem);
69
            }
70
71
            $this->getOrderModifier()->addToOrder($addToCartCommand->getCart(), $addToCartCommand->getCartItem());
72
73
            $cartManager = $this->getCartManager();
74
            $cartManager->persist($cart);
75
            $cartManager->flush();
76
77
            $this->eventDispatcher->dispatchPostEvent(CartActions::ADD, $configuration, $orderItem);
78
79
            if (!$configuration->isHtmlRequest()) {
80
                return $this->viewHandler->handle($configuration, View::create($orderItem, Response::HTTP_CREATED));
81
            }
82
            $this->flashHelper->addSuccessFlash($configuration, CartActions::ADD, $orderItem);
83
84
            return $this->redirectHandler->redirectToResource($configuration, $orderItem);
85
        }
86
87
        if (!$configuration->isHtmlRequest()) {
88
            return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST));
89
        }
90
91
        $view = View::create()
92
            ->setData([
93
                'configuration' => $configuration,
94
                $this->metadata->getName() => $orderItem,
95
                'form' => $form->createView(),
96
            ])
97
            ->setTemplate($configuration->getTemplate(CartActions::ADD . '.html'))
98
        ;
99
100
        return $this->viewHandler->handle($configuration, $view);
101
    }
102
103
    /**
104
     * @param Request $request
105
     *
106
     * @return Response
107
     */
108
    public function removeAction(Request $request)
109
    {
110
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
111
112
        $this->isGrantedOr403($configuration, CartActions::REMOVE);
113
        $orderItem = $this->findOr404($configuration);
114
115
        $event = $this->eventDispatcher->dispatchPreEvent(CartActions::REMOVE, $configuration, $orderItem);
116
117
        if ($event->isStopped() && !$configuration->isHtmlRequest()) {
118
            throw new HttpException($event->getErrorCode(), $event->getMessage());
119
        }
120
        if ($event->isStopped()) {
121
            $this->flashHelper->addFlashFromEvent($configuration, $event);
122
123
            return $this->redirectHandler->redirectToIndex($configuration, $orderItem);
124
        }
125
126
        $cart = $this->getCurrentCart();
127
        $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...
128
129
        $this->repository->remove($orderItem);
130
131
        $cartManager = $this->getCartManager();
132
        $cartManager->persist($cart);
133
        $cartManager->flush();
134
135
        $this->eventDispatcher->dispatchPostEvent(CartActions::REMOVE, $configuration, $orderItem);
136
137
        if (!$configuration->isHtmlRequest()) {
138
            return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT));
139
        }
140
141
        $this->flashHelper->addSuccessFlash($configuration, CartActions::REMOVE, $orderItem);
142
143
        return $this->redirectHandler->redirectToIndex($configuration, $orderItem);
144
    }
145
146
    /**
147
     * @return ObjectRepository
148
     */
149
    protected function getOrderRepository()
150
    {
151
        return $this->get('sylius.repository.order');
152
    }
153
154
    /**
155
     * @param RequestConfiguration $configuration
156
     *
157
     * @return RedirectResponse
158
     */
159
    protected function redirectToCartSummary(RequestConfiguration $configuration)
160
    {
161
        if (null === $configuration->getParameters()->get('redirect')) {
162
            return $this->redirectHandler->redirectToRoute($configuration, $this->getCartSummaryRoute());
163
        }
164
165
        return $this->redirectHandler->redirectToRoute($configuration, $configuration->getParameters()->get('redirect'));
166
    }
167
168
    /**
169
     * @return string
170
     */
171
    protected function getCartSummaryRoute()
172
    {
173
        return 'sylius_cart_summary';
174
    }
175
176
    /**
177
     * @return OrderInterface
178
     */
179
    protected function getCurrentCart()
180
    {
181
        return $this->getContext()->getCart();
182
    }
183
184
    /**
185
     * @return CartContextInterface
186
     */
187
    protected function getContext()
188
    {
189
        return $this->get('sylius.context.cart');
190
    }
191
192
    /**
193
     * @return EventDispatcherInterface
194
     */
195
    protected function getEventDispatcher()
196
    {
197
        return $this->get('event_dispatcher');
198
    }
199
200
    /**
201
     * @param OrderInterface $cart
202
     * @param OrderItemInterface $cartItem
203
     *
204
     * @return AddToCartCommandInterface
205
     */
206
    protected function createAddToCartCommand(OrderInterface $cart, OrderItemInterface $cartItem)
207
    {
208
        return $this->get('sylius.factory.add_to_cart_command')->createForCartAndCartItem($cart, $cartItem);
209
    }
210
211
    /**
212
     * @return FormFactoryInterface
213
     */
214
    private function getFormFactory()
215
    {
216
        return $this->get('form.factory');
217
    }
218
219
    /**
220
     * @return OrderModifierInterface
221
     */
222
    private function getOrderModifier()
223
    {
224
        return $this->get('sylius.order_modifier');
225
    }
226
227
    /**
228
     * @return EntityManagerInterface
229
     */
230
    private function getCartManager()
231
    {
232
        return $this->get('sylius.manager.order');
233
    }
234
}
235