UserResolver::getCurrentUser()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 10
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