Issues (11)

src/Context/ComparerContext.php (2 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Locastic\SyliusComparerPlugin\Context;
6
7
use Locastic\SyliusComparerPlugin\Entity\ComparerInterface;
8
use Locastic\SyliusComparerPlugin\Factory\ComparerFactoryInterface;
9
use Locastic\SyliusComparerPlugin\Repository\ComparerRepositoryInterface;
10
use Sylius\Component\Core\Model\ShopUserInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
13
14
final class ComparerContext implements ComparerContextInterface
15
{
16
    /** @var TokenStorageInterface */
17
    private $tokenStorage;
18
19
    /** @var ComparerRepositoryInterface */
20
    private $comparerRepository;
21
22
    /** @var ComparerFactoryInterface */
23
    private $comparerFactory;
24
25
    /** @var string */
26
    private $comparerCookieToken;
27
28
    public function __construct(
29
        TokenStorageInterface $tokenStorage,
30
        ComparerFactoryInterface $comparerFactroy,
31
        ComparerRepositoryInterface $comparerRepository,
32
        string $comparerCookieToken
33
    ) {
34
        $this->tokenStorage = $tokenStorage;
35
        $this->comparerFactory = $comparerFactroy;
36
        $this->comparerRepository = $comparerRepository;
37
        $this->comparerCookieToken = $comparerCookieToken;
38
    }
39
40
    public function getComparer(Request $request): ComparerInterface
41
    {
42
        $cookieComparerToken = $request->cookies->get($this->comparerCookieToken);
43
        $token = $this->tokenStorage->getToken();
44
        $user = $token ? $token->getUser() : null;
45
46
        if ($this->userIsLoggedIn($user)) {
47
            return $this->comparerRepository->findByShopUser($user) ??
0 ignored issues
show
It seems like $user can also be of type null and string; however, parameter $shopUser of Locastic\SyliusComparerP...rface::findByShopUser() does only seem to accept Sylius\Component\Core\Model\ShopUserInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

47
            return $this->comparerRepository->findByShopUser(/** @scrutinizer ignore-type */ $user) ??
Loading history...
48
                $this->comparerFactory->createForShopUser($user);
0 ignored issues
show
It seems like $user can also be of type null and string; however, parameter $shopUser of Locastic\SyliusComparerP...ce::createForShopUser() does only seem to accept Sylius\Component\Core\Model\ShopUserInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

48
                $this->comparerFactory->createForShopUser(/** @scrutinizer ignore-type */ $user);
Loading history...
49
        }
50
51
        if ($this->guestUserHasComparer($cookieComparerToken, $user)) {
52
            return $this->comparerRepository->findByToken($cookieComparerToken) ??
53
                $this->comparerFactory->createNew();
54
        }
55
56
        return $this->comparerFactory->createNew();
57
    }
58
59
    private function userIsLoggedIn($user)
60
    {
61
        return $user instanceof ShopUserInterface;
62
    }
63
64
    private function guestUserHasComparer(?string $cookieComparerToken, $user): bool
65
    {
66
        if (null !== $cookieComparerToken && !$user instanceof ShopUserInterface) {
67
            return true;
68
        }
69
70
        return false;
71
    }
72
}
73