Completed
Push — master ( b4619a...ab6ee6 )
by Kamil
12:06 queued 06:35
created

TokenBasedUserContext::getUser()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.7666
cc 3
nc 3
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\ApiBundle\Context;
15
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17
use Symfony\Component\Security\Core\User\UserInterface;
18
19
final class TokenBasedUserContext implements UserContextInterface
20
{
21
    /** @var TokenStorageInterface */
22
    private $tokenStorage;
23
24
    public function __construct(TokenStorageInterface $tokenStorage)
25
    {
26
        $this->tokenStorage = $tokenStorage;
27
    }
28
29
    public function getUser(): ?UserInterface
30
    {
31
        $token = $this->tokenStorage->getToken();
32
        if ($token === null) {
33
            return null;
34
        }
35
36
        /** @var UserInterface|string $user */
37
        $user = $token->getUser();
38
        if (is_string($user)) {
39
            return null;
40
        }
41
42
        return $user;
43
    }
44
}
45