| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\Sentry; |
| 5 |
|
| 6 |
/** |
| 7 |
* This class stores the information about the authenticated user for a request. |
| 8 |
* |
| 9 |
* @see https://develop.sentry.dev/sdk/event-payloads/types/#user |
| 10 |
*/ |
| 11 |
final class UserDataBag |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @var string|int|null The unique ID of the user |
| 15 |
*/ |
| 16 |
private $id; |
| 17 |
/** |
| 18 |
* @var string|null The email address of the user |
| 19 |
*/ |
| 20 |
private $email; |
| 21 |
/** |
| 22 |
* @var string|null The IP of the user |
| 23 |
*/ |
| 24 |
private $ipAddress; |
| 25 |
/** |
| 26 |
* @var string|null The username of the user |
| 27 |
*/ |
| 28 |
private $username; |
| 29 |
/** |
| 30 |
* @var string|null the user segment, for apps that divide users in user segments |
| 31 |
*/ |
| 32 |
private $segment; |
| 33 |
/** |
| 34 |
* @var array<string, mixed> Additional data |
| 35 |
*/ |
| 36 |
private $metadata = []; |
| 37 |
/** |
| 38 |
* UserDataBag constructor. |
| 39 |
* |
| 40 |
* @param string|int|null $id |
| 41 |
*/ |
| 42 |
public function __construct($id = null, ?string $email = null, ?string $ipAddress = null, ?string $username = null, ?string $segment = null) |
| 43 |
{ |
| 44 |
$this->setId($id); |
| 45 |
$this->setEmail($email); |
| 46 |
$this->setIpAddress($ipAddress); |
| 47 |
$this->setUsername($username); |
| 48 |
$this->setSegment($segment); |
| 49 |
} |
| 50 |
/** |
| 51 |
* Creates an instance of this object from a user ID. |
| 52 |
* |
| 53 |
* @param string|int $id The ID of the user |
| 54 |
*/ |
| 55 |
public static function createFromUserIdentifier($id) : self |
| 56 |
{ |
| 57 |
return new self($id); |
| 58 |
} |
| 59 |
/** |
| 60 |
* Creates an instance of this object from an IP address. |
| 61 |
* |
| 62 |
* @param string $ipAddress The IP address of the user |
| 63 |
*/ |
| 64 |
public static function createFromUserIpAddress(string $ipAddress) : self |
| 65 |
{ |
| 66 |
return new self(null, null, $ipAddress); |
| 67 |
} |
| 68 |
/** |
| 69 |
* Creates an instance of this object from the given data. |
| 70 |
* |
| 71 |
* @param array<string, mixed> $data The raw data |
| 72 |
*/ |
| 73 |
public static function createFromArray(array $data) : self |
| 74 |
{ |
| 75 |
$instance = new self(); |
| 76 |
foreach ($data as $field => $value) { |
| 77 |
switch ($field) { |
| 78 |
case 'id': |
| 79 |
$instance->setId($value); |
| 80 |
break; |
| 81 |
case 'ip_address': |
| 82 |
$instance->setIpAddress($value); |
| 83 |
break; |
| 84 |
case 'email': |
| 85 |
$instance->setEmail($value); |
| 86 |
break; |
| 87 |
case 'username': |
| 88 |
$instance->setUsername($value); |
| 89 |
break; |
| 90 |
case 'segment': |
| 91 |
$instance->setSegment($value); |
| 92 |
break; |
| 93 |
default: |
| 94 |
$instance->setMetadata($field, $value); |
| 95 |
break; |
| 96 |
} |
| 97 |
} |
| 98 |
return $instance; |
| 99 |
} |
| 100 |
/** |
| 101 |
* Gets the ID of the user. |
| 102 |
* |
| 103 |
* @return string|int|null |
| 104 |
*/ |
| 105 |
public function getId() |
| 106 |
{ |
| 107 |
return $this->id; |
| 108 |
} |
| 109 |
/** |
| 110 |
* Sets the ID of the user. |
| 111 |
* |
| 112 |
* @param string|int|null $id The ID |
| 113 |
*/ |
| 114 |
public function setId($id) : self |
| 115 |
{ |
| 116 |
if ($id !== null && !\is_string($id) && !\is_int($id)) { |
| 117 |
throw new \UnexpectedValueException(\sprintf('Expected an integer or string value for the $id argument. Got: "%s".', \get_debug_type($id))); |
| 118 |
} |
| 119 |
$this->id = $id; |
| 120 |
return $this; |
| 121 |
} |
| 122 |
/** |
| 123 |
* Gets the username of the user. |
| 124 |
*/ |
| 125 |
public function getUsername() : ?string |
| 126 |
{ |
| 127 |
return $this->username; |
| 128 |
} |
| 129 |
/** |
| 130 |
* Sets the username of the user. |
| 131 |
* |
| 132 |
* @param string|null $username The username |
| 133 |
*/ |
| 134 |
public function setUsername(?string $username) : self |
| 135 |
{ |
| 136 |
$this->username = $username; |
| 137 |
return $this; |
| 138 |
} |
| 139 |
/** |
| 140 |
* Gets the email of the user. |
| 141 |
*/ |
| 142 |
public function getEmail() : ?string |
| 143 |
{ |
| 144 |
return $this->email; |
| 145 |
} |
| 146 |
/** |
| 147 |
* Sets the email of the user. |
| 148 |
* |
| 149 |
* @param string|null $email The email |
| 150 |
*/ |
| 151 |
public function setEmail(?string $email) : self |
| 152 |
{ |
| 153 |
$this->email = $email; |
| 154 |
return $this; |
| 155 |
} |
| 156 |
/** |
| 157 |
* Gets the segement of the user. |
| 158 |
* |
| 159 |
* @deprecated since version 4.4. To be removed in version 5.0 |
| 160 |
*/ |
| 161 |
public function getSegment() : ?string |
| 162 |
{ |
| 163 |
return $this->segment; |
| 164 |
} |
| 165 |
/** |
| 166 |
* Sets the segment of the user. |
| 167 |
* |
| 168 |
* @param string|null $segment The segment |
| 169 |
* |
| 170 |
* @deprecated since version 4.4. To be removed in version 5.0. You may use a custom tag or context instead. |
| 171 |
*/ |
| 172 |
public function setSegment(?string $segment) : self |
| 173 |
{ |
| 174 |
$this->segment = $segment; |
| 175 |
return $this; |
| 176 |
} |
| 177 |
/** |
| 178 |
* Gets the ip address of the user. |
| 179 |
*/ |
| 180 |
public function getIpAddress() : ?string |
| 181 |
{ |
| 182 |
return $this->ipAddress; |
| 183 |
} |
| 184 |
/** |
| 185 |
* Sets the ip address of the user. |
| 186 |
* |
| 187 |
* @param string|null $ipAddress The ip address |
| 188 |
*/ |
| 189 |
public function setIpAddress(?string $ipAddress) : self |
| 190 |
{ |
| 191 |
if ($ipAddress !== null) { |
| 192 |
// Strip brackets from IPv6 addresses (e.g. [::1] -> ::1) |
| 193 |
if (\strpos($ipAddress, '[') === 0 && \substr($ipAddress, -1) === ']') { |
| 194 |
$ipAddress = \substr($ipAddress, 1, -1); |
| 195 |
} |
| 196 |
if (\filter_var($ipAddress, \FILTER_VALIDATE_IP) === \false) { |
| 197 |
$client = SentrySdk::getCurrentHub()->getClient(); |
| 198 |
if ($client !== null) { |
| 199 |
$client->getOptions()->getLoggerOrNullLogger()->debug(\sprintf('The "%s" value is not a valid IP address.', $ipAddress)); |
| 200 |
} |
| 201 |
return $this; |
| 202 |
} |
| 203 |
} |
| 204 |
$this->ipAddress = $ipAddress; |
| 205 |
return $this; |
| 206 |
} |
| 207 |
/** |
| 208 |
* Gets additional metadata. |
| 209 |
* |
| 210 |
* @return array<string, mixed> |
| 211 |
*/ |
| 212 |
public function getMetadata() : array |
| 213 |
{ |
| 214 |
return $this->metadata; |
| 215 |
} |
| 216 |
/** |
| 217 |
* Sets the given field in the additional metadata. |
| 218 |
* |
| 219 |
* @param string $name The name of the field |
| 220 |
* @param mixed $value The value |
| 221 |
*/ |
| 222 |
public function setMetadata(string $name, $value) : self |
| 223 |
{ |
| 224 |
$this->metadata[$name] = $value; |
| 225 |
return $this; |
| 226 |
} |
| 227 |
/** |
| 228 |
* Removes the given field from the additional metadata. |
| 229 |
* |
| 230 |
* @param string $name The name of the field |
| 231 |
*/ |
| 232 |
public function removeMetadata(string $name) : self |
| 233 |
{ |
| 234 |
unset($this->metadata[$name]); |
| 235 |
return $this; |
| 236 |
} |
| 237 |
/** |
| 238 |
* Merges the given context with this one. |
| 239 |
* |
| 240 |
* @param UserDataBag $other The context to merge the data with |
| 241 |
* |
| 242 |
* @return $this |
| 243 |
*/ |
| 244 |
public function merge(self $other) : self |
| 245 |
{ |
| 246 |
$this->id = $other->id; |
| 247 |
$this->email = $other->email; |
| 248 |
$this->ipAddress = $other->ipAddress; |
| 249 |
$this->username = $other->username; |
| 250 |
$this->segment = $other->segment; |
| 251 |
$this->metadata = \array_merge($this->metadata, $other->metadata); |
| 252 |
return $this; |
| 253 |
} |
| 254 |
} |
| 255 |
|