Passed
Pull Request — master (#81)
by
unknown
04:24
created

CreateNewWishlistAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 50
rs 10
c 4
b 0
f 0
wmc 4

2 Methods

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