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

CreateNewWishlistHandler::__invoke()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 32
rs 9.3222
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\CommandHandler\Wishlist;
12
13
use BitBag\SyliusWishlistPlugin\Command\Wishlist\CreateNewWishlist;
14
use BitBag\SyliusWishlistPlugin\Form\Type\CreateNewWishlistType;
15
use Doctrine\Persistence\ObjectManager;
16
use Sylius\Component\Core\Model\ShopUserInterface;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
21
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
22
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
23
use Symfony\Contracts\Translation\TranslatorInterface;
24
use Twig\Environment;
25
26
final class CreateNewWishlistHandler implements MessageHandlerInterface
27
{
28
    private ObjectManager $wishlistManager;
29
30
    private FlashBagInterface $flashBag;
31
32
    private TranslatorInterface $translator;
33
34
    private UrlGeneratorInterface $urlGenerator;
35
36
    private Environment $twigEnvironment;
37
38
    public function __construct(
39
        ObjectManager $wishlistManager,
40
        FlashBagInterface $flashBag,
41
        TranslatorInterface $translator,
42
        UrlGeneratorInterface $urlGenerator,
43
        Environment $twigEnvironment
44
    ) {
45
        $this->wishlistManager = $wishlistManager;
46
        $this->flashBag = $flashBag;
47
        $this->translator = $translator;
48
        $this->urlGenerator = $urlGenerator;
49
        $this->twigEnvironment = $twigEnvironment;
50
    }
51
52
    public function __invoke(CreateNewWishlist $createNewWishlist)
53
    {
54
        $token = $createNewWishlist->getTokenStorage();
55
        $wishlistFactory = $createNewWishlist->getWishlistFactory();
56
        $user = $token->getToken() ? $token->getToken()->getUser() : null;
57
        $formFactory = $createNewWishlist->getFormFactory();
58
59
        if ($user instanceof ShopUserInterface) {
60
            $wishlist = $wishlistFactory->createForUser($user);
61
        } else {
62
            $wishlist = $wishlistFactory->createNew();
63
        }
64
65
        $form = $formFactory->create(CreateNewWishlistType::class, $wishlist);
66
        //$form->handleRequest($request);
67
68
        if ($form->isSubmitted() && $form->isValid()) {
69
70
            $wishlist = $form->getData();
71
72
            $this->wishlistManager->persist($wishlist);
73
            $this->wishlistManager->flush();
74
75
            $this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.create_new_wishlist'));
76
77
            return new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_wishlists'));
78
        }
79
80
        return new Response(
81
            $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/CreateWishlist/index.html.twig', [
82
                'wishlist' => $wishlist,
83
                'form' => $form->createView(),
84
            ])
85
        );
86
87
88
    }
89
90
}