Passed
Pull Request — master (#84)
by
unknown
04:33
created

AddSelectedProductsToCart   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
dl 0
loc 114
rs 10
c 1
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 43 6
A handleCartItems() 0 24 5
A __construct() 0 21 1
1
<?php
2
declare(strict_types=1);
3
4
namespace BitBag\SyliusWishlistPlugin\Controller\Action;
5
6
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistProduct;
7
use BitBag\SyliusWishlistPlugin\Context\WishlistContextInterface;
8
use BitBag\SyliusWishlistPlugin\Form\Type\WishlistCollectionType;
9
use Doctrine\Common\Collections\ArrayCollection;
10
use Doctrine\ORM\EntityManagerInterface;
11
use Sylius\Component\Order\Context\CartContextInterface;
12
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
13
use Sylius\Component\Order\Modifier\OrderModifierInterface;
14
use Symfony\Component\Form\FormFactoryInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
18
use Symfony\Contracts\Translation\TranslatorInterface;
19
use Twig\Environment;
20
21
final class AddSelectedProductsToCart
22
{
23
    private WishlistContextInterface $wishlistContext;
24
25
    private CartContextInterface $cartContext;
26
27
    private FormFactoryInterface $formFactory;
28
29
    private OrderModifierInterface $orderModifier;
30
31
    private EntityManagerInterface $cartManager;
32
33
    private FlashBagInterface $flashBag;
34
35
    private TranslatorInterface $translator;
36
37
    private Environment $twigEnvironment;
38
39
    private OrderItemQuantityModifierInterface $itemQuantityModifier;
40
41
    public function __construct(
42
        WishlistContextInterface           $wishlistContext,
43
        CartContextInterface               $cartContext,
44
        FormFactoryInterface               $formFactory,
45
        OrderModifierInterface             $orderModifier,
46
        EntityManagerInterface             $cartManager,
47
        FlashBagInterface                  $flashBag,
48
        TranslatorInterface                $translator,
49
        Environment                        $twigEnvironment,
50
        OrderItemQuantityModifierInterface $itemQuantityModifier
51
    )
52
    {
53
        $this->wishlistContext = $wishlistContext;
54
        $this->cartContext = $cartContext;
55
        $this->formFactory = $formFactory;
56
        $this->orderModifier = $orderModifier;
57
        $this->flashBag = $flashBag;
58
        $this->twigEnvironment = $twigEnvironment;
59
        $this->cartManager = $cartManager;
60
        $this->translator = $translator;
61
        $this->itemQuantityModifier = $itemQuantityModifier;
62
    }
63
64
    public function __invoke(Request $request): Response
65
    {
66
        $wishlist = $this->wishlistContext->getWishlist($request);
67
        $cart = $this->cartContext->getCart();
68
69
        $commandsArray = new ArrayCollection();
70
71
        foreach ($wishlist->getWishlistProducts() as $wishlistProductItem) {
72
            $wishlistProductCommand = new AddWishlistProduct();
73
            $wishlistProductCommand->setWishlistProduct($wishlistProductItem);
74
            $commandsArray->add($wishlistProductCommand);
75
        }
76
77
        $form = $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
78
            'cart' => $cart,
79
80
        ]);
81
82
        $form->handleRequest($request);
83
84
        if ($form->isSubmitted() && $form->isValid()) {
85
            if ($this->handleCartItems($form->getData())) {
86
                $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_selected_wishlist_items_to_cart'));
87
            } else {
88
                $this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products'));
89
            }
90
91
            return new Response(
92
                $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
93
                    'wishlist' => $wishlist,
94
                    'form' => $form->createView(),
95
                ])
96
            );
97
        }
98
99
        foreach ($form->getErrors() as $error) {
100
            $this->flashBag->add('error', $error->getMessage());
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Symfony\Component\Form\FormErrorIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
            $this->flashBag->add('error', $error->/** @scrutinizer ignore-call */ getMessage());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
        }
102
103
        return new Response(
104
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
105
                'wishlist' => $wishlist,
106
                'form' => $form->createView(),
107
            ])
108
        );
109
    }
110
111
    private function handleCartItems(array $wishlistProductsCommand): bool
112
    {
113
        $result = false;
114
115
        foreach($wishlistProductsCommand as $wishlistProducts) {
116
            /** @var AddWishlistProduct $wishlistProduct */
117
            foreach ($wishlistProducts as $wishlistProduct) {
118
                if ($wishlistProduct->isSelected()) {
119
                    $result = true;
120
                    $cartItem = $wishlistProduct->getCartItem()->getCartItem();
121
                    $cart = $wishlistProduct->getCartItem()->getCart();
122
123
                    if (0 === $cartItem->getQuantity()) {
124
                        $this->itemQuantityModifier->modify($cartItem, 1);
125
                    }
126
127
                    $this->orderModifier->addToOrder($cart, $cartItem);
128
                    $this->cartManager->persist($cart);
129
                }
130
            }
131
        }
132
        $this->cartManager->flush();
133
134
        return $result;
135
    }
136
}
137