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

CreateNewWishlistHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
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\Factory\WishlistFactoryInterface;
15
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
16
use Sylius\Component\Core\Model\ShopUserInterface;
17
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
18
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19
20
final class CreateNewWishlistHandler implements MessageHandlerInterface
21
{
22
    private WishlistRepositoryInterface $wishlistRepository;
23
24
    private TokenStorageInterface $tokenStorage;
25
26
    private WishlistFactoryInterface $wishlistFactory;
27
28
    public function __construct(
29
        WishlistRepositoryInterface $wishlistRepository,
30
        TokenStorageInterface $tokenStorage,
31
        WishlistFactoryInterface $wishlistFactory
32
    ) {
33
        $this->wishlistRepository = $wishlistRepository;
34
        $this->tokenStorage = $tokenStorage;
35
        $this->wishlistFactory = $wishlistFactory;
36
    }
37
38
    public function __invoke(CreateNewWishlist $createNewWishlist)
39
    {
40
        $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
41
42
        if ($user instanceof ShopUserInterface) {
43
            $wishlist = $this->wishlistFactory->createForUser($user);
44
        } else {
45
            $wishlist = $this->wishlistFactory->createNew();
46
        }
47
        $wishlist->setName($createNewWishlist->getName());
48
        $this->wishlistRepository->add($wishlist);
49
50
        return $wishlist;
51
    }
52
}