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

AddSelectedProductsToCartAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
c 0
b 0
f 0
dl 0
loc 67
rs 10
wmc 5

2 Methods

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

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