Completed
Push — master ( 0233f4...0c7686 )
by Tarmo
33s queued 23s
created

ApiKeyUser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getPassword() 0 3 1
A getSalt() 0 3 1
A eraseCredentials() 0 2 1
A getRoles() 0 3 1
A getApiKeyIdentifier() 0 3 1
A getUserIdentifier() 0 3 1
A getUsername() 0 3 1
A __construct() 0 5 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 App\Security\Interfaces\RolesServiceInterface;
14
use Symfony\Component\Security\Core\User\UserInterface;
15
use function array_merge;
16
use function array_unique;
17
18
/**
19
 * Class ApiKeyUser
20
 *
21
 * @package App\Security
22
 * @author TLe, Tarmo Leppänen <[email protected]>
23
 */
24
class ApiKeyUser implements ApiKeyUserInterface, UserInterface
25
{
26
    private string $identifier;
27
    private string $apiKeyIdentifier;
28
29
    /**
30
     * @var array<int, string>
31
     */
32
    private array $roles;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 20
    public function __construct(ApiKey $apiKey, array $roles)
38
    {
39 20
        $this->identifier = $apiKey->getToken();
40 20
        $this->apiKeyIdentifier = $apiKey->getId();
41 20
        $this->roles = array_unique(array_merge($roles, [RolesServiceInterface::ROLE_API]));
42 20
    }
43
44 2
    public function getUserIdentifier(): string
45
    {
46 2
        return $this->identifier;
47
    }
48
49 7
    public function getApiKeyIdentifier(): string
50
    {
51 7
        return $this->apiKeyIdentifier;
52
    }
53
54 15
    public function getRoles(): array
55
    {
56 15
        return $this->roles;
57
    }
58
59
    /**
60
     * @codeCoverageIgnore
61
     */
62
    public function getPassword(): ?string
63
    {
64
        return null;
65
    }
66
67
    /**
68
     * @codeCoverageIgnore
69
     */
70
    public function getSalt(): ?string
71
    {
72
        return null;
73
    }
74
75
    /**
76
     * @codeCoverageIgnore
77
     */
78
    public function eraseCredentials(): void
79
    {
80
    }
81
82
    /**
83
     * @remimder Remove this method when Symfony 6.0.0 is released
84
     *
85
     * @codeCoverageIgnore
86
     */
87
    public function getUsername(): string
88
    {
89
        return $this->getUserIdentifier();
90
    }
91
}
92