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

AddSelectedProductsToCart::handleCartItems()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
nc 4
nop 1
dl 0
loc 22
rs 9.8666
c 1
b 0
f 0
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\ORM\EntityManagerInterface;
10
use Sylius\Component\Order\Context\CartContextInterface;
11
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
12
use Sylius\Component\Order\Modifier\OrderModifierInterface;
13
use Symfony\Component\Form\FormFactoryInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
17
use Symfony\Contracts\Translation\TranslatorInterface;
18
use Twig\Environment;
19
20
final class AddSelectedProductsToCart
21
{
22
    private WishlistContextInterface $wishlistContext;
23
24
    private CartContextInterface $cartContext;
25
26
    private FormFactoryInterface $formFactory;
27
28
    private OrderModifierInterface $orderModifier;
29
30
    private EntityManagerInterface $cartManager;
31
32
    private FlashBagInterface $flashBag;
33
34
    private TranslatorInterface $translator;
35
36
    private Environment $twigEnvironment;
37
38
    private OrderItemQuantityModifierInterface $itemQuantityModifier;
39
40
    public function __construct(
41
        WishlistContextInterface           $wishlistContext,
42
        CartContextInterface               $cartContext,
43
        FormFactoryInterface               $formFactory,
44
        OrderModifierInterface             $orderModifier,
45
        EntityManagerInterface             $cartManager,
46
        FlashBagInterface                  $flashBag,
47
        TranslatorInterface                $translator,
48
        Environment                        $twigEnvironment,
49
        OrderItemQuantityModifierInterface $itemQuantityModifier
50
    )
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 = [];
69
70
        foreach ($wishlist->getWishlistProducts() as $wishlistProductItem) {
71
            $wishlistProductCommand = new AddWishlistProduct();
72
            $wishlistProductCommand->setWishlistProduct($wishlistProductItem);
73
            $commandsArray[] = $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
85
            $wishlistProducts = $form->get("items")->getData();
86
87
            if ($this->handleCartItems($wishlistProducts)) {
88
                $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_selected_wishlist_items_to_cart'));
89
            } else {
90
                $this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products'));
91
            }
92
93
            return new Response(
94
                $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
95
                    'wishlist' => $wishlist,
96
                    'form' => $form->createView(),
97
                ])
98
            );
99
        }
100
101
        foreach ($form->getErrors() as $error) {
102
            $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

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