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

CreateNewWishlistHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A __invoke() 0 14 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\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
58
        if ($user instanceof ShopUserInterface) {
59
            $wishlist = $wishlistFactory->createForUser($user);
60
        } else {
61
            $wishlist = $wishlistFactory->createNew();
62
        }
63
        $wishlist->setName('amen');
64
        $this->wishlistManager->persist($wishlist);
65
        $this->wishlistManager->flush();
66
    }
67
}