Completed
Push — master ( 126c81...e32e37 )
by Tarmo
19s queued 13s
created

ApiKeyUser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A eraseCredentials() 0 2 1
A getSalt() 0 3 1
A getApiKey() 0 3 1
A getPassword() 0 3 1
A getRoles() 0 3 1
A __construct() 0 5 1
A getUserIdentifier() 0 3 1
A getUsername() 0 3 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Security/ApiKeyUser.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Security;
10
11
use App\Entity\ApiKey;
12
use App\Security\Interfaces\ApiKeyUserInterface;
13
use Symfony\Component\Serializer\Annotation\Groups;
14
use function array_merge;
15
use function array_unique;
16
17
/**
18
 * Class ApiKeyUser
19
 *
20
 * @package App\Security
21
 * @author TLe, Tarmo Leppänen <[email protected]>
22
 */
23
class ApiKeyUser implements ApiKeyUserInterface
24
{
25
    /**
26
     * @Groups({
27
     *      "ApiKeyUser",
28
     *      "ApiKeyUser.apiKey",
29
     *  })
30
     */
31
    private string $username;
32
33
    /**
34
     * @Groups({
35
     *      "ApiKeyUser.apiKey",
36
     *  })
37
     */
38
    private ApiKey $apiKey;
39
40
    /**
41
     * @var array<int, string>
42
     *
43
     * @Groups({
44
     *      "ApiKeyUser",
45
     *      "ApiKeyUser.roles",
46
     *  })
47
     */
48
    private array $roles;
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 34
    public function __construct(ApiKey $apiKey, array $roles)
54
    {
55 34
        $this->apiKey = $apiKey;
56 34
        $this->username = $this->apiKey->getToken();
57 34
        $this->roles = array_unique(array_merge($roles, [RolesService::ROLE_API]));
58 34
    }
59
60 28
    public function getApiKey(): ApiKey
61
    {
62 28
        return $this->apiKey;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     *
68
     * @return array<int, string> The user roles
69
     */
70 24
    public function getRoles(): array
71
    {
72 24
        return $this->roles;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     *
78
     * @codeCoverageIgnore
79
     */
80
    public function getPassword(): string
81
    {
82
        return '';
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     *
88
     * @codeCoverageIgnore
89
     */
90
    public function getSalt(): ?string
91
    {
92
        return null;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     *
98
     * @codeCoverageIgnore
99
     */
100
    public function getUserIdentifier(): string
101
    {
102
        return $this->username;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     *
108
     * @codeCoverageIgnore
109
     */
110
    public function eraseCredentials(): void
111
    {
112
    }
113
114
    /**
115
     * @todo Remove this when this `getUsername` is removed from main interface (Symfony 6.0)
116
     *
117
     * {@inheritdoc}
118
     *
119
     * @codeCoverageIgnore
120
     */
121
    public function getUsername(): string
122
    {
123
        return $this->getUserIdentifier();
124
    }
125
}
126