Merge pull request #294 from cachethq/redo-migrations
[BACKWARDS INCOMPATIBLE] Redo migrations
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateWebHooksTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('web_hooks', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name')->nullable(false);
|
||||
$table->string('endpoint')->nullable(false);
|
||||
$table->tinyInteger('hook_type')->default(0); // When should this web hook be called?
|
||||
$table->tinyInteger('request_type')->default(0); // ENUM: GET, POST, PUT, DELETE etc
|
||||
$table->boolean('active')->default(0);
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
|
||||
// Indexes
|
||||
$table->index('active');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::drop('web_hooks');
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateWebHookResponsesTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('web_hook_response', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->integer('hook_id')->unsigned();
|
||||
$table->tinyInteger('response_code')->nullable(false); // This should return something like 200, 301 etc.
|
||||
$table->string('response_type'); // application/json etc.
|
||||
$table->text('sent_headers');
|
||||
$table->longText('sent_body'); // What we sent the web hook
|
||||
$table->text('recv_headers');
|
||||
$table->longText('recv_body'); // Potentially a big response will be returned
|
||||
$table->float('time_taken')->default(0); // How long did the request take, in seconds.
|
||||
$table->timestamps();
|
||||
|
||||
$table->index('hook_id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::drop('web_hook_response');
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class UserIdColumnForComponents extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('components', function (Blueprint $table) {
|
||||
$table->unsignedInteger('user_id')->nullable();
|
||||
|
||||
$table->foreign('user_id')->references('id')->on('users')->onDelete('SET NULL')->onUpdate('NO ACTION');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('components', function (Blueprint $table) {
|
||||
$table->dropForeign('components_user_id_foreign');
|
||||
$table->dropColumn('user_id');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class UserIdColumnForIncidents extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('incidents', function (Blueprint $table) {
|
||||
$table->unsignedInteger('user_id')->nullable();
|
||||
|
||||
$table->foreign('user_id')->references('id')->on('users')->onDelete('SET NULL')->onUpdate('NO ACTION');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('incidents', function (Blueprint $table) {
|
||||
$table->dropForeign('incidents_user_id_foreign');
|
||||
$table->dropColumn('user_id');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AlterTableIncidentsRenameComponentColumn extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('incidents', function (Blueprint $table) {
|
||||
$table->renameColumn('component', 'component_id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('incidents', function (Blueprint $table) {
|
||||
$table->renameColumn('component_id', 'component');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AlterTableIncidentsRemoveDefaultComponent extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('incidents', function (Blueprint $table) {
|
||||
if (Config::get('database')['default'] === 'mysql') {
|
||||
DB::statement("ALTER TABLE incidents CHANGE component_id component_id TINYINT(4) NOT NULL DEFAULT '0';");
|
||||
} elseif (Config::get('database')['default'] === 'pgsql') {
|
||||
DB::statement("ALTER TABLE incidents ALTER COLUMN component_id SET DEFAULT '0';");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('incidents', function (Blueprint $table) {
|
||||
if (Config::get('database')['default'] === 'mysql') {
|
||||
DB::statement("ALTER TABLE incidents CHANGE component_id component_id TINYINT(4) NOT NULL DEFAULT '1';");
|
||||
} elseif (Config::get('database')['default'] === 'pgsql') {
|
||||
DB::statement("ALTER TABLE incidents ALTER COLUMN component_id SET DEFAULT '1';");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateSessionTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('sessions', function (Blueprint $table) {
|
||||
$table->string('id')->unique();
|
||||
$table->text('payload');
|
||||
$table->integer('last_activity');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::drop('sessions');
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AlterTableComponentsAddDeletedAt extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('components', function (Blueprint $table) {
|
||||
$table->softDeletes();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AlterTableComponentsAddMetaColumns extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('components', function (Blueprint $table) {
|
||||
$table->text('tags')->nullable()->default(null)->after('description');
|
||||
$table->text('link')->nullable()->default(null)->after('description');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('components', function (Blueprint $table) {
|
||||
$table->dropColumn('tags');
|
||||
$table->dropColumn('link');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class AlterTableSettingsValueLongText extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
if (Config::get('database')['default'] === 'mysql') {
|
||||
DB::statement("ALTER TABLE settings CHANGE `value` `value` LONGTEXT;");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
if (Config::get('database')['default'] === 'mysql') {
|
||||
DB::statement("ALTER TABLE settings CHANGE `value` `value` TEXT;");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AlterTableComponentsAddOrderColumn extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('components', function (Blueprint $table) {
|
||||
$table->tinyInteger('order')->default(0)->after('status');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('components', function (Blueprint $table) {
|
||||
$table->dropColumn('order');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AlterTableUsersAddApiKey extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->string('api_key')->nullable()->default(null)->after('email');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->dropColumn('api_key');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AlterTableComponentsAddGroupIdColumn extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('components', function (Blueprint $table) {
|
||||
$table->integer('group_id')->nullable()->default(null)->after('order');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('components', function (Blueprint $table) {
|
||||
$table->dropColumn('group_id');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,6 @@ class CreateComponentGroupsTable extends Migration
|
||||
$table->increments('id');
|
||||
$table->string('name');
|
||||
$table->timestamps();
|
||||
|
||||
$table->softDeletes();
|
||||
});
|
||||
}
|
||||
@@ -16,11 +16,20 @@ class CreateComponentsTable extends Migration
|
||||
Schema::create('components', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name');
|
||||
$table->string('description')->nullable()->default(null);
|
||||
$table->tinyInteger('status')->default(1);
|
||||
$table->text('description')->default('');
|
||||
$table->text('link')->default('');
|
||||
$table->text('tags')->default('');
|
||||
$table->integer('status');
|
||||
$table->integer('order');
|
||||
$table->integer('group_id');
|
||||
$table->integer('user_id');
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
|
||||
$table->index('group_id');
|
||||
$table->index('user_id');
|
||||
$table->index('status');
|
||||
$table->index('order');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -15,13 +15,11 @@ class CreateIncidentTemplatesTable extends Migration
|
||||
{
|
||||
Schema::create('incident_templates', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name')->nullable(false);
|
||||
$table->string('slug', 50)->nullable(false);
|
||||
$table->longText('template')->nullable(false);
|
||||
|
||||
$table->string('name');
|
||||
$table->string('slug');
|
||||
$table->longText('template');
|
||||
$table->timestamps();
|
||||
|
||||
$table->index('slug');
|
||||
$table->softDeletes();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -15,15 +15,17 @@ class CreateIncidentsTable extends Migration
|
||||
{
|
||||
Schema::create('incidents', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->tinyInteger('component')->default(1);
|
||||
$table->integer('component_id')->default(0);
|
||||
$table->string('name');
|
||||
$table->tinyInteger('status')->default(1);
|
||||
$table->longText('message');
|
||||
$table->integer('status');
|
||||
$table->longText('message')->default('');
|
||||
$table->integer('user_id');
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
|
||||
$table->index('component');
|
||||
$table->index('component_id');
|
||||
$table->index('status');
|
||||
$table->index('user_id');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -15,11 +15,11 @@ class CreateMetricPointsTable extends Migration
|
||||
{
|
||||
Schema::create('metric_points', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->integer('metric_id')->unsigned();
|
||||
$table->integer('value')->unsigned();
|
||||
$table->integer('metric_id');
|
||||
$table->integer('value');
|
||||
$table->timestamps();
|
||||
|
||||
$table->foreign('metric_id')->references('id')->on('metrics');
|
||||
$table->index('metric_id');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -15,11 +15,13 @@ class CreateMetricsTable extends Migration
|
||||
{
|
||||
Schema::create('metrics', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name')->nullable(false);
|
||||
$table->string('suffix')->nullable(false);
|
||||
$table->string('description')->nullable(false);
|
||||
$table->boolean('display_chart')->default(false);
|
||||
$table->string('name');
|
||||
$table->string('suffix');
|
||||
$table->text('description')->default('');
|
||||
$table->boolean('display_chart')->default(1);
|
||||
$table->timestamps();
|
||||
|
||||
$table->index('display_chart');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -15,12 +15,12 @@ class CreateServicesTable extends Migration
|
||||
{
|
||||
Schema::create('services', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
|
||||
$table->string('type')->nullable(false);
|
||||
$table->boolean('active')->default(0); // Inactive by default
|
||||
$table->text('properties')->nullable(true);
|
||||
|
||||
$table->string('type');
|
||||
$table->boolean('active');
|
||||
$table->text('properties');
|
||||
$table->timestamps();
|
||||
|
||||
$table->index('active');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ class CreateSettingsTable extends Migration
|
||||
Schema::create('settings', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name');
|
||||
$table->text('value')->nullable()->default(null);
|
||||
$table->longText('value');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
@@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateSubscribersTable extends Migration
|
||||
class CreateSubscriptionsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
@@ -13,9 +13,9 @@ class CreateSubscribersTable extends Migration
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('subscribers', function (Blueprint $table) {
|
||||
Schema::create('subscriptions', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('email')->nullable(false);
|
||||
$table->string('email');
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
});
|
||||
@@ -28,6 +28,6 @@ class CreateSubscribersTable extends Migration
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::drop('subscribers');
|
||||
Schema::drop('subscriptions');
|
||||
}
|
||||
}
|
||||
@@ -15,16 +15,20 @@ class CreateUsersTable extends Migration
|
||||
{
|
||||
Schema::create('users', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('username', 200)->nullable(false);
|
||||
$table->string('password')->nullable(false);
|
||||
$table->string('username');
|
||||
$table->string('password');
|
||||
$table->rememberToken();
|
||||
$table->string('email')->nullable()->default(null);
|
||||
$table->string('email');
|
||||
$table->string('api_key');
|
||||
$table->boolean('active')->default(1);
|
||||
$table->tinyInteger('level')->default(2);
|
||||
$table->timestamps();
|
||||
|
||||
$table->index('remember_token');
|
||||
$table->index('active');
|
||||
$table->unique('username');
|
||||
$table->unique('api_key');
|
||||
$table->unique('email');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Models;
|
||||
|
||||
use Exception;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string $endpoint
|
||||
* @property int $hook_type
|
||||
* @property int $request_type
|
||||
* @property int $active
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
* @property \Carbon\Carbon $deleted_at
|
||||
*/
|
||||
class WebHook extends Model
|
||||
{
|
||||
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\Relations\HasMany
|
||||
*/
|
||||
public function response()
|
||||
{
|
||||
return $this->hasMany('CachetHQ\Cachet\Models\WebHookContent', 'hook_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all active hooks.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function scopeActive(Builder $query)
|
||||
{
|
||||
return $query->where('active', 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setups a ping event that is fired upon a web hook.
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\WebHookResponse
|
||||
*/
|
||||
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 string $data Data to send to the Web Hook
|
||||
*
|
||||
* @return \CachetHQ\Cachet\Models\WebHookResponse
|
||||
*/
|
||||
public function fire($eventType, $data)
|
||||
{
|
||||
$startTime = microtime(true);
|
||||
|
||||
$client = new Client();
|
||||
$request = $client->createRequest($this->requestMethod, $this->endpoint, [
|
||||
'headers' => [
|
||||
'X-Cachet-Event' => $eventType,
|
||||
],
|
||||
'body' => $data
|
||||
]);
|
||||
|
||||
try {
|
||||
$response = $client->send($request);
|
||||
} catch (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.
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getRequestMethodAttribute()
|
||||
{
|
||||
switch ($this->request_type) {
|
||||
case self::HEAD:
|
||||
return 'HEAD';
|
||||
case self::GET:
|
||||
return 'GET';
|
||||
case self::POST:
|
||||
return 'POST';
|
||||
case self::PATCH:
|
||||
return 'PATCH';
|
||||
case self::PUT:
|
||||
return 'PUT';
|
||||
case self::DELETE:
|
||||
return 'DELETE';
|
||||
default:
|
||||
throw new Exception('Unknown request type value: '.$this->request_type);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $hook_id
|
||||
* @property int $response_code
|
||||
* @property string $sent_headers
|
||||
* @property string $sent_body
|
||||
* @property string $recv_headers
|
||||
* @property string $recv_body
|
||||
* @property float $time_taken
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
*/
|
||||
class WebHookResponse extends Model
|
||||
{
|
||||
/**
|
||||
* Returns the hook that a response belongs to.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function hook()
|
||||
{
|
||||
return $this->belongsTo('CachetHQ\Cachet\Models\WebHook', 'id', 'hook_id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user