UserResolver   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 22
ccs 8
cts 8
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCurrentUser() 0 10 3
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Security;
6
7
use App\Entity\User;
8
use App\Exception\NoCurrentUserException;
9
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
10
11
final class UserResolver
12
{
13
    private TokenStorageInterface $tokenStorage;
14
15 49
    public function __construct(TokenStorageInterface $tokenStorage)
16
    {
17 49
        $this->tokenStorage = $tokenStorage;
18
    }
19
20
    /**
21
     * @throws NoCurrentUserException
22
     */
23 28
    public function getCurrentUser(): User
24
    {
25 28
        $token = $this->tokenStorage->getToken();
26 28
        $user = $token !== null ? $token->getUser() : null;
27
28 28
        if (!($user instanceof User)) {
29 9
            throw new NoCurrentUserException();
30
        }
31
32 19
        return $user;
33
    }
34
}
35