|
5 | 5 | namespace Hawk; |
6 | 6 |
|
7 | 7 | /** |
8 | | - * Class Options is responsible for configuring catcher |
9 | | - * |
10 | | - * @package Hawk |
| 8 | + * Class Options is responsible for configuring the Hawk catcher. |
11 | 9 | */ |
12 | 10 | class Options |
13 | 11 | { |
14 | 12 | /** |
15 | | - * Default available options |
16 | | - * |
17 | | - * @var array |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + private $integrationToken = ''; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var string |
18 | 19 | */ |
19 | | - private $options = [ |
20 | | - 'integrationToken' => '', |
21 | | - 'url' => 'https://k1.hawk.so/', |
22 | | - 'release' => '', |
23 | | - 'error_types' => \E_ALL, |
24 | | - 'beforeSend' => null, |
25 | | - 'timeout' => 10, |
| 20 | + private $url = 'https://k1.hawk.so/'; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + private $release = ''; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var int|null |
| 29 | + */ |
| 30 | + private $errorTypes = null; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var bool |
| 34 | + */ |
| 35 | + private $captureSilencedErrors = false; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var callable|null |
| 39 | + */ |
| 40 | + private $beforeSend = null; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var int |
| 44 | + */ |
| 45 | + private $timeout = 10; |
| 46 | + |
| 47 | + /** |
| 48 | + * Map of accepted option keys to class properties. |
| 49 | + */ |
| 50 | + private const OPTION_KEYS = [ |
| 51 | + 'integrationToken' => 'integrationToken', |
| 52 | + 'integration_token' => 'integrationToken', |
| 53 | + 'url' => 'url', |
| 54 | + 'release' => 'release', |
| 55 | + 'errorTypes' => 'errorTypes', |
| 56 | + 'error_types' => 'errorTypes', |
| 57 | + 'captureSilencedErrors' => 'captureSilencedErrors', |
| 58 | + 'capture_silenced_errors' => 'captureSilencedErrors', |
| 59 | + 'beforeSend' => 'beforeSend', |
| 60 | + 'before_send' => 'beforeSend', |
| 61 | + 'timeout' => 'timeout', |
26 | 62 | ]; |
27 | 63 |
|
28 | 64 | /** |
29 | 65 | * Options constructor. |
30 | 66 | * |
31 | | - * @param array $options |
| 67 | + * @param array $options Associative array of options to initialize. |
32 | 68 | */ |
33 | 69 | public function __construct(array $options = []) |
34 | 70 | { |
35 | | - $this->options = array_merge($this->options, $options); |
| 71 | + foreach ($options as $key => $value) { |
| 72 | + $normalizedKey = self::OPTION_KEYS[$key] ?? null; |
| 73 | + |
| 74 | + if ($normalizedKey === null) { |
| 75 | + throw new \InvalidArgumentException("Unknown option: $key"); |
| 76 | + } |
| 77 | + |
| 78 | + $this->setOption($normalizedKey, $value); |
| 79 | + } |
36 | 80 | } |
37 | 81 |
|
38 | 82 | /** |
39 | | - * Returns access token. It is available on projects settings |
| 83 | + * Set a class property based on the normalized option key. |
40 | 84 | * |
41 | | - * @return string |
| 85 | + * @param string $key |
| 86 | + * @param mixed $value |
42 | 87 | */ |
43 | | - public function getIntegrationToken(): string |
| 88 | + private function setOption(string $key, $value): void |
44 | 89 | { |
45 | | - return $this->options['integrationToken']; |
| 90 | + switch ($key) { |
| 91 | + case 'integrationToken': |
| 92 | + case 'release': |
| 93 | + case 'url': |
| 94 | + if (!is_string($value)) { |
| 95 | + throw new \InvalidArgumentException("Option '$key' must be a string."); |
| 96 | + } |
| 97 | + $this->$key = $value; |
| 98 | + |
| 99 | + break; |
| 100 | + |
| 101 | + case 'errorTypes': |
| 102 | + if (!is_int($value) && $value !== null) { |
| 103 | + throw new \InvalidArgumentException("Option 'errorTypes' must be an integer or null."); |
| 104 | + } |
| 105 | + $this->errorTypes = $value; |
| 106 | + |
| 107 | + break; |
| 108 | + |
| 109 | + case 'captureSilencedErrors': |
| 110 | + if (!is_bool($value)) { |
| 111 | + throw new \InvalidArgumentException("Option 'captureSilencedErrors' must be a boolean."); |
| 112 | + } |
| 113 | + $this->captureSilencedErrors = $value; |
| 114 | + |
| 115 | + break; |
| 116 | + |
| 117 | + case 'beforeSend': |
| 118 | + if (!is_callable($value) && $value !== null) { |
| 119 | + throw new \InvalidArgumentException("Option 'beforeSend' must be callable or null."); |
| 120 | + } |
| 121 | + $this->beforeSend = $value; |
| 122 | + |
| 123 | + break; |
| 124 | + |
| 125 | + case 'timeout': |
| 126 | + if (!is_int($value)) { |
| 127 | + throw new \InvalidArgumentException("Option 'timeout' must be an integer."); |
| 128 | + } |
| 129 | + $this->timeout = $value; |
| 130 | + |
| 131 | + break; |
| 132 | + |
| 133 | + default: |
| 134 | + throw new \InvalidArgumentException("Unknown option '$key'."); |
| 135 | + } |
46 | 136 | } |
47 | 137 |
|
48 | | - /** |
49 | | - * Returns URL that should be send |
50 | | - * |
51 | | - * @return string |
52 | | - */ |
53 | | - public function getUrl(): string |
| 138 | + public function getIntegrationToken(): string |
54 | 139 | { |
55 | | - return $this->options['url']; |
| 140 | + return $this->integrationToken; |
56 | 141 | } |
57 | 142 |
|
58 | | - public function getTimeout(): int |
| 143 | + public function getUrl(): string |
59 | 144 | { |
60 | | - return $this->options['timeout']; |
| 145 | + return $this->url; |
61 | 146 | } |
62 | 147 |
|
63 | | - /** |
64 | | - * Returns application release |
65 | | - * |
66 | | - * @return string |
67 | | - */ |
68 | 148 | public function getRelease(): string |
69 | 149 | { |
70 | | - return $this->options['release']; |
| 150 | + return $this->release; |
71 | 151 | } |
72 | 152 |
|
73 | | - /** |
74 | | - * Returns error types |
75 | | - * |
76 | | - * @return int |
77 | | - */ |
78 | 153 | public function getErrorTypes(): int |
79 | 154 | { |
80 | | - return $this->options['error_types']; |
| 155 | + return $this->errorTypes ?? error_reporting(); |
| 156 | + } |
| 157 | + |
| 158 | + public function shouldCaptureSilencedErrors(): bool |
| 159 | + { |
| 160 | + return $this->captureSilencedErrors; |
81 | 161 | } |
82 | 162 |
|
83 | | - /** |
84 | | - * Returns before send callback |
85 | | - * |
86 | | - * @return callable|null |
87 | | - */ |
88 | 163 | public function getBeforeSend(): ?callable |
89 | 164 | { |
90 | | - return $this->options['beforeSend']; |
| 165 | + return $this->beforeSend; |
| 166 | + } |
| 167 | + |
| 168 | + public function getTimeout(): int |
| 169 | + { |
| 170 | + return $this->timeout; |
91 | 171 | } |
92 | 172 | } |
0 commit comments