Completed
Push — feature/evo-2472-whoami ( 843346...066c1c )
by
unknown
83:53 queued 67:03
created

SecurityUser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 77.27%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 100
ccs 17
cts 22
cp 0.7727
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getRoles() 0 4 1
A getPassword() 0 4 1
A getSalt() 0 4 1
A getUsername() 0 7 2
A eraseCredentials() 0 3 1
A getUser() 0 4 1
A __toString() 0 6 2
1
<?php
2
/**
3
 * security consultant entity
4
 */
5
6
namespace Graviton\SecurityBundle\Entities;
7
8
use Symfony\Component\Security\Core\Role\Role;
9
use Symfony\Component\Security\Core\User\UserInterface;
10
11
/**
12
 * Class SecurityUser
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class SecurityUser implements UserInterface
19
{
20
    /**
21
     * @var Object
22
     */
23
    private $user;
24
25
    /**
26
     * @var Role[]
27
     */
28
    private $roles;
29
30
31
    /**
32
     * Constructor of the class.
33
     *
34
     * @param object $user  the user
35
     * @param Role[] $roles roles for the contract
36
     */
37 6
    public function __construct($user, array $roles = array())
38
    {
39 6
        $this->user = $user;
40 6
        $this->roles = $roles;
41 6
    }
42
43
    /**
44
     * Returns the roles granted to the user.
45
     *
46
     * @return Role[] The user roles
47
     */
48 3
    public function getRoles()
49
    {
50 3
        return $this->roles;
51
    }
52
53
    /**
54
     * Returns the password used to authenticate the user.
55
     *
56
     * @return string The password
57
     */
58 1
    public function getPassword()
59
    {
60 1
        return '';
61
    }
62
63
    /**
64
     * Returns the salt that was originally used to encode the password.
65
     *
66
     * @return null The salt
67
     */
68 1
    public function getSalt()
69
    {
70 1
        return null;
71
    }
72
73
    /**
74
     * Returns the username used to authenticate the user.
75
     *
76
     * @return string The username
77
     */
78 2
    public function getUsername()
79
    {
80 2
        if (method_exists($this->user, 'getUsername')) {
81 2
            return $this->user->getUsername();
82
        }
83
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Symfony\Component\Securi...rInterface::getUsername of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
84
    }
85
86
    /**
87
     * Removes sensitive data from the user.
88
     *
89
     * This is important if, at any given point, sensitive information like
90
     * the plain-text password is stored on this object.
91
     *
92
     * @return void
93
     */
94 3
    public function eraseCredentials()
95
    {
96 3
    }
97
98
    /**
99
     * Provides the consultant object.
100
     *
101
     * @return Object
102
     */
103 1
    public function getUser()
104
    {
105 1
        return $this->user;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function __toString()
112
    {
113
        $roles = $this->$this->getRoles();
114
        $username = $this->getUsername() ? $this->getUsername() : 'anonymous';
115
        return reser($roles).':'.$username;
116
    }
117
}
118