Completed
Push — master ( 5bc6c3...0c8ba6 )
by Mikołaj
19:22
created

OrderItemController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 12
dl 0
loc 44
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A addAction() 0 41 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusWishlistPlugin\Controller;
6
7
use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
8
use Sylius\Bundle\OrderBundle\Controller\OrderItemController as BaseController;
9
use Sylius\Component\Core\Model\OrderItemInterface;
10
use Sylius\Component\Order\CartActions;
11
use Symfony\Component\Form\SubmitButton;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15
16
final class OrderItemController extends BaseController
17
{
18
    public function addAction(Request $request): Response
19
    {
20
        $cart = $this->getCurrentCart();
21
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
22
23
        $this->isGrantedOr403($configuration, CartActions::ADD);
24
        /** @var OrderItemInterface $orderItem */
25
        $orderItem = $this->newResourceFactory->create($configuration, $this->factory);
26
27
        $this->getQuantityModifier()->modify($orderItem, 1);
28
29
        $form = $this->getFormFactory()->create(
30
            $configuration->getFormType(),
31
            $this->createAddToCartCommand($cart, $orderItem),
32
            $configuration->getFormOptions()
33
        );
34
35
        $form->handleRequest($request);
36
37
        /** @var SubmitButton $addToWishlist */
38
        $addToWishlist = $form->get('addToWishlist');
39
40
        if ($addToWishlist->isClicked()) {
41
            /** @var AddToCartCommandInterface $addToCartCommand */
42
            $addToCartCommand = $form->getData();
43
44
            /** @var OrderItemInterface $item */
45
            $item = $addToCartCommand->getCartItem();
46
            $variant = $item->getVariant();
47
48
            if ($variant === null) {
49
                throw new NotFoundHttpException('Could not find variant');
50
            }
51
52
            return new Response($this->generateUrl('bitbag_sylius_wishlist_plugin_shop_wishlist_add_product_variant', [
53
                'variantId' => $variant->getId(),
54
            ]));
55
        }
56
57
        return parent::addAction($request);
58
    }
59
}
60