Passed
Pull Request — master (#81)
by
unknown
03:25
created

CreateNewWishlistAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

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

2 Methods

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