1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Honeybadger; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Honeybadger\Exceptions\ServiceException; |
8
|
|
|
use Honeybadger\Exceptions\ServiceExceptionFactory; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
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 array |
36
|
|
|
* |
37
|
|
|
* @throws \Honeybadger\Exceptions\ServiceException |
38
|
|
|
*/ |
39
|
|
|
public function notification(array $notification): array |
40
|
|
|
{ |
41
|
|
|
try { |
42
|
|
|
$response = $this->client->post( |
43
|
|
|
'notices', |
44
|
|
|
['body' => json_encode($notification, JSON_PARTIAL_OUTPUT_ON_ERROR)] |
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
|
|
|
return (string) $response->getBody() |
55
|
|
|
? json_decode($response->getBody(), true) |
56
|
|
|
: []; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $key |
61
|
|
|
* @return void |
62
|
|
|
* |
63
|
|
|
* @throws \Honeybadger\Exceptions\ServiceException |
64
|
|
|
*/ |
65
|
|
|
public function checkin(string $key): void |
66
|
|
|
{ |
67
|
|
|
try { |
68
|
|
|
$response = $this->client->head(sprintf('check_in/%s', $key)); |
69
|
|
|
} catch (Exception $e) { |
70
|
|
|
throw ServiceException::generic(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($response->getStatusCode() !== Response::HTTP_OK) { |
74
|
|
|
throw (new ServiceExceptionFactory($response))->make(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return \GuzzleHttp\Client |
80
|
|
|
*/ |
81
|
|
|
private function makeClient(): Client |
82
|
|
|
{ |
83
|
|
|
return new Client([ |
84
|
|
|
'base_uri' => Honeybadger::API_URL, |
85
|
|
|
'http_errors' => false, |
86
|
|
|
'headers' => [ |
87
|
|
|
'X-API-Key' => $this->config['api_key'], |
88
|
|
|
'Accept' => 'application/json', |
89
|
|
|
'Content-Type' => 'application/json', |
90
|
|
|
], |
91
|
|
|
'timeout' => $this->config['client']['timeout'], |
92
|
|
|
'proxy' => $this->config['client']['proxy'], |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|