-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added event logging in database (#8)
* Added event logging in database * feedback * fixed table for magento webhooks * code review feedback * bumped phpunit version
- Loading branch information
1 parent
e72545f
commit 50c4ac8
Showing
17 changed files
with
343 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Upgrade guide | ||
|
||
## 1.x to 2.x | ||
|
||
2.x introduces the use of a database to log files | ||
|
||
### Database usage | ||
|
||
A migration has been added to this package to keep historical data of all the event webhooks. This means that using a database has become mandatory. | ||
|
||
### Scheduling clean up | ||
|
||
In order to keep the logs from bloating up disk usage, we've added a `CleanLogsCommand` which defaults to a month if no parameters have been passed. | ||
Add this to your scheduler. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
database/migrations/2024_01_24_153000_create_event_log_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::create('magento_webhooks_event_logs', function (Blueprint $table): void { | ||
$table->id(); | ||
$table->string('event')->index(); | ||
$table->json('data')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists('magento_webhooks_event_logs'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Tests"> | ||
<directory>./tests/*</directory> | ||
</testsuite> | ||
</testsuites> | ||
<coverage> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</coverage> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true"> | ||
<testsuites> | ||
<testsuite name="Tests"> | ||
<directory>./tests/*</directory> | ||
</testsuite> | ||
</testsuites> | ||
<source> | ||
<include> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</source> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace JustBetter\MagentoWebhooks\Actions; | ||
|
||
use Illuminate\Support\Carbon; | ||
use JustBetter\MagentoWebhooks\Contracts\CleansLogs; | ||
use JustBetter\MagentoWebhooks\Models\EventLog; | ||
|
||
class CleanLogs implements CleansLogs | ||
{ | ||
public function clean(Carbon $date): void | ||
{ | ||
EventLog::query() | ||
->where('created_at', '<', $date) | ||
->delete(); | ||
} | ||
|
||
public static function bind(): void | ||
{ | ||
app()->singleton(CleansLogs::class, static::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace JustBetter\MagentoWebhooks\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Carbon; | ||
use JustBetter\MagentoWebhooks\Jobs\CleanLogsJob; | ||
|
||
class CleanLogsCommand extends Command | ||
{ | ||
protected $signature = 'magento:webhooks:clean-logs {--date=}'; | ||
|
||
protected $description = 'Cleans event logs based on date'; | ||
|
||
public function handle(): int | ||
{ | ||
/** @var ?string $date */ | ||
$date = $this->option('date'); | ||
|
||
$date = $date !== null | ||
? Carbon::parse($date) | ||
: now()->subMonth(); | ||
|
||
CleanLogsJob::dispatch($date); | ||
|
||
return static::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace JustBetter\MagentoWebhooks\Contracts; | ||
|
||
use Illuminate\Support\Carbon; | ||
|
||
interface CleansLogs | ||
{ | ||
public function clean(Carbon $date): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace JustBetter\MagentoWebhooks\Jobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldBeUnique; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Support\Carbon; | ||
use JustBetter\MagentoWebhooks\Contracts\CleansLogs; | ||
|
||
class CleanLogsJob implements ShouldBeUnique, ShouldQueue | ||
{ | ||
use Dispatchable; | ||
use InteractsWithQueue; | ||
use Queueable; | ||
|
||
public function __construct( | ||
protected Carbon $date, | ||
) { | ||
$this->onQueue(config('magento-webhooks.queue')); | ||
} | ||
|
||
public function handle(CleansLogs $contract): void | ||
{ | ||
$contract->clean($this->date); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace JustBetter\MagentoWebhooks\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Carbon; | ||
|
||
/** | ||
* @property int $id | ||
* @property string $event | ||
* @property ?array $data | ||
* @property ?Carbon $created_at | ||
* @property ?Carbon $updated_at | ||
*/ | ||
class EventLog extends Model | ||
{ | ||
protected $table = 'magento_webhooks_event_logs'; | ||
|
||
protected $guarded = []; | ||
|
||
protected $casts = [ | ||
'data' => 'array', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace JustBetter\MagentoWebhooks\Tests\Actions; | ||
|
||
use JustBetter\MagentoWebhooks\Actions\CleanLogs; | ||
use JustBetter\MagentoWebhooks\Models\EventLog; | ||
use JustBetter\MagentoWebhooks\Tests\TestCase; | ||
|
||
class CleanLogsTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_cleans_logs(): void | ||
{ | ||
EventLog::query()->create([ | ||
'event' => 'some-event', | ||
'data' => ['some' => 'value'], | ||
'created_at' => now()->subMonths(2), | ||
]); | ||
|
||
EventLog::query()->create([ | ||
'event' => 'some-event', | ||
'data' => ['some' => 'value'], | ||
'created_at' => now(), | ||
]); | ||
|
||
/** @var CleanLogs $action */ | ||
$action = app(CleanLogs::class); | ||
$action->clean(now()->subMonth()); | ||
|
||
$this->assertEquals(1, EventLog::query()->count()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.