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

CreateNewWishlistAction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 35
dl 0
loc 68
rs 10
c 3
b 0
f 0
wmc 6

2 Methods

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