-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathDsn.php
More file actions
236 lines (197 loc) · 6.13 KB
/
Dsn.php
File metadata and controls
236 lines (197 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
declare(strict_types=1);
namespace Sentry;
/**
* This class represents a Sentry DSN that can be obtained from the Settings
* page of a project.
*
* @author Stefano Arlandini <sarlandini@alice.it>
*/
final class Dsn
{
/**
* @var string Regex to match the organization ID in the host.
* This only applies to Sentry SaaS DSNs that contain the organization ID.
*/
private const SENTRY_ORG_ID_REGEX = '/^o(\d+)\./';
/**
* @var string The protocol to be used to access the resource
*/
private $scheme;
/**
* @var string The host that holds the resource
*/
private $host;
/**
* @var int The port on which the resource is exposed
*/
private $port;
/**
* @var string The public key to authenticate the SDK
*/
private $publicKey;
/**
* @var string The ID of the resource to access
*/
private $projectId;
/**
* @var string The specific resource that the web client wants to access
*/
private $path;
/**
* @var int|null
*/
private $orgId;
/**
* Class constructor.
*
* @param string $scheme The protocol to be used to access the resource
* @param string $host The host that holds the resource
* @param int $port The port on which the resource is exposed
* @param string $projectId The ID of the resource to access
* @param string $path The specific resource that the web client wants to access
* @param string $publicKey The public key to authenticate the SDK
* @param ?int $orgId The org ID
*/
private function __construct(string $scheme, string $host, int $port, string $projectId, string $path, string $publicKey, ?int $orgId = null)
{
$this->scheme = $scheme;
$this->host = $host;
$this->port = $port;
$this->projectId = $projectId;
$this->path = $path;
$this->publicKey = $publicKey;
$this->orgId = $orgId;
}
/**
* Creates an instance of this class by parsing the given string.
*
* @param string $value The string to parse
*/
public static function createFromString(string $value): self
{
$parsedDsn = parse_url($value);
if ($parsedDsn === false) {
throw new \InvalidArgumentException(\sprintf('The "%s" DSN is invalid.', $value));
}
foreach (['scheme', 'host', 'path', 'user'] as $component) {
if (!isset($parsedDsn[$component]) || (isset($parsedDsn[$component]) && empty($parsedDsn[$component]))) {
throw new \InvalidArgumentException(\sprintf('The "%s" DSN must contain a scheme, a host, a user and a path component.', $value));
}
}
if (!\in_array($parsedDsn['scheme'], ['http', 'https'], true)) {
throw new \InvalidArgumentException(\sprintf('The scheme of the "%s" DSN must be either "http" or "https".', $value));
}
$segmentPaths = explode('/', $parsedDsn['path']);
$projectId = array_pop($segmentPaths);
$lastSlashPosition = strrpos($parsedDsn['path'], '/');
$path = $parsedDsn['path'];
if ($lastSlashPosition !== false) {
$path = substr($parsedDsn['path'], 0, $lastSlashPosition);
}
$orgId = null;
if (preg_match(self::SENTRY_ORG_ID_REGEX, $parsedDsn['host'], $matches) == 1) {
$orgId = (int) $matches[1];
}
return new self(
$parsedDsn['scheme'],
$parsedDsn['host'],
$parsedDsn['port'] ?? ($parsedDsn['scheme'] === 'http' ? 80 : 443),
$projectId,
$path,
$parsedDsn['user'],
$orgId
);
}
/**
* Gets the protocol to be used to access the resource.
*/
public function getScheme(): string
{
return $this->scheme;
}
/**
* Gets the host that holds the resource.
*/
public function getHost(): string
{
return $this->host;
}
/**
* Gets the port on which the resource is exposed.
*/
public function getPort(): int
{
return $this->port;
}
/**
* Gets the specific resource that the web client wants to access.
*/
public function getPath(): string
{
return $this->path;
}
/**
* Gets the ID of the resource to access.
*/
public function getProjectId(): string
{
return $this->projectId;
}
/**
* Gets the public key to authenticate the SDK.
*/
public function getPublicKey(): string
{
return $this->publicKey;
}
public function getOrgId(): ?int
{
return $this->orgId;
}
/**
* Returns the URL of the API for the envelope endpoint.
*/
public function getEnvelopeApiEndpointUrl(): string
{
return $this->getBaseEndpointUrl() . '/envelope/';
}
/**
* Returns the URL of the API for the CSP report endpoint.
*/
public function getCspReportEndpointUrl(): string
{
return $this->getBaseEndpointUrl() . '/security/?sentry_key=' . $this->publicKey;
}
/**
* @see https://www.php.net/manual/en/language.oop5.magic.php#object.tostring
*/
public function __toString(): string
{
$url = $this->scheme . '://' . $this->publicKey;
$url .= '@' . $this->host;
if (($this->scheme === 'http' && $this->port !== 80) || ($this->scheme === 'https' && $this->port !== 443)) {
$url .= ':' . $this->port;
}
if ($this->path !== null) {
$url .= $this->path;
}
$url .= '/' . $this->projectId;
return $url;
}
/**
* Returns the base url to Sentry from the DSN.
*/
private function getBaseEndpointUrl(): string
{
$url = $this->scheme . '://' . $this->host;
if (($this->scheme === 'http' && $this->port !== 80) || ($this->scheme === 'https' && $this->port !== 443)) {
$url .= ':' . $this->port;
}
if ($this->path !== null) {
$url .= $this->path;
}
$url .= '/api/' . $this->projectId;
return $url;
}
}