Completed
Push — feature/evo-2472-whoami ( 066c1c...01f6e4 )
by
unknown
09:00
created

SecurityUser::hasRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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
     * Check if user has role
110
     * @param string $role
111
     * @return bool
112
     */
113
    public function hasRole($role)
114
    {
115
        return in_array($role, $this->roles);
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function __toString()
122
    {
123
        $roles = $this->$this->getRoles();
124
        $username = $this->getUsername() ? $this->getUsername() : 'anonymous';
125
        return reser($roles).':'.$username;
126
    }
127
}
128