Passed
Push — master ( 989659...d90b52 )
by Damien
04:23
created

UserProvider::__invoke()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 13
c 0
b 0
f 0
nc 12
nop 0
dl 0
loc 25
rs 8.4444
1
<?php
2
3
namespace DH\AuditorBundle\User;
4
5
use DH\Auditor\Provider\Doctrine\Configuration;
6
use DH\Auditor\User\User;
7
use DH\Auditor\User\UserInterface as AuditorUserInterface;
8
use DH\Auditor\User\UserProviderInterface;
9
use Exception;
10
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
11
use Symfony\Component\Security\Core\Role\SwitchUserRole;
1 ignored issue
show
Bug introduced by
The type Symfony\Component\Securi...ore\Role\SwitchUserRole 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...
12
use Symfony\Component\Security\Core\Security;
13
use Symfony\Component\Security\Core\User\UserInterface;
14
15
class UserProvider implements UserProviderInterface
16
{
17
    /**
18
     * @var Security
19
     */
20
    private $security;
21
22
    /**
23
     * @var Configuration
24
     */
25
    private $configuration;
26
27
    public function __construct(Security $security, Configuration $configuration)
28
    {
29
        $this->security = $security;
30
        $this->configuration = $configuration;
31
    }
32
33
    public function __invoke(): ?AuditorUserInterface
34
    {
35
        $tokenUser = $this->getTokenUser();
36
        $impersonatorUser = $this->getImpersonatorUser();
37
38
        $identifier = null;
39
        $username = null;
40
41
        if (null !== $tokenUser && $tokenUser instanceof UserInterface) {
42
            if (method_exists($tokenUser, 'getId')) {
43
                $identifier = $tokenUser->getId();
44
            }
45
46
            $username = $tokenUser->getUsername();
47
        }
48
49
        if (null !== $impersonatorUser && $impersonatorUser instanceof UserInterface) {
50
            $username .= sprintf('[impersonator %s]', $impersonatorUser->getUsername());
51
        }
52
53
        if (null === $identifier && null === $username) {
54
            return null;
55
        }
56
57
        return new User($identifier, $username);
58
    }
59
60
    /**
61
     * @return null|string|UserInterface
62
     */
63
    private function getTokenUser()
64
    {
65
        try {
66
            $token = $this->security->getToken();
67
        } catch (Exception $e) {
68
            $token = null;
69
        }
70
71
        if (null === $token) {
72
            return null;
73
        }
74
75
        $tokenUser = $token->getUser();
76
        if ($tokenUser instanceof UserInterface) {
77
            return $tokenUser;
78
        }
79
80
        return null;
81
    }
82
83
    /**
84
     * @return null|string|UserInterface
85
     */
86
    private function getImpersonatorUser()
87
    {
88
        $token = $this->security->getToken();
89
90
        // Symfony >= 5
91
        if (class_exists(SwitchUserToken::class) && $token instanceof SwitchUserToken) {
92
            return $token->getOriginalToken()->getUser();
93
        }
94
95
        // Symfony < 5
96
        $roles = [];
97
        if (null !== $token) {
98
            $roles = method_exists($token, 'getRoleNames') ? $token->getRoleNames() : $token->getRoles();
1 ignored issue
show
Bug introduced by
The method getRoles() does not exist on Symfony\Component\Securi...on\Token\TokenInterface. Did you maybe mean getRoleNames()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
            $roles = method_exists($token, 'getRoleNames') ? $token->getRoleNames() : $token->/** @scrutinizer ignore-call */ getRoles();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
        }
100
101
        foreach ($roles as $role) {
102
            if ($role instanceof SwitchUserRole) {
103
                return $role->getSource()->getUser();
104
            }
105
        }
106
107
        return null;
108
    }
109
}
110