mirror of
https://github.com/CachetHQ/Cachet.git
synced 2025-01-17 21:49:01 +01:00
Flesh out the WebHook stuff, add fire and ping methods
This commit is contained in:
parent
3e22652a9b
commit
dd7f99db3a
@ -1,7 +1,110 @@
|
||||
<?php
|
||||
|
||||
class WebHook extends Eloquent {
|
||||
// Request Methods.
|
||||
const HEAD = 0;
|
||||
const GET = 1;
|
||||
const POST = 2;
|
||||
const PATCH = 3;
|
||||
const PUT = 4;
|
||||
const DELETE = 5;
|
||||
|
||||
/**
|
||||
* Returns all responses for a WebHook.
|
||||
* @return Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function response() {
|
||||
return $this->hasMany('WebHookContent', 'hook_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all active hooks.
|
||||
* @param Illuminate\Database\Eloquent\Builder $query
|
||||
* @return Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeActive($query) {
|
||||
return $query->where('active', 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setups a Ping event that is fired upon a web hook.
|
||||
* @return array result of the ping
|
||||
*/
|
||||
public function ping() {
|
||||
return $this->fire('ping', 'Coming live to you from Cachet.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires the actual web hook event.
|
||||
* @param string $eventType the event to send X-Cachet-Event
|
||||
* @param mixed $data Data to send to the Web Hook
|
||||
* @return object
|
||||
*/
|
||||
public function fire($eventType, $data = null) {
|
||||
$startTime = microtime(true);
|
||||
|
||||
$request = $this->client->createRequest($this->requestMethod, $this->endpoint, [
|
||||
'headers' => [
|
||||
'X-Cachet-Event' => $eventType,
|
||||
],
|
||||
'body' => $data
|
||||
]);
|
||||
|
||||
try {
|
||||
$response = $this->client->send($request);
|
||||
} catch (\GuzzleHttp\Exception\ClientException $e) {
|
||||
// Do nothing with the exception, we want it.
|
||||
$response = $e->getResponse();
|
||||
}
|
||||
|
||||
$timeTaken = microtime(TRUE) - $startTime;
|
||||
|
||||
// Store the request
|
||||
$hookResponse = new WebHookResponse;
|
||||
$hookResponse->web_hook_id = $this->id;
|
||||
$hookResponse->response_code = $response->getStatusCode();
|
||||
$hookResponse->sent_headers = json_encode($request->getHeaders());
|
||||
$hookResponse->sent_body = json_encode($data);
|
||||
$hookResponse->recv_headers = json_encode($response->getHeaders());
|
||||
$hookResponse->recv_body = json_encode($response->getBody());
|
||||
$hookResponse->time_taken = $timeTaken;
|
||||
$hookResponse->save();
|
||||
|
||||
return $hookResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a human readable request type name.
|
||||
* @return string HEAD, GET, POST, DELETE, PATCH, PUT etc
|
||||
*/
|
||||
public function getRequestMethodAttribute() {
|
||||
$requestMethod = NULL;
|
||||
|
||||
switch ($this->request_type) {
|
||||
case self::HEAD:
|
||||
$requestMethod = 'HEAD';
|
||||
break;
|
||||
case self::GET:
|
||||
$requestMethod = 'GET';
|
||||
break;
|
||||
case self::POST:
|
||||
$requestMethod = 'POST';
|
||||
break;
|
||||
case self::PATCH:
|
||||
$requestMethod = 'PATCH';
|
||||
break;
|
||||
case self::PUT:
|
||||
$requestMethod = 'PUT';
|
||||
break;
|
||||
case self::DELETE:
|
||||
$requestMethod = 'DELETE';
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Exception('Unknown request type value: ' . $this->request_type);
|
||||
break;
|
||||
}
|
||||
|
||||
return $requestMethod;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user