1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Honeybadger; |
4
|
|
|
|
5
|
|
|
use Throwable; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Honeybadger\Concerns\Newable; |
8
|
|
|
use Honeybadger\Contracts\Reporter; |
9
|
|
|
use Honeybadger\Support\Repository; |
10
|
|
|
use Honeybadger\Handlers\ErrorHandler; |
11
|
|
|
use Honeybadger\Handlers\ExceptionHandler; |
12
|
|
|
use Honeybadger\Exceptions\ServiceException; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request as FoundationRequest; |
14
|
|
|
|
15
|
|
|
class Honeybadger implements Reporter |
16
|
|
|
{ |
17
|
|
|
use Newable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* SDK Version. |
21
|
|
|
*/ |
22
|
|
|
const VERSION = '1.3.0'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Honeybadger API URL. |
26
|
|
|
*/ |
27
|
|
|
const API_URL = 'https://api.honeybadger.io/v1/'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Honeybadger\HoneybadgerClient; |
31
|
|
|
*/ |
32
|
|
|
protected $client; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \Honeybadger\Config |
36
|
|
|
*/ |
37
|
|
|
protected $config; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \Honeybadger\Support\Repository |
41
|
|
|
*/ |
42
|
|
|
protected $context; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $config |
46
|
|
|
* @param \GuzzleHttp\Client $client |
47
|
|
|
*/ |
48
|
|
|
public function __construct(array $config = [], Client $client = null) |
49
|
|
|
{ |
50
|
|
|
$this->config = new Config($config); |
51
|
|
|
|
52
|
|
|
$this->client = new HoneybadgerClient($this->config, $client); |
53
|
|
|
$this->context = new Repository; |
54
|
|
|
|
55
|
|
|
$this->setHandlers(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function notify(Throwable $throwable, FoundationRequest $request = null) : array |
62
|
|
|
{ |
63
|
|
|
if (! $this->shouldReport($throwable)) { |
64
|
|
|
return []; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$notification = (new ExceptionNotification($this->config, $this->context)) |
68
|
|
|
->make($throwable, $request); |
69
|
|
|
|
70
|
|
|
return $this->client->notification($notification); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function customNotification(array $payload) : array |
77
|
|
|
{ |
78
|
|
|
if (is_null($this->config['api_key'])) { |
79
|
|
|
return []; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$notification = (new CustomNotification($this->config, $this->context)) |
83
|
|
|
->make($payload); |
84
|
|
|
|
85
|
|
|
return $this->client->notification($notification); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function rawNotification(callable $callable) : array |
92
|
|
|
{ |
93
|
|
|
if (is_null($this->config['api_key'])) { |
94
|
|
|
return []; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$notification = (new RawNotification($this->config, $this->context)) |
98
|
|
|
->make($callable($this->config, $this->context)); |
99
|
|
|
|
100
|
|
|
return $this->client->notification($notification); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function checkin(string $key) : void |
107
|
|
|
{ |
108
|
|
|
$this->client->checkin($key); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param int|string $key |
113
|
|
|
* @param int|string $value |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
public function context($key, $value) : void |
117
|
|
|
{ |
118
|
|
|
$this->context->set($key, $value); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
private function setHandlers() : void |
125
|
|
|
{ |
126
|
|
|
if ($this->config['handlers']['exception']) { |
127
|
|
|
(new ExceptionHandler($this))->register(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($this->config['handlers']['error']) { |
131
|
|
|
(new ErrorHandler($this))->register(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param \Throwable $throwable |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
private function excludedException(Throwable $throwable) : bool |
140
|
|
|
{ |
141
|
|
|
return $throwable instanceof ServiceException |
142
|
|
|
|| in_array( |
143
|
|
|
get_class($throwable), |
144
|
|
|
$this->config['excluded_exceptions'] |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param \Throwable $throwable |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
private function shouldReport(Throwable $throwable) : bool |
153
|
|
|
{ |
154
|
|
|
return ! $this->excludedException($throwable) && ! is_null($this->config['api_key']); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|