BaseWishlistProductsAction::createForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
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\Context\WishlistContextInterface;
14
use BitBag\SyliusWishlistPlugin\Form\Type\WishlistCollectionType;
15
use BitBag\SyliusWishlistPlugin\Processor\WishlistCommandProcessorInterface;
16
use Sylius\Component\Order\Context\CartContextInterface;
17
use Symfony\Component\Form\FormFactoryInterface;
18
use Symfony\Component\Form\FormInterface;
19
use Symfony\Component\HttpFoundation\RedirectResponse;
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 Symfony\Component\Routing\Generator\UrlGeneratorInterface;
25
26
abstract class BaseWishlistProductsAction
27
{
28
    public WishlistContextInterface $wishlistContext;
29
30
    public CartContextInterface $cartContext;
31
32
    public FormFactoryInterface $formFactory;
33
34
    public FlashBagInterface $flashBag;
35
36
    public WishlistCommandProcessorInterface $wishlistCommandProcessor;
37
38
    public MessageBusInterface $messageBus;
39
40
    public UrlGeneratorInterface $urlGenerator;
41
42
    public function __construct(
43
        WishlistContextInterface $wishlistContext,
44
        CartContextInterface $cartContext,
45
        FormFactoryInterface $formFactory,
46
        FlashBagInterface $flashBag,
47
        WishlistCommandProcessorInterface $wishlistCommandProcessor,
48
        MessageBusInterface $messageBus,
49
        UrlGeneratorInterface $urlGenerator
50
    ) {
51
        $this->wishlistContext = $wishlistContext;
52
        $this->cartContext = $cartContext;
53
        $this->formFactory = $formFactory;
54
        $this->flashBag = $flashBag;
55
        $this->wishlistCommandProcessor = $wishlistCommandProcessor;
56
        $this->messageBus = $messageBus;
57
        $this->urlGenerator = $urlGenerator;
58
    }
59
60
    public function __invoke(Request $request): Response
61
    {
62
        $form = $this->createForm($request);
63
64
        $form->handleRequest($request);
65
66
        if ($form->isSubmitted() && $form->isValid()) {
67
            $this->handleCommand($form);
68
69
            return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products'));
70
        }
71
72
        foreach ($form->getErrors() as $error) {
73
            $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

73
            $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...
74
        }
75
76
        return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products'));
77
    }
78
79
    abstract protected function handleCommand(FormInterface $form): void;
80
81
    private function createForm(Request $request): FormInterface
82
    {
83
        $wishlist = $this->wishlistContext->getWishlist($request);
84
        $cart = $this->cartContext->getCart();
85
86
        $commandsArray = $this->wishlistCommandProcessor->createAddCommandCollectionFromWishlistProducts($wishlist->getWishlistProducts());
87
88
        return $this->formFactory->create(WishlistCollectionType::class, ['items' => $commandsArray], [
89
            'cart' => $cart,
90
        ]);
91
    }
92
}
93