Passed
Push — master ( 35eebd...7b8a1a )
by Roman
16:45
created

PlayerFromTokenExtractor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 30
ccs 12
cts 15
cp 0.8
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A player() 0 18 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Game\Features\GamePlay\Authentication;
6
7
use App\Game\Features\GamePlay\Player\PlayerDto;
8
use App\Game\Features\GamePlay\Player\PlayerId;
9
use App\Game\Features\GamePlay\Player\Role;
10
use JsonException;
11
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...e\TokenStorageInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Uid\UuidV4;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Uid\UuidV4 was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
final class PlayerFromTokenExtractor
15
{
16
    private TokenStorageInterface $tokenStorage;
17
18
    public function __construct(TokenStorageInterface $tokenStorage)
19
    {
20
        $this->tokenStorage = $tokenStorage;
21
    }
22
23
    /**
24
     * @throws PlayerNotFoundInTokenStorageException
25
     */
26 3
    public function player(): PlayerDto
27
    {
28 3
        $token = $this->tokenStorage->getToken();
29 3
        if (null === $token) {
30 1
            throw new PlayerNotFoundInTokenStorageException();
31
        }
32
33
        try {
34 2
            $decoded = json_decode($token->getUser(), true, 512, JSON_THROW_ON_ERROR);
35
36 1
            return new PlayerDto(
37 1
                new PlayerId(UuidV4::fromString($decoded['id'])),
38 1
                $decoded['nickname'],
39 1
                (int) $decoded['level'],
40 1
                new Role($decoded['role'])
41
            );
42 1
        } catch (JsonException) {
43 1
            throw new PlayerNotFoundInTokenStorageException();
44
        }
45
    }
46
}
47