Completed
Push — master ( ad6910...d77fda )
by Tarmo
18s queued 12s
created

UserTypeIdentification::getUserToken()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 2
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 3
rs 10
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 Doctrine\ORM\NonUniqueResultException;
15
use Stringable;
16
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18
use Symfony\Component\Security\Core\User\UserInterface;
19
20
/**
21
 * Class UserTypeIdentification
22
 *
23
 * @package App\Security
24
 * @author TLe, Tarmo Leppänen <[email protected]>
25
 */
26
class UserTypeIdentification
27
{
28 534
    public function __construct(
29
        private TokenStorageInterface $tokenStorage,
30
        private UserRepository $userRepository,
31
    ) {
32 534
    }
33
34
    /**
35
     * Helper method to get current logged in ApiKey entity via token storage.
36
     */
37 9
    public function getApiKey(): ?ApiKey
38
    {
39 9
        $apiKeyUser = $this->getApiKeyUser();
40
41 9
        return $apiKeyUser === null ? null : $apiKeyUser->getApiKey();
42
    }
43
44
    /**
45
     * Helper method to get current logged in User entity via token storage.
46
     *
47
     * @throws NonUniqueResultException
48
     */
49 332
    public function getUser(): ?User
50
    {
51 332
        $user = $this->getSecurityUser();
52
53 332
        return $user === null ? null : $this->userRepository->loadUserByUsername($user->getUsername(), true);
54
    }
55
56
    /**
57
     * Helper method to get user identity object via token storage.
58
     */
59 452
    public function getIdentity(): ?UserInterface
60
    {
61 452
        return $this->getSecurityUser() ?? $this->getApiKeyUser();
62
    }
63
64
    /**
65
     * Helper method to get current logged in ApiKeyUser via token storage.
66
     */
67 222
    public function getApiKeyUser(): ?ApiKeyUser
68
    {
69 222
        $apiKeyUser = $this->getUserToken();
70
71 222
        return $apiKeyUser instanceof ApiKeyUser ? $apiKeyUser : null;
72
    }
73
74
    /**
75
     * Helper method to get current logged in SecurityUser via token storage.
76
     */
77 516
    public function getSecurityUser(): ?SecurityUser
78
    {
79 516
        $securityUser = $this->getUserToken();
80
81 516
        return $securityUser instanceof SecurityUser ? $securityUser : null;
82
    }
83
84
    /**
85
     * Returns a user representation. Can be a UserInterface instance, an
86
     * object implementing a __toString method, or the username as a regular
87
     * string.
88
     */
89 534
    private function getUserToken(): UserInterface | Stringable | string | null
0 ignored issues
show
Bug introduced by
The type App\Security\null 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...
90
    {
91 534
        $token = $this->tokenStorage->getToken();
92
93 534
        return !($token === null || $token instanceof AnonymousToken) ? $token->getUser() : null;
94
    }
95
}
96