1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This software may be modified and distributed under the terms |
5
|
|
|
* of the MIT license. See the LICENSE file for details. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Happyr\Auth0Bundle\Model\Authentication\UserProfile; |
9
|
|
|
|
10
|
|
|
use Happyr\Auth0Bundle\Model\ApiResponse; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Tobias Nyholm <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
final class UserInfo implements ApiResponse, \ArrayAccess |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array the raw data from the API |
19
|
|
|
*/ |
20
|
|
|
private $data; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param array $data |
24
|
|
|
*/ |
25
|
|
|
private function __construct(array $data) |
26
|
|
|
{ |
27
|
|
|
$this->data = $data; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $data |
32
|
|
|
* |
33
|
|
|
* @return self |
34
|
|
|
*/ |
35
|
|
|
public static function create($data) |
36
|
|
|
{ |
37
|
|
|
return new self($data); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function __toString() |
44
|
|
|
{ |
45
|
|
|
if (!empty($this->data['email'])) { |
46
|
|
|
return (string) $this->data['email']; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!empty($this->data['name'])) { |
50
|
|
|
return (string) $this->data['name']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (!empty($this->data['nickname'])) { |
54
|
|
|
return (string) $this->data['nickname']; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return ''; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function offsetExists($offset) |
61
|
|
|
{ |
62
|
|
|
return isset($this->data[$offset]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function offsetGet($offset) |
66
|
|
|
{ |
67
|
|
|
return $this->data[$offset]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function offsetSet($offset, $value) |
71
|
|
|
{ |
72
|
|
|
throw new \LogicException('The UserInfo object is read only'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function offsetUnset($offset) |
76
|
|
|
{ |
77
|
|
|
throw new \LogicException('The UserInfo object is read only'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function isEmailVerified() |
84
|
|
|
{ |
85
|
|
|
return $this->data['email_verified']; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getEmail() |
92
|
|
|
{ |
93
|
|
|
return $this->data['email']; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getUsername() |
100
|
|
|
{ |
101
|
|
|
if (!isset($this->data['username'])) { |
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->data['username']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getClientId() |
112
|
|
|
{ |
113
|
|
|
return $this->data['clientID']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return \DateTimeInterface |
118
|
|
|
*/ |
119
|
|
|
public function getUpdatedAt() |
120
|
|
|
{ |
121
|
|
|
return new \DateTimeImmutable($this->data['updated_at']); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function getName() |
128
|
|
|
{ |
129
|
|
|
return $this->data['name'] ?? $this->data['nickname']; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function getPicture() |
136
|
|
|
{ |
137
|
|
|
return $this->data['picture']; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getNickname() |
144
|
|
|
{ |
145
|
|
|
return $this->data['nickname']; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function getIdentities() |
152
|
|
|
{ |
153
|
|
|
return $this->data['identities']; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return \DateTimeInterface |
158
|
|
|
*/ |
159
|
|
|
public function getCreatedAt() |
160
|
|
|
{ |
161
|
|
|
return new \DateTimeImmutable($this->data['created_at']); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public function getUserId() |
168
|
|
|
{ |
169
|
|
|
return $this->data['sub'] ?? $this->data['user_id']; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public function getRoles() |
176
|
|
|
{ |
177
|
|
|
if (!isset($this->data['roles'])) { |
178
|
|
|
return []; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this->data['roles']; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $name |
186
|
|
|
* @param mixed $default |
187
|
|
|
* |
188
|
|
|
* @return mixed |
189
|
|
|
*/ |
190
|
|
View Code Duplication |
public function getAppMetadata($name, $default = null) |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
if (!isset($this->data['app_metadata'])) { |
193
|
|
|
return $default; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (!array_key_exists($name, $this->data['app_metadata'])) { |
197
|
|
|
return $default; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $this->data['app_metadata'][$name]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $name |
205
|
|
|
* @param mixed $default |
206
|
|
|
* |
207
|
|
|
* @return mixed |
208
|
|
|
*/ |
209
|
|
View Code Duplication |
public function getUserMetadata($name, $default = null) |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
if (!isset($this->data['user_metadata'])) { |
212
|
|
|
return $default; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if (!array_key_exists($name, $this->data['user_metadata'])) { |
216
|
|
|
return $default; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $this->data['user_metadata'][$name]; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.