Passed
Pull Request — master (#81)
by
unknown
05:33
created

CreateNewWishlistHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 9
rs 10
c 1
b 0
f 0
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
    private TokenStorageInterface $tokenStorage;
24
    private WishlistFactoryInterface $wishlistFactory;
25
26
    public function __construct(
27
        WishlistRepositoryInterface $wishlistRepository,
28
        TokenStorageInterface $tokenStorage,
29
        WishlistFactoryInterface $wishlistFactory
30
    ) {
31
32
        $this->wishlistRepository = $wishlistRepository;
33
        $this->tokenStorage = $tokenStorage;
34
        $this->wishlistFactory = $wishlistFactory;
35
    }
36
37
    public function __invoke(CreateNewWishlist $createNewWishlist)
38
    {
39
        $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
40
41
        if ($user instanceof ShopUserInterface) {
42
            $wishlist = $this->wishlistFactory->createForUser($user);
43
        } else {
44
            $wishlist = $this->wishlistFactory->createNew();
45
        }
46
        $wishlist->setName($createNewWishlist->getName());
47
        $this->wishlistRepository->add($wishlist);
48
49
        return $wishlist;
50
    }
51
}