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

WishlistsResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 1
b 1
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\Resolver;
12
13
use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
14
use Sylius\Component\Core\Model\ShopUserInterface;
15
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
16
17
final class WishlistsResolver implements WishlistsResolverInterface
18
{
19
    private WishlistRepositoryInterface $wishlistRepository;
20
21
    private TokenStorageInterface $tokenStorage;
22
23
    private WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver;
24
25
    public function __construct(
26
        WishlistRepositoryInterface $wishlistRepository,
27
        TokenStorageInterface $tokenStorage,
28
        WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver
29
    ) {
30
        $this->wishlistRepository = $wishlistRepository;
31
        $this->tokenStorage = $tokenStorage;
32
        $this->wishlistCookieTokenResolver = $wishlistCookieTokenResolver;
33
    }
34
35
    public function resolve(): array
36
    {
37
        $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
38
39
        $wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve();
40
41
        if ($user instanceof ShopUserInterface) {
42
            return $this->wishlistRepository->findAllByShopUserAndToken($user->getId(), $wishlistCookieToken);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->wishlistRe..., $wishlistCookieToken) could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
43
        }
44
45
        return $this->wishlistRepository->findAllByAnonymous($wishlistCookieToken);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->wishlistRe...s($wishlistCookieToken) could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
46
    }
47
}
48