Passed
Pull Request — master (#84)
by Maxime
02:53
created

TokenStorageUserProvider::getUser()   C

Complexity

Conditions 12
Paths 88

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 12
eloc 26
c 6
b 0
f 0
nc 88
nop 0
dl 0
loc 43
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace DH\DoctrineAuditBundle\User;
4
5
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
6
use Symfony\Component\Security\Core\Role\SwitchUserRole;
7
use Symfony\Component\Security\Core\Security;
8
use Symfony\Component\Security\Core\User\UserInterface as BaseUserInterface;
9
10
class TokenStorageUserProvider implements UserProviderInterface
11
{
12
    private $security;
13
14
    public function __construct(Security $security)
15
    {
16
        $this->security = $security;
17
    }
18
19
    public function getUser(): ?UserInterface
20
    {
21
        $user = null;
22
23
        try {
24
            $token = $this->security->getToken();
25
        } catch (\Exception $e) {
26
            $token = null;
27
        }
28
29
        if (null === $token) {
30
            return null;
31
        }
32
33
        $tokenUser = $token->getUser();
34
        if ($tokenUser instanceof BaseUserInterface) {
35
            $impersonation = '';
36
            if ($this->security->isGranted('ROLE_PREVIOUS_ADMIN')) {
37
                $impersonatorUser = null;
38
                // Symfony > 4.3
39
                if ($token instanceof SwitchUserToken) {
40
                    $impersonatorUser = $token->getOriginalToken()->getUser();
41
                } else {
42
                    foreach ($this->security->getToken()->getRoles() as $role) {
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Securi...enInterface::getRoles() has been deprecated: since Symfony 4.3, use the getRoleNames() method instead ( Ignorable by Annotation )

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

42
                    foreach (/** @scrutinizer ignore-deprecated */ $this->security->getToken()->getRoles() as $role) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
43
                        if ($role instanceof SwitchUserRole) {
44
                            $impersonatorUser = $role->getSource()->getUser();
45
46
                            break;
47
                        }
48
                    }
49
                }
50
51
                if (\is_object($impersonatorUser)) {
52
                    $id = method_exists($impersonatorUser, 'getId') ? $impersonatorUser->getId() : null;
53
                    $username = method_exists($impersonatorUser, 'getUsername') ? $impersonatorUser->getUsername() : (string) $impersonatorUser;
54
                    $impersonation = ' [impersonator '.$username.':'.$id.']';
55
                }
56
            }
57
            $id = method_exists($tokenUser, 'getId') ? $tokenUser->getId() : null;
58
            $user = new User($id, $tokenUser->getUsername().$impersonation);
59
        }
60
61
        return $user;
62
    }
63
}
64