Passed
Pull Request — master (#81)
by
unknown
03:50
created

ShowChosenWishlistAction::handleCartItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 16
rs 10
c 0
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\Entity\WishlistInterface;
14
use BitBag\SyliusWishlistPlugin\Form\Type\AddProductsToCartType;
15
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
18
use Sylius\Component\Order\Context\CartContextInterface;
19
use Sylius\Component\Order\Modifier\OrderModifierInterface;
20
use Symfony\Component\Form\FormFactoryInterface;
21
use Symfony\Component\Form\FormInterface;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
25
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
26
use Symfony\Contracts\Translation\TranslatorInterface;
27
use Twig\Environment;
28
29
final class ShowChosenWishlistAction
30
{
31
    private WishlistRepositoryInterface $wishlistRepository;
32
33
    private CartContextInterface $cartContext;
34
35
    private FormFactoryInterface $formFactory;
36
37
    private OrderModifierInterface $orderModifier;
38
39
    private EntityManagerInterface $cartManager;
40
41
    private FlashBagInterface $flashBag;
42
43
    private TranslatorInterface $translator;
44
45
    private Environment $twigEnvironment;
46
47
    public function __construct(
48
        WishlistRepositoryInterface $wishlistRepository,
49
        CartContextInterface $cartContext,
50
        FormFactoryInterface $formFactory,
51
        OrderModifierInterface $orderModifier,
52
        EntityManagerInterface $cartManager,
53
        FlashBagInterface $flashBag,
54
        TranslatorInterface $translator,
55
        Environment $twigEnvironment
56
    ) {
57
        $this->wishlistRepository = $wishlistRepository;
58
        $this->cartContext = $cartContext;
59
        $this->formFactory = $formFactory;
60
        $this->orderModifier = $orderModifier;
61
        $this->flashBag = $flashBag;
62
        $this->twigEnvironment = $twigEnvironment;
63
        $this->cartManager = $cartManager;
64
        $this->translator = $translator;
65
    }
66
67
    public function __invoke(int $wishlistId, Request $request): Response
68
    {
69
        /** @var WishlistInterface $wishlist */
70
        $wishlist = $this->wishlistRepository->find($wishlistId);
71
72
        $cart = $this->cartContext->getCart();
73
74
        $form = $this->formFactory->create(AddProductsToCartType::class, null, [
75
            'cart' => $cart,
76
            'wishlist_products' => $wishlist->getWishlistProducts(),
77
        ]);
78
79
        $form->handleRequest($request);
80
81
        if ($form->isSubmitted() && $form->isValid()) {
82
83
            if ($this->handleCartItems($form)) {
84
                $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_to_cart'));
85
            } else {
86
                $this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.increase_quantity'));
87
            }
88
89
            return new Response(
90
                $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
91
                    'wishlist' => $wishlist,
92
                    'form' => $form->createView(),
93
                ])
94
            );
95
        }
96
97
        foreach ($form->getErrors() as $error) {
98
            $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

98
            $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...
99
        }
100
101
        return new Response(
102
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
103
                'wishlist' => $wishlist,
104
                'form' => $form->createView(),
105
            ])
106
        );
107
    }
108
109
    private function handleCartItems(FormInterface $form): bool
110
    {
111
        $result = false;
112
113
        /** @var AddToCartCommandInterface $command */
114
        foreach ($form->getData() as $command) {
115
            if (0 < $command->getCartItem()->getQuantity()) {
116
                $result = true;
117
                $this->orderModifier->addToOrder($command->getCart(), $command->getCartItem());
118
                $this->cartManager->persist($command->getCart());
119
            }
120
        }
121
122
        $this->cartManager->flush();
123
124
        return $result;
125
    }
126
127
}