Passed
Pull Request — master (#79)
by
unknown
05:53 queued 01:39
created

CreateNewWishlistAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 56
rs 10
c 2
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A __invoke() 0 25 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\Entity\Wishlist;
14
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
15
use BitBag\SyliusWishlistPlugin\Form\Type\CreateNewWishlistType;
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\Routing\Generator\UrlGeneratorInterface;
23
use Symfony\Contracts\Translation\TranslatorInterface;
24
use Twig\Environment;
25
26
final class CreateNewWishlistAction
27
{
28
    private ObjectManager $wishlistManager;
29
30
    private FlashBagInterface $flashBag;
31
32
    private TranslatorInterface $translator;
33
34
    private UrlGeneratorInterface $urlGenerator;
35
36
    private FormFactoryInterface $formFactory;
37
38
    private Environment $twigEnvironment;
39
40
    public function __construct(
41
        ObjectManager $wishlistManager,
42
        FlashBagInterface $flashBag,
43
        TranslatorInterface $translator,
44
        UrlGeneratorInterface $urlGenerator,
45
        FormFactoryInterface $formFactory,
46
        Environment $twigEnvironment
47
48
    ) {
49
        $this->wishlistManager = $wishlistManager;
50
        $this->flashBag = $flashBag;
51
        $this->translator = $translator;
52
        $this->urlGenerator = $urlGenerator;
53
        $this->formFactory = $formFactory;
54
        $this->twigEnvironment = $twigEnvironment;
55
    }
56
57
    public function __invoke(Request $request): Response
58
    {
59
        /** @var WishlistInterface $wishlist */
60
        $wishlist = new Wishlist();
61
62
        $form = $this->formFactory->create(CreateNewWishlistType::class, $wishlist);
63
        $form->handleRequest($request);
64
65
        if ($form->isSubmitted() && $form->isValid()) {
66
67
            $wishlist = $form->getData();
68
69
            $this->wishlistManager->persist($wishlist);
70
            $this->wishlistManager->flush();
71
72
            $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.create_new_wishlist'));
73
74
            return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_wishlists'));
75
        }
76
77
78
        return new Response(
79
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/Wishlist/createWishlist.html.twig', [
80
                'wishlist' => $wishlist,
81
                'form' => $form->createView(),
82
83
            ])
84
        );
85
86
87
    }
88
89
}
90