WishlistContext::getWishlist()   B
last analyzed

Complexity

Conditions 9
Paths 12

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 15
nc 12
nop 1
dl 0
loc 32
rs 8.0555
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\Context;
12
13
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
14
use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface;
15
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
16
use Sylius\Component\Core\Model\ShopUserInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
20
21
final class WishlistContext implements WishlistContextInterface
22
{
23
    private TokenStorageInterface $tokenStorage;
24
25
    private WishlistRepositoryInterface $wishlistRepository;
26
27
    private WishlistFactoryInterface $wishlistFactory;
28
29
    private string $wishlistCookieToken;
30
31
    public function __construct(
32
        TokenStorageInterface $tokenStorage,
33
        WishlistRepositoryInterface $wishlistRepository,
34
        WishlistFactoryInterface $wishlistFactory,
35
        string $wishlistCookieToken
36
    ) {
37
        $this->tokenStorage = $tokenStorage;
38
        $this->wishlistRepository = $wishlistRepository;
39
        $this->wishlistFactory = $wishlistFactory;
40
        $this->wishlistCookieToken = $wishlistCookieToken;
41
    }
42
43
    public function getWishlist(Request $request): WishlistInterface
44
    {
45
        /** @var ?string $cookieWishlistToken */
46
        $cookieWishlistToken = $request->cookies->get($this->wishlistCookieToken);
47
48
        /** @var ?TokenInterface $token */
49
        $token = $this->tokenStorage->getToken();
50
51
        /** @var WishlistInterface $wishlist */
52
        $wishlist = $this->wishlistFactory->createNew();
53
54
        $user = null !== $token ? $token->getUser() : null;
55
56
        if (null === $cookieWishlistToken && null === $user) {
57
            return $wishlist;
58
        }
59
60
        if (null !== $cookieWishlistToken && !$user instanceof ShopUserInterface) {
61
            return null !== $this->wishlistRepository->findByToken($cookieWishlistToken) ?
0 ignored issues
show
Bug Best Practice introduced by
The expression return null !== $this->w...hlistToken) : $wishlist could return the type null which is incompatible with the type-hinted return BitBag\SyliusWishlistPlu...ntity\WishlistInterface. Consider adding an additional type-check to rule them out.
Loading history...
62
                $this->wishlistRepository->findByToken($cookieWishlistToken) :
63
                $wishlist
64
            ;
65
        }
66
67
        if ($user instanceof ShopUserInterface) {
68
            return null !== $this->wishlistRepository->findOneByShopUser($user) ?
0 ignored issues
show
Bug Best Practice introduced by
The expression return null !== $this->w...y->createForUser($user) could return the type null which is incompatible with the type-hinted return BitBag\SyliusWishlistPlu...ntity\WishlistInterface. Consider adding an additional type-check to rule them out.
Loading history...
69
                $this->wishlistRepository->findOneByShopUser($user) :
70
                $this->wishlistFactory->createForUser($user)
71
            ;
72
        }
73
74
        return $wishlist;
75
    }
76
}
77