Passed
Pull Request — master (#81)
by
unknown
05:33
created

CreateNewWishlistAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 46
rs 10
c 3
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A __invoke() 0 16 3
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\CreateNewWishlist;
14
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
15
use BitBag\SyliusWishlistPlugin\Form\Type\CreateNewWishlistType;
16
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface;
17
use Doctrine\Persistence\ObjectManager;
18
use Symfony\Component\Form\FormFactoryInterface;
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
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
26
use Symfony\Contracts\Translation\TranslatorInterface;
27
use Twig\Environment;
28
29
final class CreateNewWishlistAction
30
{
31
    private FormFactoryInterface $formFactory;
32
33
    private MessageBusInterface $commandBus;
34
35
    private Environment $twigEnvironment;
36
37
    private FlashBagInterface $flashBag;
38
39
    private TranslatorInterface $translator;
40
41
    private UrlGeneratorInterface $urlGenerator;
42
43
    public function __construct(
44
        FormFactoryInterface $formFactory,
45
        MessageBusInterface $commandBus,
46
        Environment $twigEnvironment,
47
        FlashBagInterface $flashBag,
48
        TranslatorInterface $translator,
49
        UrlGeneratorInterface $urlGenerator
50
    ) {
51
        $this->formFactory = $formFactory;
52
        $this->commandBus = $commandBus;
53
        $this->twigEnvironment = $twigEnvironment;
54
        $this->flashBag = $flashBag;
55
        $this->translator = $translator;
56
        $this->urlGenerator = $urlGenerator;
57
    }
58
59
    public function __invoke(Request $request): Response
60
    {
61
        $form = $this->formFactory->create(CreateNewWishlistType::class);
62
        $form->handleRequest($request);
63
64
        if ($form->isSubmitted() && $form->isValid()) {
65
            $createNewWishlist = $form->getData();
66
            $this->commandBus->dispatch($createNewWishlist);
67
            $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.create_new_wishlist'));
68
69
            return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_wishlists'));
70
        }
71
72
        return new Response(
73
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/CreateWishlist/index.html.twig', [
74
                'form' => $form->createView(),
75
            ])
76
        );
77
    }
78
}
79