Passed
Pull Request — master (#84)
by
unknown
03:48
created

AddSelectedProductsToCart::handleCartItems()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 22
rs 9.4555
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
        $this->wishlistContext = $wishlistContext;
53
        $this->cartContext = $cartContext;
54
        $this->formFactory = $formFactory;
55
        $this->orderModifier = $orderModifier;
56
        $this->flashBag = $flashBag;
57
        $this->twigEnvironment = $twigEnvironment;
58
        $this->cartManager = $cartManager;
59
        $this->translator = $translator;
60
        $this->itemQuantityModifier = $itemQuantityModifier;
61
    }
62
63
    public function __invoke(Request $request): Response
64
    {
65
        $wishlist = $this->wishlistContext->getWishlist($request);
66
        $cart = $this->cartContext->getCart();
67
68
        $commandsArray = new ArrayCollection();
69
70
        foreach ($wishlist->getWishlistProducts() as $wishlistProductItem) {
71
            $wishlistProductCommand = new AddWishlistProduct();
72
            $wishlistProductCommand->setWishlistProduct($wishlistProductItem);
73
            $commandsArray->add($wishlistProductCommand);
74
        }
75
76
        $form = $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
77
            'cart' => $cart,
78
79
        ]);
80
81
        $form->handleRequest($request);
82
83
        if ($form->isSubmitted() && $form->isValid()) {
84
            $this->handleCartItems($form->getData());
85
86
            return new Response(
87
                $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
88
                    'wishlist' => $wishlist,
89
                    'form' => $form->createView(),
90
                ])
91
            );
92
        }
93
94
        foreach ($form->getErrors() as $error) {
95
            $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

95
            $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...
96
        }
97
98
        return new Response(
99
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
100
                'wishlist' => $wishlist,
101
                'form' => $form->createView(),
102
            ])
103
        );
104
    }
105
106
    private function handleCartItems(array $wishlistProductsCommand): void
107
    {
108
        foreach ($wishlistProductsCommand as $wishlistProducts) {
109
            /** @var AddWishlistProduct $wishlistProduct */
110
            foreach ($wishlistProducts as $wishlistProduct) {
111
                $addToCartCommand = $wishlistProduct->getCartItem();
112
                $cart = $addToCartCommand->getCart();
113
                $cartItem = $addToCartCommand->getCartItem();
114
                if (0 >= $cartItem->getVariant()->getOnHand()) {
115
                    $message = sprintf('%s does not have sufficient stock.', $cartItem->getProductName());
116
                    $this->flashBag->add('error', $this->translator->trans($message));
117
                } else {
118
                    $this->itemQuantityModifier->modify($cartItem, 1);
119
                    $this->orderModifier->addToOrder($cart, $cartItem);
120
                    $this->cartManager->persist($cart);
121
                    if (!$this->flashBag->has('success')) {
122
                        $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_to_cart'));
123
                    }
124
                }
125
            }
126
        }
127
        $this->cartManager->flush();
128
    }
129
}
130