UserTypeIdentification   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 69
ccs 20
cts 20
cp 1
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 5 2
A getApiKey() 0 7 2
A __construct() 0 5 1
A getApiKeyUser() 0 5 2
A getSecurityUser() 0 5 2
A getIdentity() 0 3 1
A getUserToken() 0 3 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Security/UserTypeIdentification.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\Entity\User;
13
use App\Repository\UserRepository;
14
use App\Security\Provider\ApiKeyUserProvider;
15
use Doctrine\ORM\NonUniqueResultException;
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17
use Symfony\Component\Security\Core\User\UserInterface;
18
19
/**
20
 * Class UserTypeIdentification
21
 *
22
 * @package App\Security
23
 * @author TLe, Tarmo Leppänen <[email protected]>
24
 */
25
class UserTypeIdentification
26
{
27 626
    public function __construct(
28
        private readonly TokenStorageInterface $tokenStorage,
29
        private readonly UserRepository $userRepository,
30
        private readonly ApiKeyUserProvider $apiKeyUserProvider,
31
    ) {
32 626
    }
33
34
    /**
35
     * Helper method to get current logged in ApiKey entity via token storage.
36
     */
37 5
    public function getApiKey(): ?ApiKey
38
    {
39 5
        $apiKeyUser = $this->getApiKeyUser();
40
41 5
        return $apiKeyUser === null
42 4
            ? null
43 5
            : $this->apiKeyUserProvider->getApiKeyForToken($apiKeyUser->getUserIdentifier());
44
    }
45
46
    /**
47
     * Helper method to get current logged in User entity via token storage.
48
     *
49
     * @throws NonUniqueResultException
50
     */
51 338
    public function getUser(): ?User
52
    {
53 338
        $user = $this->getSecurityUser();
54
55 338
        return $user === null ? null : $this->userRepository->loadUserByIdentifier($user->getUserIdentifier(), true);
56
    }
57
58
    /**
59
     * Helper method to get user identity object via token storage.
60
     */
61 548
    public function getIdentity(): SecurityUser|ApiKeyUser|null
62
    {
63 548
        return $this->getSecurityUser() ?? $this->getApiKeyUser();
64
    }
65
66
    /**
67
     * Helper method to get current logged in ApiKeyUser via token storage.
68
     */
69 303
    public function getApiKeyUser(): ?ApiKeyUser
70
    {
71 303
        $apiKeyUser = $this->getUserToken();
72
73 303
        return $apiKeyUser instanceof ApiKeyUser ? $apiKeyUser : null;
74
    }
75
76
    /**
77
     * Helper method to get current logged in SecurityUser via token storage.
78
     */
79 616
    public function getSecurityUser(): ?SecurityUser
80
    {
81 616
        $securityUser = $this->getUserToken();
82
83 616
        return $securityUser instanceof SecurityUser ? $securityUser : null;
84
    }
85
86
    /**
87
     * Returns a user representation. Can be a UserInterface instance, an
88
     * object implementing a __toString method, or the username as a regular
89
     * string.
90
     */
91 626
    private function getUserToken(): UserInterface|null
92
    {
93 626
        return $this->tokenStorage->getToken()?->getUser();
94
    }
95
}
96