Completed
Push — master ( c4b22f...ba9072 )
by Matze
08:46
created

UserVO::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BrainExe\Core\Authentication;
4
5
use JsonSerializable;
6
7
/**
8
 * @api
9
 */
10
class UserVO implements JsonSerializable
11
{
12
13
    const PROPERTY_AVATAR = 'avatar';
14
    const PROPERTY_EMAIL  = 'email';
15
16
    const ROLE_ADMIN = 'admin';
17
    const ROLE_USER  = 'user';
18
19
    const AVATAR_1 = 'avatar1.png';
20
    const AVATAR_2 = 'avatar2.png';
21
    const AVATAR_3 = 'avatar3.png';
22
    const AVATAR_4 = 'avatar4.png';
23
    const AVATAR_5 = 'avatar5.png';
24
25
    const ROLES = [
26
        self::ROLE_ADMIN,
27
        self::ROLE_USER
28
    ];
29
30
    const AVATARS        = [
31
        self::AVATAR_1,
32
        self::AVATAR_2,
33
        self::AVATAR_3,
34
        self::AVATAR_4,
35
        self::AVATAR_5,
36
    ];
37
38
    /**
39
     * @var integer
40
     */
41
    public $id;
42
43
    /**
44
     * @var string
45
     */
46
    public $username;
47
48
    /**
49
     * @var string
50
     */
51
    public $password_hash;
52
53
    /**
54
     * @var string
55
     */
56
    public $one_time_secret;
57
58
    /**
59
     * @var string
60
     */
61
    public $password;
62
63
    /**
64
     * @var string
65
     */
66
    public $email;
67
68
    /**
69
     * @var string[]
70
     */
71
    public $roles = [];
72
73
    /**
74
     * @var string
75
     */
76
    public $avatar;
77
78
    /**
79
     * @param string $role
80
     * @return boolean
81
     */
82 1
    public function hasRole($role)
83
    {
84 1
        return in_array($role, $this->roles);
85
    }
86
87
    /**
88
     * @return string[]
89
     */
90 1
    public function getRoles()
91
    {
92 1
        return $this->roles;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 1
    public function getPassword()
99
    {
100 1
        return $this->password_hash;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 1
    public function getSalt()
107
    {
108 1
        return $this->username;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 1
    public function getUsername()
115
    {
116 1
        return $this->username;
117
    }
118
119
    /**
120
     * @return array
121
     */
122 1
    public function jsonSerialize()
123
    {
124 1
        return $this->toArray();
125
    }
126
127
    /**
128
     * @return array
129
     */
130 1
    public function toArray()
131
    {
132
        return [
133 1
            'userId'   => $this->id,
134 1
            'username' => $this->username,
135 1
            'roles'    => $this->roles,
136 1
            'email'    => $this->email,
137 1
            'avatar'   => $this->avatar,
138
        ];
139
    }
140
}
141