Passed
Pull Request — master (#81)
by
unknown
04:01
created

CreateNewWishlistHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 12
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\Entity\WishlistInterface;
15
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface;
16
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
17
use Sylius\Component\Core\Model\ShopUserInterface;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
20
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
21
22
final class CreateNewWishlistHandler implements MessageHandlerInterface
23
{
24
    private WishlistRepositoryInterface $wishlistRepository;
25
26
    private TokenStorageInterface $tokenStorage;
27
28
    private WishlistFactoryInterface $wishlistFactory;
29
30
    private RequestStack $requestStack;
31
32
    private string $wishlistCookieToken;
33
34
    public function __construct(
35
        WishlistRepositoryInterface $wishlistRepository,
36
        TokenStorageInterface $tokenStorage,
37
        WishlistFactoryInterface $wishlistFactory,
38
        RequestStack $requestStack,
39
        string $wishlistCookieToken
40
    ) {
41
        $this->wishlistRepository = $wishlistRepository;
42
        $this->tokenStorage = $tokenStorage;
43
        $this->wishlistFactory = $wishlistFactory;
44
        $this->requestStack = $requestStack;
45
        $this->wishlistCookieToken = $wishlistCookieToken;
46
    }
47
48
    public function __invoke(CreateNewWishlist $createNewWishlist): WishlistInterface
49
    {
50
        $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
51
52
        if ($user instanceof ShopUserInterface) {
53
            $wishlist = $this->wishlistFactory->createForUser($user);
54
        } else {
55
            $wishlist = $this->wishlistFactory->createNew();
56
        }
57
58
        $mainRequest = $this->requestStack->getMasterRequest();
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\HttpFo...ack::getMasterRequest() has been deprecated: since symfony/http-foundation 5.3, use getMainRequest() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

58
        $mainRequest = /** @scrutinizer ignore-deprecated */ $this->requestStack->getMasterRequest();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
59
60
        if ($mainRequest->cookies->get($this->wishlistCookieToken)) {
61
            $wishlist->setToken($mainRequest->cookies->get($this->wishlistCookieToken));
62
        }
63
64
        $wishlist->setName($createNewWishlist->getName());
65
        $this->wishlistRepository->add($wishlist);
66
67
        return $wishlist;
68
    }
69
}