Passed
Pull Request — master (#93)
by
unknown
04:02
created

AddSelectedProductsToCart   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 58
c 1
b 0
f 0
dl 0
loc 112
rs 10
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 44 6
A __construct() 0 20 1
A handleCartItems() 0 22 6
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
        $this->wishlistContext = $wishlistContext;
59
        $this->cartContext = $cartContext;
60
        $this->formFactory = $formFactory;
61
        $this->orderModifier = $orderModifier;
62
        $this->flashBag = $flashBag;
63
        $this->twigEnvironment = $twigEnvironment;
64
        $this->cartManager = $cartManager;
65
        $this->translator = $translator;
66
        $this->itemQuantityModifier = $itemQuantityModifier;
67
    }
68
69
    public function __invoke(Request $request): Response
70
    {
71
        $wishlist = $this->wishlistContext->getWishlist($request);
72
        $cart = $this->cartContext->getCart();
73
74
        $commandsArray = [];
75
76
        foreach ($wishlist->getWishlistProducts() as $wishlistProductItem) {
77
            $wishlistProductCommand = new AddWishlistProduct();
78
            $wishlistProductCommand->setWishlistProduct($wishlistProductItem);
79
            $commandsArray[] = $wishlistProductCommand;
80
        }
81
82
        $form = $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
83
            'cart' => $cart,
84
        ]);
85
86
        $form->handleRequest($request);
87
88
        if ($form->isSubmitted() && $form->isValid()) {
89
            $wishlistProducts = $form->get('items')->getData();
90
91
            if ($this->handleCartItems($wishlistProducts)) {
92
                $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_selected_wishlist_items_to_cart'));
93
            } else {
94
                $this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products'));
95
            }
96
97
            return new Response(
98
                $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
99
                    'wishlist' => $wishlist,
100
                    'form' => $form->createView(),
101
                ])
102
            );
103
        }
104
105
        foreach ($form->getErrors() as $error) {
106
            $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

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