This repository has been archived by the owner on Nov 20, 2023. It is now read-only.
forked from dator-zz/HoptoadBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirbrakeApi.php
193 lines (162 loc) · 5.88 KB
/
AirbrakeApi.php
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
185
186
187
188
189
190
191
192
193
<?php
namespace Airbrake\AirbrakeBundle;
use Symfony\Component\EventDispatcher\Event;
/**
* Airbrake API
*
*
* @author Clément JOBEILI ([email protected])
* @author Rich Cavanaugh (no@email)
* @author Till Klampaeckel ([email protected])
*
* @see Zend\Http\Client
*/
class AirbrakeApi
{
const NOTIFIER_NAME = 'AirbrakeBundle';
const NOTIFIER_VERSION = '2.0';
const NOTIFIER_URL = 'http://github.com/realestateconz/AirbrakeBundle';
const NOTIFIER_API_VERSION = '0.1';
const EXCEPTION_SSL = '403';
const EXCEPTION_NOTICE = '422';
const EXCEPTION_ERROR = '500';
/**
* @var Symfony\Component\EventDispatcher\Event $event the event dispatched by Symfony
*/
public $event;
/**
* @var array $options the options array (key, env)
*/
public $options;
protected $clients = array('curl', 'zend', 'pear');
protected $defaults = array(
'client' => 'curl',
'env' => 'dev'
);
public function __construct(array $parameters)
{
$this->options = array_merge($this->defaults, $parameters);
}
public function setEvent(Event $event)
{
$this->event = $event;
}
public function getEvent()
{
return $this->event;
}
public function getClient()
{
$client = $this->options['client'];
if(in_array(strtolower($client), $this->clients)){
$class = 'Airbrake\\AirbrakeBundle\\Client\\'. ucfirst($client);
return new $class;
}else{
throw new \Exception(sprintf('The client %s is not supported by AirbrakeBundle ', $client));
}
}
/**
* Notify the error to airbrake
*
* Raise 4 exceptions:
* - if Zend2.0 is not in the autoload
* - if the response status is 403
* - if the response status is 422
* - if the response status is 500
*
*/
public function notify()
{
$url = "http://airbrakeapp.com/notifier_api/v2/notices";
$headers = array(
'Accept' => 'text/xml, application/xml',
'Content-Type' => 'text/xml'
);
$body = $this->build();
$client = $this->getClient();
$client->setUrl($url);
$client->setHeaders($headers);
$client->setBody($body);
$response = $client->send();
if($response == self::EXCEPTION_SSL){
throw new \Exception('The requested project does not support SSL');
}else if($response == self::EXCEPTION_NOTICE){
throw new \Exception('The submitted notice was invalid - check the xml and ensure the API key is correct');
}else if($response == self::EXCEPTION_ERROR){
throw new \Exception('Unexpected errors - submit a bug report at http://help.airbrakeapp.com');
}
return $response;
}
/**
* Build the xml notice
*/
protected function build()
{
$event = $this->getEvent();
$exception = $this->getEvent()->getException();
$request = $this->getEvent()->getRequest();
$parameters = $request->attributes;
$doc = new \SimpleXMLElement('<notice />');
$doc->addAttribute('version', self::NOTIFIER_API_VERSION);
$doc->addChild('api-key', $this->options['key']);
$notifier = $doc->addChild('notifier');
$notifier->addChild('name', self::NOTIFIER_NAME);
$notifier->addChild('version', self::NOTIFIER_VERSION);
$notifier->addChild('url', self::NOTIFIER_URL);
$error = $doc->addChild('error');
$error->addChild('class', get_class($exception));
$error->addChild('message', $exception->getMessage());
$this->addXmlBacktrace($error, $exception);
$env = $doc->addChild('server-environment');
$env->addChild('project-root', $request->server->get('DOCUMENT_ROOT'));
$env->addChild('environment-name', $this->options['env']);
$rq = $doc->addChild('request');
$rq->addChild('url', $request->getRequestUri());
$rq->addChild('component', '');
$rq->addChild('action', $request->attributes->get('_controller'));
$this->addXmlVars($rq, 'params', $request->query->all());
$this->addXmlVars($rq, 'session', $request->getSession()->getAttributes());
$this->addXmlVars($rq, 'cgi-data', $request->server->all());
return $doc->asXML();
}
/**
* Format an array in XML
*/
protected function addXmlVars($parent, $key, $source)
{
if (empty($source)) return;
$node = $parent->addChild($key);
foreach ($source as $key => $val) {
if (is_array($val)) {
$val = str_replace("\n", ' ', print_r($val, true));
}
if (is_array($key)) {
$key = str_replace("\n", ' ', print_r($key, true));
}
$var_node = $node->addChild('var', $val);
$var_node->addAttribute('key', $key);
}
}
/**
* Format the exception stacktrace in XML
*/
protected function addXmlBacktrace($parent, $exception)
{
$backtrace = $parent->addChild('backtrace');
foreach ($exception->getTrace() as $entry) {
$line_node = $backtrace->addChild('line');
if (!isset($entry['file'])) {
$entry['file'] = $entry['class'];
}
if (!isset($entry['line'])) {
$entry['line'] = '?';
}
if (!isset($entry['function'])) {
$entry['function'] = '?';
}
$line_node->addAttribute('file', $entry['file']);
$line_node->addAttribute('number', $entry['line']);
$line_node->addAttribute('method', $entry['function']);
}
}
}