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