Skip to content

Commit

Permalink
Feature/fixes by pionect (#40)
Browse files Browse the repository at this point in the history
* Check if migration ran

* remove dinamic migration dates

* tenant_id unsigned

* simplify getWebhooks

* add missing : to eloquent events in README

* add properties to webhook

* use AddWebhook::class in stead of class string

* remove hook.* aliases to fix tests
  • Loading branch information
dducro authored and mpociot committed Dec 8, 2016
1 parent 0e3f90e commit fd26ae8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ be affiliated with the originating application.

```bash
php artisan hook:add http://www.myapp.com/hooks/ '\App\Events\PodcastWasPurchased'
php artisan hook:add http://www.myapp.com/hooks/ 'eloquent.saved \App\User'
php artisan hook:add http://www.myapp.com/hooks/ 'eloquent.saved: \App\User'
```

```php
Expand Down Expand Up @@ -88,7 +88,7 @@ Let's say you want to have a webhook that gets called every time your User model

The event that gets called from Laravel will be:

`eloquent.updated \App\User`
`eloquent.updated: \App\User`

So this will be the event you want to listen for.

Expand All @@ -103,7 +103,7 @@ This command takes two arguments:
- The event name. This could either be one of the `eloquent.*` events, or one of your custom events.

```bash
php artisan hook:add http://www.myapp.com/hook/ 'eloquent.saved \App\User'
php artisan hook:add http://www.myapp.com/hook/ 'eloquent.saved: \App\User'
```

You can also add multiple webhooks for the same event, as all configured webhooks will get called asynchronously.
Expand Down
35 changes: 14 additions & 21 deletions src/Mpociot/CaptainHook/CaptainHookServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use Mpociot\CaptainHook\Commands\AddWebhook;
use Illuminate\Foundation\Bus\DispatchesJobs;
Expand Down Expand Up @@ -81,10 +82,13 @@ public function register()
protected function publishMigration()
{
$migrations = [
__DIR__.'/../../database/2015_10_29_000000_captain_hook_setup_table.php' => database_path('/migrations/'.date('Y_m_d').'_000000_captain_hook_setup_table.php'),
__DIR__.'/../../database/2015_10_29_000001_captain_hook_setup_logs_table.php' => database_path('/migrations/'.date('Y_m_d').'_000001_captain_hook_setup_logs_table.php'),
__DIR__.'/../../database/2015_10_29_000000_captain_hook_setup_table.php' =>
database_path('/migrations/2015_10_29_000000_captain_hook_setup_table.php'),
__DIR__.'/../../database/2015_10_29_000001_captain_hook_setup_logs_table.php' =>
database_path('/migrations/2015_10_29_000001_captain_hook_setup_logs_table.php'),
];

// To be backwards compatible
foreach ($migrations as $migration => $toPath) {
preg_match('/_captain_hook_.*\.php/', $migration, $match);
$published_migration = glob(database_path('/migrations/*'.$match[0]));
Expand Down Expand Up @@ -152,13 +156,14 @@ public function setWebhooks($webhooks)
*/
public function getWebhooks()
{
if (! $this->getCache()->has(Webhook::CACHE_KEY)) {
$this->getCache()->rememberForever(Webhook::CACHE_KEY, function () {
// Check if migration ran
if (Schema::hasTable((new Webhook)->getTable())) {
return collect($this->getCache()->rememberForever(Webhook::CACHE_KEY, function () {
return Webhook::all();
});
}));
}

return collect($this->getCache()->get(Webhook::CACHE_KEY));
return collect();
}

/**
Expand Down Expand Up @@ -214,22 +219,10 @@ public function handleEvent($eventData)
*/
protected function registerCommands()
{
$this->app['hook.list'] = $this->app->share(function () {
return new ListWebhooks();
});

$this->app['hook.add'] = $this->app->share(function () {
return new AddWebhook();
});

$this->app['hook.delete'] = $this->app->share(function () {
return new DeleteWebhook();
});

$this->commands(
'hook.list',
'hook.add',
'hook.delete'
ListWebhooks::class,
AddWebhook::class,
DeleteWebhook::class
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Mpociot/CaptainHook/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
/**
* This file is part of CaptainHook arrrrr.
*
* @property integer id
* @property integer tenant_id
* @property string event
* @property string url
* @license MIT
*/
class Webhook extends Eloquent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function up()
{
Schema::create('webhooks', function (Blueprint $table) {
$table->increments('id');
$table->integer('tenant_id')->nullable();
$table->integer('tenant_id')->unsigned()->nullable();
$table->string('url');
$table->string('event');
$table->timestamps();
Expand Down
15 changes: 9 additions & 6 deletions tests/CommandsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

use Mockery as m;
use Mpociot\CaptainHook\Commands\AddWebhook;
use Mpociot\CaptainHook\Commands\DeleteWebhook;
use Mpociot\CaptainHook\Webhook;

class CommandsTest extends Orchestra\Testbench\TestCase
{
Expand All @@ -16,7 +19,7 @@ public function setUp()

public function testCannotAddWebhookWithoutName()
{
$cmd = m::mock('\\Mpociot\\CaptainHook\\Commands\\AddWebhook[argument,error]');
$cmd = m::mock(AddWebhook::class . '[argument,error]');

$cmd->shouldReceive('error')
->once()
Expand All @@ -36,7 +39,7 @@ public function testCannotAddWebhookWithoutName()

public function testCanAddWebhook()
{
$cmd = m::mock('\\Mpociot\\CaptainHook\\Commands\\AddWebhook[argument,info]');
$cmd = m::mock(AddWebhook::class . '[argument,info]');

$cmd->shouldReceive('argument')
->with('url')
Expand All @@ -59,11 +62,11 @@ public function testCanAddWebhook()

public function testCannotDeleteWebhookWithWrongID()
{
$webhook = \Mpociot\CaptainHook\Webhook::create([
Webhook::create([
'url' => 'http://foo.baz',
'event' => 'DeleteWebhook',
]);
$cmd = m::mock('\\Mpociot\\CaptainHook\\Commands\\DeleteWebhook[argument,error]');
$cmd = m::mock(DeleteWebhook::class . '[argument,error]');

$cmd->shouldReceive('argument')
->with('id')
Expand All @@ -82,11 +85,11 @@ public function testCannotDeleteWebhookWithWrongID()

public function testCanDeleteWebhook()
{
$webhook = \Mpociot\CaptainHook\Webhook::create([
$webhook = Webhook::create([
'url' => 'http://foo.baz',
'event' => 'DeleteWebhook',
]);
$cmd = m::mock('\\Mpociot\\CaptainHook\\Commands\\DeleteWebhook[argument,info]');
$cmd = m::mock(DeleteWebhook::class . '[argument,info]');

$cmd->shouldReceive('argument')
->with('id')
Expand Down

0 comments on commit fd26ae8

Please sign in to comment.