-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOptions.php
More file actions
184 lines (151 loc) · 4.26 KB
/
Options.php
File metadata and controls
184 lines (151 loc) · 4.26 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
<?php
declare(strict_types=1);
namespace Hawk;
/**
* Class Options is responsible for configuring the Hawk catcher.
*/
class Options
{
/**
* @var string
*/
private $integrationToken = '';
/**
* @var string
*/
private $url = 'https://k1.hawk.so/';
/**
* @var string
*/
private $release = '';
/**
* @var int|null
*/
private $errorTypes = null;
/**
* @var bool
*/
private $captureSilencedErrors = false;
/**
* @var callable|null
*/
private $beforeSend = null;
/**
* @var int
*/
private $timeout = 2;
/**
* @var string
*/
private $transport = 'curl';
/**
* Map of accepted option keys to class properties.
*/
private const OPTION_KEYS = [
'integrationToken' => 'integrationToken',
'integration_token' => 'integrationToken',
'url' => 'url',
'release' => 'release',
'errorTypes' => 'errorTypes',
'error_types' => 'errorTypes',
'captureSilencedErrors' => 'captureSilencedErrors',
'capture_silenced_errors' => 'captureSilencedErrors',
'beforeSend' => 'beforeSend',
'before_send' => 'beforeSend',
'timeout' => 'timeout',
'transport' => 'transport',
];
/**
* Options constructor.
*
* @param array $options Associative array of options to initialize.
*/
public function __construct(array $options = [])
{
foreach ($options as $key => $value) {
$normalizedKey = self::OPTION_KEYS[$key] ?? null;
if ($normalizedKey === null) {
throw new \InvalidArgumentException("Unknown option: $key");
}
$this->setOption($normalizedKey, $value);
}
}
/**
* Set a class property based on the normalized option key.
*
* @param string $key
* @param mixed $value
*/
private function setOption(string $key, $value): void
{
switch ($key) {
case 'integrationToken':
case 'release':
case 'url':
case 'transport':
if (!is_string($value)) {
throw new \InvalidArgumentException("Option '$key' must be a string.");
}
$this->$key = $value;
break;
case 'errorTypes':
if (!is_int($value) && $value !== null) {
throw new \InvalidArgumentException("Option 'errorTypes' must be an integer or null.");
}
$this->errorTypes = $value;
break;
case 'captureSilencedErrors':
if (!is_bool($value)) {
throw new \InvalidArgumentException("Option 'captureSilencedErrors' must be a boolean.");
}
$this->captureSilencedErrors = $value;
break;
case 'beforeSend':
if (!is_callable($value) && $value !== null) {
throw new \InvalidArgumentException("Option 'beforeSend' must be callable or null.");
}
$this->beforeSend = $value;
break;
case 'timeout':
if (!is_int($value)) {
throw new \InvalidArgumentException("Option 'timeout' must be an integer.");
}
$this->timeout = $value;
break;
default:
throw new \InvalidArgumentException("Unknown option '$key'.");
}
}
public function getIntegrationToken(): string
{
return $this->integrationToken;
}
public function getUrl(): string
{
return $this->url;
}
public function getRelease(): string
{
return $this->release;
}
public function getErrorTypes(): int
{
return $this->errorTypes ?? error_reporting();
}
public function shouldCaptureSilencedErrors(): bool
{
return $this->captureSilencedErrors;
}
public function getBeforeSend(): ?callable
{
return $this->beforeSend;
}
public function getTimeout(): int
{
return $this->timeout;
}
public function getTransport(): string
{
return $this->transport;
}
}