Completed
Push — master ( f47ecb...1c0a88 )
by Roni
14:50
created

UserAwareComponent   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 9
Bugs 1 Features 1
Metric Value
wmc 15
c 9
b 1
f 1
lcom 1
cbo 5
dl 0
loc 84
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getContainer() 0 4 1
A getUser() 0 16 4
B getImpersonatingUser() 0 16 5
A getUsername() 0 10 2
A getAnonymousUserName() 0 10 3
1
<?php
2
3
/*
4
 * This file is part of the XiideaEasyAuditBundle package.
5
 *
6
 * (c) Xiidea <http://www.xiidea.net>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Xiidea\EasyAuditBundle\Common;
13
14
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
16
use Symfony\Component\Security\Core\Role\SwitchUserRole;
17
use Xiidea\EasyAuditBundle\Traits\ServiceContainerGetterMethods;
18
19
class UserAwareComponent implements ContainerAwareInterface
20
{
21
    use ServiceContainerGetterMethods;
22
    use ContainerAwareTrait;
23
24
    /**
25
     * @return \Symfony\Component\DependencyInjection\ContainerInterface
26
     */
27
    protected function getContainer()
28
    {
29
        return $this->container;
30
    }
31
32
    /**
33
     * Get a user from the Security Context
34
     *
35
     * @return mixed
36
     * @throws \LogicException If SecurityBundle is not available
37
     */
38
    public function getUser()
39
    {
40
        if (!$this->getContainer()->has('security.token_storage')) {
41
            throw new \LogicException('The SecurityBundle is not registered in your application.');
42
        }
43
44
        if (null === $token = $this->getContainer()->get('security.token_storage')->getToken()) {
45
            return null;
46
        }
47
48
        if (!is_object($user = $token->getUser())) {
49
            return null;
50
        }
51
52
        return $user;
53
    }
54
55
    /**
56
     * @return mixed
57
     */
58
    protected final function getImpersonatingUser()
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
59
    {
60
        if (null === $token = $this->getContainer()->get('security.token_storage')->getToken()) {
61
            return null;
62
        }
63
64
        if ($this->getContainer()->get('security.authorization_checker')->isGranted('ROLE_PREVIOUS_ADMIN')) {
65
            foreach ($token->getRoles() as $role) {
66
                if ($role instanceof SwitchUserRole) {
67
                    return $role->getSource()->getUser();
68
                }
69
            }
70
        }
71
72
        return null;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getUsername()
79
    {
80
        $user = $this->getUser();
81
82
        if(empty($user)) {
83
            return $this->getAnonymousUserName();
84
        }
85
86
        return $user->getUsername();
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    protected function getAnonymousUserName()
93
    {
94
        $request = $this->getRequest();
95
96
        if ($request && $request->getClientIp()) {
97
            return "Anonymous";
98
        }
99
100
        return "By Command";
101
    }
102
}
103