UserTypeIdentification::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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