Security   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 109
Duplicated Lines 12.84 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 14
c 2
b 0
f 1
lcom 1
cbo 6
dl 14
loc 109
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getStorage() 0 4 1
A getAuthenChecker() 0 4 1
A getToken() 0 4 1
A getUser() 14 14 4
A isGranted() 0 4 1
A isLoggedIn() 0 4 1
A getUsername() 0 10 2
A simulate() 0 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace DoS\UserBundle\Security;
4
5
use Symfony\Component\DependencyInjection\ContainerInterface;
6
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
7
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
8
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
9
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
10
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
11
use DoS\UserBundle\Model\UserInterface;
12
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
13
14
class Security
15
{
16
    /**
17
     * @var ContainerInterface
18
     */
19
    protected $container;
20
21
    public function __construct(ContainerInterface $container)
22
    {
23
        $this->container = $container;
24
    }
25
26
    /**
27
     * @return TokenStorageInterface
28
     */
29
    private function getStorage()
30
    {
31
        return $this->container->get('security.token_storage');
32
    }
33
34
    /**
35
     * @return AuthorizationCheckerInterface
36
     */
37
    private function getAuthenChecker()
38
    {
39
        return $this->container->get('security.authorization_checker');
40
    }
41
42
    /**
43
     * @return null|TokenInterface
44
     */
45
    public function getToken()
46
    {
47
        return $this->getStorage()->getToken();
48
    }
49
50
    /**
51
     * @return string|void
52
     */
53
    public function getUsername()
54
    {
55
        $token = $this->getToken();
56
57
        if ($token instanceof TokenInterface) {
58
            return $token->getUsername();
59
        }
60
61
        return;
62
    }
63
64
    /**
65
     * @return UserInterface|void
66
     */
67 View Code Duplication
    public function getUser()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        $token = $this->getToken();
70
71
        if ($token instanceof TokenInterface) {
72
            if ($token instanceof AnonymousToken || $token->getUser() === 'anon.') {
73
                return;
74
            }
75
76
            return $token->getUser();
77
        }
78
79
        return;
80
    }
81
82
    /**
83
     * @param      $attributes
84
     * @param null $object
85
     *
86
     * @return bool
87
     */
88
    public function isGranted($attributes, $object = null)
89
    {
90
        return $this->getAuthenChecker()->isGranted($attributes, $object);
91
    }
92
93
    /**
94
     * @return bool
95
     */
96
    public function isLoggedIn()
97
    {
98
        return $this->isGranted('IS_AUTHENTICATED_REMEMBERED');
99
    }
100
101
    /**
102
     * @param string $username
103
     * @param string $password
104
     * @param string $firewall
105
     *
106
     * @return \Symfony\Component\Security\Core\User\UserInterface
107
     *
108
     * @throws UsernameNotFoundException
109
     */
110
    public function simulate($username, $password = null, $firewall = 'main')
111
    {
112
        if (!$user = $this->container->get('dos.provider.user')->loadUserByUsername($username)) {
113
            throw new UsernameNotFoundException();
114
        }
115
116
        $this->container->get('security.token_storage')->setToken(
117
            new UsernamePasswordToken($user, $password, $firewall, $user->getRoles())
118
        );
119
120
        return $user;
121
    }
122
}
123