Passed
Pull Request — master (#84)
by
unknown
04:09
created

AddSelectedProductsToCart   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 69
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 39 5
A __construct() 0 14 1
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\AddSelectedProductsToCart as AddSelectedProductsToCartCommand;
14
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistProduct;
15
use BitBag\SyliusWishlistPlugin\Context\WishlistContextInterface;
16
use BitBag\SyliusWishlistPlugin\Form\Type\WishlistCollectionType;
17
use Doctrine\Common\Collections\ArrayCollection;
18
use Sylius\Component\Order\Context\CartContextInterface;
19
use Symfony\Component\Form\FormFactoryInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
23
use Symfony\Component\Messenger\MessageBusInterface;
24
use Twig\Environment;
25
26
final class AddSelectedProductsToCart
27
{
28
    private WishlistContextInterface $wishlistContext;
29
30
    private CartContextInterface $cartContext;
31
32
    private FormFactoryInterface $formFactory;
33
34
    private FlashBagInterface $flashBag;
35
36
    private Environment $twigEnvironment;
37
38
    private MessageBusInterface $commandBus;
39
40
    public function __construct(
41
        WishlistContextInterface $wishlistContext,
42
        CartContextInterface $cartContext,
43
        FormFactoryInterface $formFactory,
44
        FlashBagInterface $flashBag,
45
        Environment $twigEnvironment,
46
        MessageBusInterface $commandBus
47
    ) {
48
        $this->wishlistContext = $wishlistContext;
49
        $this->cartContext = $cartContext;
50
        $this->formFactory = $formFactory;
51
        $this->flashBag = $flashBag;
52
        $this->twigEnvironment = $twigEnvironment;
53
        $this->commandBus = $commandBus;
54
    }
55
56
    public function __invoke(Request $request): Response
57
    {
58
        $wishlist = $this->wishlistContext->getWishlist($request);
59
        $cart = $this->cartContext->getCart();
60
61
        $commandsArray = new ArrayCollection();
62
63
        foreach ($wishlist->getWishlistProducts() as $wishlistProductItem) {
64
            $wishlistProductCommand = new AddWishlistProduct();
65
            $wishlistProductCommand->setWishlistProduct($wishlistProductItem);
66
            $commandsArray->add($wishlistProductCommand);
67
        }
68
69
        $form = $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
70
            'cart' => $cart,
71
        ]);
72
73
        $form->handleRequest($request);
74
75
        if ($form->isSubmitted() && $form->isValid()) {
76
            $command = new AddSelectedProductsToCartCommand($form->get('items')->getData());
77
            $this->commandBus->dispatch($command);
78
79
            return new Response(
80
                $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
81
                    'wishlist' => $wishlist,
82
                    'form' => $form->createView(),
83
                ])
84
            );
85
        }
86
87
        foreach ($form->getErrors() as $error) {
88
            $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

88
            $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...
89
        }
90
91
        return new Response(
92
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/WishlistDetails/index.html.twig', [
93
                'wishlist' => $wishlist,
94
                'form' => $form->createView(),
95
            ])
96
        );
97
    }
98
}
99