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

PlayerFromTokenExtractor::player()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 0
dl 0
loc 18
ccs 12
cts 12
cp 1
crap 3
rs 9.8666
c 0
b 0
f 0
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