|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Honeybadger; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use Honeybadger\Exceptions\ServiceException; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
use Honeybadger\Exceptions\ServiceExceptionFactory; |
|
10
|
|
|
|
|
11
|
|
|
class HoneybadgerClient |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var \Honeybadger\Config |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $config; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \GuzzleHttp\Client |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $client; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param \Honeybadger\Config $config |
|
25
|
|
|
* @param \GuzzleHttp\Client $client |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(Config $config, Client $client = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->config = $config; |
|
30
|
|
|
$this->client = $client ?? $this->makeClient(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param array $notification |
|
35
|
|
|
* @return void |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \Honeybadger\Exceptions\ServiceException |
|
38
|
|
|
*/ |
|
39
|
|
|
public function notification($notification) : void |
|
40
|
|
|
{ |
|
41
|
|
|
try { |
|
42
|
|
|
$response = $this->client->post( |
|
43
|
|
|
'notices', |
|
44
|
|
|
['body' => json_encode($notification)] |
|
45
|
|
|
); |
|
46
|
|
|
} catch (Exception $e) { |
|
47
|
|
|
throw ServiceException::generic(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($response->getStatusCode() !== Response::HTTP_CREATED) { |
|
51
|
|
|
throw (new ServiceExceptionFactory($response))->make(); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $key |
|
57
|
|
|
* @return void |
|
58
|
|
|
* |
|
59
|
|
|
* @throws \Honeybadger\Exceptions\ServiceException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function checkin(string $key) : void |
|
62
|
|
|
{ |
|
63
|
|
|
try { |
|
64
|
|
|
$response = $this->client->head(sprintf('check_in/%s', $key)); |
|
65
|
|
|
} catch (Exception $e) { |
|
66
|
|
|
throw ServiceException::generic(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($response->getStatusCode() !== Response::HTTP_OK) { |
|
70
|
|
|
throw (new ServiceExceptionFactory($response))->make(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return \GuzzleHttp\Client |
|
76
|
|
|
*/ |
|
77
|
|
|
private function makeClient() : Client |
|
78
|
|
|
{ |
|
79
|
|
|
return new Client([ |
|
80
|
|
|
'base_uri' => Honeybadger::API_URL, |
|
81
|
|
|
'http_errors' => false, |
|
82
|
|
|
'headers' => [ |
|
83
|
|
|
'X-API-Key' => $this->config['api_key'], |
|
84
|
|
|
'Accept' => 'application/json', |
|
85
|
|
|
'Content-Type' => 'application/json', |
|
86
|
|
|
], |
|
87
|
|
|
'timeout' => $this->config['client']['timeout'], |
|
88
|
|
|
'proxy' => $this->config['client']['proxy'], |
|
89
|
|
|
]); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|