KuleuvenUser::getRoles()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Kuleuven\AuthenticationBundle\Model;
4
5
use Kuleuven\AuthenticationBundle\Traits\ShibbolethAttributesResolverTrait;
6
7
class KuleuvenUser implements KuleuvenUserInterface
8
{
9
    use ShibbolethAttributesResolverTrait;
10
11
    /**
12
     * @var string
13
     */
14
    protected $username;
15
16
    /**
17
     * @var array
18
     */
19
    protected $roles;
20
21
    /**
22
     * @param string $username
23
     * @param array  $attributes
24
     * @param array  $roles
25
     */
26
    public function __construct($username, array $attributes, $roles = [])
27
    {
28
        $this->username = $username;
29
30
        $this->setAttributes($attributes);
31
32
        if ($this->isStudent()) {
33
            $roles[] = 'ROLE_STUDENT';
34
        }
35
        if ($this->isEmployee()) {
36
            $roles[] = 'ROLE_EMPLOYEE';
37
        }
38
        if ($this->isFaculty()) {
39
            $roles[] = 'ROLE_FACULTY';
40
        }
41
        if ($this->isMember()) {
42
            $roles[] = 'ROLE_MEMBER';
43
        }
44
        if ($this->isStaff()) {
45
            $roles[] = 'ROLE_STAFF';
46
        }
47
        $this->roles = !empty($roles) ? $roles : [];
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getUsername()
54
    {
55
        return $this->username;
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function getRoles()
62
    {
63
        return $this->roles;
64
    }
65
66
    /**
67
     * @return null
68
     */
69
    public function getPassword()
70
    {
71
        return null;
72
    }
73
74
    /**
75
     * @return null
76
     */
77
    public function getSalt()
78
    {
79
        return null;
80
    }
81
82
    /**
83
     *
84
     */
85
    public function eraseCredentials()
86
    {
87
        // noop
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function serialize()
94
    {
95
        return serialize(
96
            [
97
                $this->username,
98
                $this->attributes,
99
            ]
100
        );
101
    }
102
103
    /**
104
     * @param string $serialized
105
     */
106
    public function unserialize($serialized)
107
    {
108
        $data = unserialize($serialized);
109
        $data = array_merge($data, array_fill(0, 5, null));
110
111
        list(
112
            $this->username,
113
            $this->attributes,
114
            ) = $data;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function __toString()
121
    {
122
        return (string)$this->getUsername();
123
    }
124
}
125