Passed
Pull Request — master (#85)
by
unknown
03:29
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
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusWishlistPlugin\Controller\Action;
12
13
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistProduct;
14
use BitBag\SyliusWishlistPlugin\Context\WishlistContextInterface;
15
use BitBag\SyliusWishlistPlugin\Form\Type\WishlistCollectionType;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Sylius\Component\Order\Context\CartContextInterface;
18
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
19
use Sylius\Component\Order\Modifier\OrderModifierInterface;
20
use Symfony\Component\Form\FormFactoryInterface;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
24
use Symfony\Contracts\Translation\TranslatorInterface;
25
use Twig\Environment;
26
27
final class AddSelectedProductsToCart
28
{
29
    private WishlistContextInterface $wishlistContext;
30
31
    private CartContextInterface $cartContext;
32
33
    private FormFactoryInterface $formFactory;
34
35
    private OrderModifierInterface $orderModifier;
36
37
    private EntityManagerInterface $cartManager;
38
39
    private FlashBagInterface $flashBag;
40
41
    private TranslatorInterface $translator;
42
43
    private Environment $twigEnvironment;
44
45
    private OrderItemQuantityModifierInterface $itemQuantityModifier;
46
47
    public function __construct(
48
        WishlistContextInterface           $wishlistContext,
49
        CartContextInterface               $cartContext,
50
        FormFactoryInterface               $formFactory,
51
        OrderModifierInterface             $orderModifier,
52
        EntityManagerInterface             $cartManager,
53
        FlashBagInterface                  $flashBag,
54
        TranslatorInterface                $translator,
55
        Environment                        $twigEnvironment,
56
        OrderItemQuantityModifierInterface $itemQuantityModifier
57
    )
58
    {
59
        $this->wishlistContext = $wishlistContext;
60
        $this->cartContext = $cartContext;
61
        $this->formFactory = $formFactory;
62
        $this->orderModifier = $orderModifier;
63
        $this->flashBag = $flashBag;
64
        $this->twigEnvironment = $twigEnvironment;
65
        $this->cartManager = $cartManager;
66
        $this->translator = $translator;
67
        $this->itemQuantityModifier = $itemQuantityModifier;
68
    }
69
70
    public function __invoke(Request $request): Response
71
    {
72
        $wishlist = $this->wishlistContext->getWishlist($request);
73
        $cart = $this->cartContext->getCart();
74
75
        $commandsArray = [];
76
77
        foreach ($wishlist->getWishlistProducts() as $wishlistProductItem) {
78
            $wishlistProductCommand = new AddWishlistProduct();
79
            $wishlistProductCommand->setWishlistProduct($wishlistProductItem);
80
            $commandsArray[] = $wishlistProductCommand;
81
        }
82
83
        $form = $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
84
            'cart' => $cart,
85
86
        ]);
87
88
        $form->handleRequest($request);
89
90
        if ($form->isSubmitted() && $form->isValid()) {
91
92
            $wishlistProducts = $form->get("items")->getData();
93
94
            if ($this->handleCartItems($wishlistProducts)) {
95
                $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_selected_wishlist_items_to_cart'));
96
            } else {
97
                $this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products'));
98
            }
99
100
            return new Response(
101
                $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
102
                    'wishlist' => $wishlist,
103
                    'form' => $form->createView(),
104
                ])
105
            );
106
        }
107
108
        foreach ($form->getErrors() as $error) {
109
            $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

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