Passed
Branch master (e07e14)
by Damien
02:22
created

UserProvider::getToken()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
nc 4
nop 0
dl 0
loc 13
c 0
b 0
f 0
cc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\AuditorBundle\User;
6
7
use DH\Auditor\Provider\Doctrine\Configuration;
8
use DH\Auditor\User\User;
9
use DH\Auditor\User\UserInterface as AuditorUserInterface;
10
use DH\Auditor\User\UserProviderInterface;
11
use Exception;
12
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
13
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
14
use Symfony\Component\Security\Core\Security;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
17
class UserProvider implements UserProviderInterface
18
{
19
    private Security $security;
20
21
    private Configuration $configuration;
22
23
    public function __construct(Security $security, Configuration $configuration)
24
    {
25
        $this->security = $security;
26
        $this->configuration = $configuration;
27
    }
28
29
    public function __invoke(): ?AuditorUserInterface
30
    {
31
        $identifier = null;
32
        $username = null;
33
        $tokenUser = null;
34
35
        $token = $this->getToken();
36
        if (null !== $token) {
37
            $tokenUser = $token->getUser();
38
        }
39
40
        $impersonatorUser = $this->getImpersonatorUser();
41
42
        if ($tokenUser instanceof UserInterface) {
43
            if (method_exists($tokenUser, 'getId')) {
44
                $identifier = $tokenUser->getId();
45
            }
46
47
            $username = '';
48
            if (method_exists($tokenUser, 'getUserIdentifier')) {
49
                $username = $tokenUser->getUserIdentifier();
50
            } elseif (method_exists($tokenUser, 'getUsername')) {
51
                $username = $tokenUser->getUsername();
52
            }
53
        }
54
55
        if ($impersonatorUser instanceof UserInterface) {
56
            $impersonatorUsername = '';
57
            if (method_exists($impersonatorUser, 'getUserIdentifier')) {
58
                $impersonatorUsername = $impersonatorUser->getUserIdentifier();
59
            } elseif (method_exists($impersonatorUser, 'getUsername')) {
60
                $impersonatorUsername = $impersonatorUser->getUsername();
61
            }
62
63
            $username .= '[impersonator '.$impersonatorUsername.']';
64
        }
65
66
        if (null === $identifier && null === $username) {
67
            return null;
68
        }
69
70
        return new User((string) $identifier, $username);
71
    }
72
73
    private function getToken(): ?TokenInterface
74
    {
75
        try {
76
            $token = $this->security->getToken();
77
        } catch (Exception) {
78
            $token = null;
79
        }
80
81
        if (!$token instanceof TokenInterface) {
82
            return null;
83
        }
84
85
        return $token;
86
    }
87
88
    private function getImpersonatorUser(): ?UserInterface
89
    {
90
        $token = $this->security->getToken();
91
92
        if ($token instanceof SwitchUserToken) {
93
            return $token->getOriginalToken()->getUser();
94
        }
95
96
        return null;
97
    }
98
}
99