Security::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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