LoggedInShopUserProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 35
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A provide() 0 17 3
A isUserLoggedIn() 0 6 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.io and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusVueStorefrontPlugin\Sylius\Provider;
14
15
use Sylius\Component\Core\Model\ShopUserInterface;
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
18
19
final class LoggedInShopUserProvider implements LoggedInShopUserProviderInterface
20
{
21
    /** @var TokenStorageInterface */
22
    private $tokenStorage;
23
24
    public function __construct(TokenStorageInterface $tokenStorage)
25
    {
26
        $this->tokenStorage = $tokenStorage;
27
    }
28
29
    public function provide(): ShopUserInterface
30
    {
31
        $token = $this->tokenStorage->getToken();
32
33
        if (null === $token) {
34
            throw new TokenNotFoundException('No token found.');
35
        }
36
37
        /** @var ShopUserInterface|null $user */
38
        $user = $token->getUser();
39
40
        if (!$user instanceof ShopUserInterface) {
41
            throw new TokenNotFoundException('No logged in user.');
42
        }
43
44
        return $user;
45
    }
46
47
    public function isUserLoggedIn(): bool
48
    {
49
        $token = $this->tokenStorage->getToken();
50
51
        return null !== $token && $token->getUser() instanceof ShopUserInterface;
52
    }
53
}
54