From 105bfb302ac026240eb0f5c9ec8ca828139999e5 Mon Sep 17 00:00:00 2001 From: Proxynth Date: Fri, 10 Jul 2026 12:50:40 +0200 Subject: [PATCH] refactor: dispatch domain events through event bus --- .../Http/Controllers/WebhookLogController.php | 4 + .../Laravel/Middleware/ValidateWebhook.php | 2 +- .../Laravel/Jobs/RetryWebhookJob.php | 5 +- .../Application/{ => Ports}/EventBus.php | 2 +- .../Laravel/EventBus/LaravelEventBus.php | 2 +- .../Providers/LarawebhookServiceProvider.php | 2 +- tests/ArchTest.php | 12 ++ tests/Fakes/Shared/FakeEventBus.php | 2 +- .../API/WebhookLogControllerTest.php | 28 ++++ .../ValidateWebhookMiddlewareTest.php | 134 +++++++++++++++++- tests/Unit/Jobs/RetryWebhookJobTest.php | 40 +++++- tests/Unit/LarawebhookServiceProviderTest.php | 2 +- 12 files changed, 223 insertions(+), 12 deletions(-) rename src/Shared/Application/{ => Ports}/EventBus.php (83%) diff --git a/src/Audit/Infrastructure/Laravel/Http/Controllers/WebhookLogController.php b/src/Audit/Infrastructure/Laravel/Http/Controllers/WebhookLogController.php index a07ed40..693649d 100644 --- a/src/Audit/Infrastructure/Laravel/Http/Controllers/WebhookLogController.php +++ b/src/Audit/Infrastructure/Laravel/Http/Controllers/WebhookLogController.php @@ -20,6 +20,7 @@ use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\Signature; use Proxynth\Larawebhook\Processing\Application\Commands\ReplayWebhookCommand; use Proxynth\Larawebhook\Processing\Application\UseCases\ReplayWebhook; +use Proxynth\Larawebhook\Shared\Application\Ports\EventBus; use Proxynth\Larawebhook\Shared\Domain\Enums\WebhookService; use RuntimeException; use Throwable; @@ -31,6 +32,7 @@ public function __construct( private readonly GetWebhookLogDetails $getWebhookLogDetails, private readonly ReplayWebhook $replayWebhook, private readonly WebhookSecretResolver $secretResolver, + private readonly EventBus $eventBus, ) {} public function index(Request $request): JsonResponse @@ -80,6 +82,8 @@ public function replay(WebhookLog $log): JsonResponse signature: $this->extractSignatureFromPayload($log), )); + $this->eventBus->dispatchMany($result->events); + return response()->json([ 'success' => $result->log->status === 'success', 'message' => $result->log->status === 'success' diff --git a/src/Ingestion/Infrastructure/Laravel/Middleware/ValidateWebhook.php b/src/Ingestion/Infrastructure/Laravel/Middleware/ValidateWebhook.php index 758e9ab..17836cc 100644 --- a/src/Ingestion/Infrastructure/Laravel/Middleware/ValidateWebhook.php +++ b/src/Ingestion/Infrastructure/Laravel/Middleware/ValidateWebhook.php @@ -13,7 +13,7 @@ use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\Signature; use Proxynth\Larawebhook\Processing\Application\Ports\RetryPolicyResolver; use Proxynth\Larawebhook\Processing\Infrastructure\Laravel\Jobs\RetryWebhookJob; -use Proxynth\Larawebhook\Shared\Application\EventBus; +use Proxynth\Larawebhook\Shared\Application\Ports\EventBus; use Proxynth\Larawebhook\Shared\Domain\Enums\WebhookService; use Symfony\Component\HttpFoundation\Response; diff --git a/src/Processing/Infrastructure/Laravel/Jobs/RetryWebhookJob.php b/src/Processing/Infrastructure/Laravel/Jobs/RetryWebhookJob.php index 409e4ef..d969b47 100644 --- a/src/Processing/Infrastructure/Laravel/Jobs/RetryWebhookJob.php +++ b/src/Processing/Infrastructure/Laravel/Jobs/RetryWebhookJob.php @@ -13,6 +13,7 @@ use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\Signature; use Proxynth\Larawebhook\Processing\Application\Commands\RetryWebhookCommand; use Proxynth\Larawebhook\Processing\Application\UseCases\RetryWebhook; +use Proxynth\Larawebhook\Shared\Application\Ports\EventBus; class RetryWebhookJob implements ShouldQueue { @@ -42,7 +43,7 @@ public function __construct( * * @throws WebhookException */ - public function handle(RetryWebhook $retryWebhook): void + public function handle(RetryWebhook $retryWebhook, EventBus $eventBus): void { $result = $retryWebhook->handle(new RetryWebhookCommand( payload: $this->payload, @@ -55,6 +56,8 @@ public function handle(RetryWebhook $retryWebhook): void idempotencyKey: $this->idempotencyKey, )); + $eventBus->dispatchMany($result->events); + if (! $result->shouldRetry) { return; } diff --git a/src/Shared/Application/EventBus.php b/src/Shared/Application/Ports/EventBus.php similarity index 83% rename from src/Shared/Application/EventBus.php rename to src/Shared/Application/Ports/EventBus.php index 5459ce6..3a05400 100644 --- a/src/Shared/Application/EventBus.php +++ b/src/Shared/Application/Ports/EventBus.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Proxynth\Larawebhook\Shared\Application; +namespace Proxynth\Larawebhook\Shared\Application\Ports; use Proxynth\Larawebhook\Shared\Domain\Events\DomainEvent; diff --git a/src/Shared/Infrastructure/Laravel/EventBus/LaravelEventBus.php b/src/Shared/Infrastructure/Laravel/EventBus/LaravelEventBus.php index cc93619..4ebc58a 100644 --- a/src/Shared/Infrastructure/Laravel/EventBus/LaravelEventBus.php +++ b/src/Shared/Infrastructure/Laravel/EventBus/LaravelEventBus.php @@ -5,7 +5,7 @@ namespace Proxynth\Larawebhook\Shared\Infrastructure\Laravel\EventBus; use Illuminate\Contracts\Events\Dispatcher; -use Proxynth\Larawebhook\Shared\Application\EventBus; +use Proxynth\Larawebhook\Shared\Application\Ports\EventBus; use Proxynth\Larawebhook\Shared\Domain\Events\DomainEvent; final readonly class LaravelEventBus implements EventBus diff --git a/src/Shared/Infrastructure/Laravel/Providers/LarawebhookServiceProvider.php b/src/Shared/Infrastructure/Laravel/Providers/LarawebhookServiceProvider.php index a7f2937..5fff0d7 100644 --- a/src/Shared/Infrastructure/Laravel/Providers/LarawebhookServiceProvider.php +++ b/src/Shared/Infrastructure/Laravel/Providers/LarawebhookServiceProvider.php @@ -47,8 +47,8 @@ use Proxynth\Larawebhook\Processing\Infrastructure\Idempotency\DefaultIdempotencyResolver; use Proxynth\Larawebhook\Processing\Infrastructure\Persistence\EloquentProcessedWebhookRecorder; use Proxynth\Larawebhook\Processing\Infrastructure\Persistence\EloquentReplayableWebhookRepository; -use Proxynth\Larawebhook\Shared\Application\EventBus; use Proxynth\Larawebhook\Shared\Application\Larawebhook; +use Proxynth\Larawebhook\Shared\Application\Ports\EventBus; use Proxynth\Larawebhook\Shared\Infrastructure\Laravel\EventBus\LaravelEventBus; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; diff --git a/tests/ArchTest.php b/tests/ArchTest.php index ee6e4d2..8d01a3d 100644 --- a/tests/ArchTest.php +++ b/tests/ArchTest.php @@ -1,6 +1,8 @@ expect(['var_dump', 'dd', 'dump', 'ray']) @@ -116,3 +118,13 @@ arch('application layer does not call config helper') ->expect('config') ->not->toBeUsedIn($pureApplications); + +arch('application use cases do not depend on laravel event dispatcher') + ->expect([ + 'Proxynth\Larawebhook\Ingestion\Application', + 'Proxynth\Larawebhook\Processing\Application', + 'Proxynth\Larawebhook\Audit\Application', + ])->not->toUse([ + Dispatcher::class, + Event::class, + ]); diff --git a/tests/Fakes/Shared/FakeEventBus.php b/tests/Fakes/Shared/FakeEventBus.php index 741f3e9..e20676a 100644 --- a/tests/Fakes/Shared/FakeEventBus.php +++ b/tests/Fakes/Shared/FakeEventBus.php @@ -4,7 +4,7 @@ namespace Proxynth\Larawebhook\Tests\Fakes\Shared; -use Proxynth\Larawebhook\Shared\Application\EventBus; +use Proxynth\Larawebhook\Shared\Application\Ports\EventBus; use Proxynth\Larawebhook\Shared\Domain\Events\DomainEvent; final class FakeEventBus implements EventBus diff --git a/tests/Feature/Http/Controllers/API/WebhookLogControllerTest.php b/tests/Feature/Http/Controllers/API/WebhookLogControllerTest.php index 34e245f..413ef97 100644 --- a/tests/Feature/Http/Controllers/API/WebhookLogControllerTest.php +++ b/tests/Feature/Http/Controllers/API/WebhookLogControllerTest.php @@ -1,6 +1,11 @@ json('message'))->toContain('Error replaying webhook:'); expect($response->json('message'))->toContain('Database connection lost'); }); + +it('dispatches replay domain events', function () { + Event::fake(); + + app()->instance( + EventBus::class, + new LaravelEventBus(app(Dispatcher::class)), + ); + + $log = WebhookLog::factory()->create([ + 'service' => 'stripe', + 'event' => 'payment_intent.succeeded', + 'payload' => ['type' => 'payment_intent.succeeded', 'data' => ['amount' => 1000]], + 'status' => 'failed', + 'attempt' => 0, + ]); + + $this->postJson("/api/larawebhook/logs/{$log->id}/replay"); + + Event::assertDispatched(WebhookReplayed::class); + Event::assertDispatched(WebhookLogged::class); + +}); diff --git a/tests/Feature/Middleware/ValidateWebhookMiddlewareTest.php b/tests/Feature/Middleware/ValidateWebhookMiddlewareTest.php index 1c5e2c0..ef2d2fb 100644 --- a/tests/Feature/Middleware/ValidateWebhookMiddlewareTest.php +++ b/tests/Feature/Middleware/ValidateWebhookMiddlewareTest.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use Illuminate\Events\Dispatcher; use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Route; use Proxynth\Larawebhook\Audit\Domain\Events\WebhookLogged; @@ -9,10 +10,10 @@ use Proxynth\Larawebhook\Ingestion\Domain\Events\WebhookReceived; use Proxynth\Larawebhook\Ingestion\Domain\Events\WebhookRejected; use Proxynth\Larawebhook\Ingestion\Domain\Events\WebhookValidated; -use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\Signature; use Proxynth\Larawebhook\Processing\Infrastructure\Laravel\Jobs\RetryWebhookJob; use Proxynth\Larawebhook\Processing\Infrastructure\Persistence\Models\ProcessedWebhookEvent; -use Proxynth\Larawebhook\Shared\Application\EventBus; +use Proxynth\Larawebhook\Shared\Application\Ports\EventBus; +use Proxynth\Larawebhook\Shared\Infrastructure\Laravel\EventBus\LaravelEventBus; use Proxynth\Larawebhook\Tests\Fakes\Shared\FakeEventBus; beforeEach(function () { @@ -1144,3 +1145,132 @@ expect(WebhookLog::query()->count())->toBe(2) ->and(ProcessedWebhookEvent::query()->count())->toBe(1); }); + +it('dispatches domain events after successful webhook receive', function () { + Event::fake([ + WebhookReceived::class, + WebhookValidated::class, + WebhookLogged::class, + ]); + + app()->instance( + EventBus::class, + new LaravelEventBus(app(Dispatcher::class)), + ); + + $payload = json_encode([ + 'action' => 'opened', + 'pull_request' => [ + 'id' => 123, + ], + ], JSON_THROW_ON_ERROR); + + $signature = githubSignature($payload, 'test_github_secret'); + + $this->call( + 'POST', + 'test-github-webhook', + [], + [], + [], + [ + 'HTTP_X_HUB_SIGNATURE_256' => $signature->value(), + 'HTTP_X_GITHUB_EVENT' => 'ping', + 'HTTP_X_GITHUB_DELIVERY' => 'delivery_123', + 'CONTENT_TYPE' => 'application/json', + ], + $payload + )->assertOk(); + + Event::assertDispatched(WebhookReceived::class); + Event::assertDispatched(WebhookValidated::class); + Event::assertDispatched(WebhookLogged::class); +}); + +it('dispatches domain events after rejected webhook receive', function () { + Event::fake(); + + config()->set('larawebhook.retries.async', false); + + app()->instance( + EventBus::class, + new LaravelEventBus(app(Dispatcher::class)), + ); + + $payload = json_encode([ + 'action' => 'opened', + 'pull_request' => [ + 'id' => 123, + ], + ], JSON_THROW_ON_ERROR); + + $this->call( + 'POST', + 'test-github-webhook', + [], + [], + [], + [ + 'HTTP_X_HUB_SIGNATURE_256' => 'sha1=invalid_signature', + 'HTTP_X_GITHUB_EVENT' => 'ping', + 'HTTP_X_GITHUB_DELIVERY' => 'delivery_123', + 'CONTENT_TYPE' => 'application/json', + ], + $payload + ); + + Event::assertDispatched(WebhookReceived::class); + Event::assertDispatched(WebhookRejected::class); + Event::assertDispatched(WebhookLogged::class); + Event::assertNotDispatched(WebhookValidated::class); +}); + +it('does not dispatch receive events for already processed webhook', function () { + Event::fake(); + + $payload = json_encode([ + 'action' => 'opened', + 'pull_request' => [ + 'id' => 123, + ], + ], JSON_THROW_ON_ERROR); + + $signature = githubSignature($payload, 'test_github_secret'); + + $this->call( + 'POST', + 'test-github-webhook', + [], + [], + [], + [ + 'HTTP_X_HUB_SIGNATURE_256' => $signature->value(), + 'HTTP_X_GITHUB_EVENT' => 'ping', + 'HTTP_X_GITHUB_DELIVERY' => 'delivery_123', + 'CONTENT_TYPE' => 'application/json', + ], + $payload + ); + + $this->call( + 'POST', + 'test-github-webhook', + [], + [], + [], + [ + 'HTTP_X_HUB_SIGNATURE_256' => $signature->value(), + 'HTTP_X_GITHUB_EVENT' => 'ping', + 'HTTP_X_GITHUB_DELIVERY' => 'delivery_123', + 'CONTENT_TYPE' => 'application/json', + ], + $payload + )->assertOk() + ->assertJson([ + 'status' => 'already_processed', + ]); + + Event::assertNotDispatched(WebhookReceived::class); + Event::assertNotDispatched(WebhookValidated::class); + Event::assertNotDispatched(WebhookLogged::class); +}); diff --git a/tests/Unit/Jobs/RetryWebhookJobTest.php b/tests/Unit/Jobs/RetryWebhookJobTest.php index b851e49..01efd95 100644 --- a/tests/Unit/Jobs/RetryWebhookJobTest.php +++ b/tests/Unit/Jobs/RetryWebhookJobTest.php @@ -3,11 +3,17 @@ declare(strict_types=1); use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Events\Dispatcher; use Illuminate\Support\Facades\Queue; +use Proxynth\Larawebhook\Audit\Domain\Events\WebhookLogged; +use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\Signature; use Proxynth\Larawebhook\Processing\Application\Data\RetryPolicy; use Proxynth\Larawebhook\Processing\Application\Ports\RetryPolicyResolver; use Proxynth\Larawebhook\Processing\Application\UseCases\RetryWebhook; +use Proxynth\Larawebhook\Processing\Domain\Events\WebhookProcessingFailed; use Proxynth\Larawebhook\Processing\Infrastructure\Laravel\Jobs\RetryWebhookJob; +use Proxynth\Larawebhook\Shared\Application\Ports\EventBus; +use Proxynth\Larawebhook\Shared\Infrastructure\Laravel\EventBus\LaravelEventBus; use Proxynth\Larawebhook\Tests\Fakes\Processing\FakeRetryPolicyResolver; beforeEach(function () { @@ -83,7 +89,7 @@ idempotencyKey: 'delivery_123', ); - $job->handle(app(RetryWebhook::class)); + $job->handle(app(RetryWebhook::class), app(EventBus::class)); Queue::assertNothingPushed(); }); @@ -102,7 +108,7 @@ idempotencyKey: 'delivery_123', ); - $job->handle(app(RetryWebhook::class)); + $job->handle(app(RetryWebhook::class), app(EventBus::class)); Queue::assertPushed(RetryWebhookJob::class, function (RetryWebhookJob $queuedJob) { return $queuedJob->attempt() === 1 @@ -134,8 +140,36 @@ idempotencyKey: 'delivery_123', ); - $job->handle(app(RetryWebhook::class)); + $job->handle(app(RetryWebhook::class), app(EventBus::class)); Queue::assertNothingPushed(); }); }); + +it('dispatches domain events produced by retry webhook', function () { + Event::fake(); + + app()->instance( + EventBus::class, + new LaravelEventBus(app(Dispatcher::class)), + ); + + $job = new RetryWebhookJob( + payload: '{"action":"opened"}', + signature: Signature::fromString('sha256=invalid'), + service: 'github', + event: 'opened', + secret: 'github_secret', + attempt: 1, + externalId: 'delivery_123', + idempotencyKey: 'delivery_123', + ); + + $job->handle( + app(RetryWebhook::class), + app(EventBus::class), + ); + + Event::assertDispatched(WebhookProcessingFailed::class); + Event::assertDispatched(WebhookLogged::class); +}); diff --git a/tests/Unit/LarawebhookServiceProviderTest.php b/tests/Unit/LarawebhookServiceProviderTest.php index d20f11b..38e86d1 100644 --- a/tests/Unit/LarawebhookServiceProviderTest.php +++ b/tests/Unit/LarawebhookServiceProviderTest.php @@ -17,7 +17,7 @@ use Proxynth\Larawebhook\Ingestion\Application\Ports\WebhookServiceMetadataResolver; use Proxynth\Larawebhook\Processing\Application\Ports\ReplayableWebhookRepository; use Proxynth\Larawebhook\Processing\Application\UseCases\RetryWebhook; -use Proxynth\Larawebhook\Shared\Application\EventBus; +use Proxynth\Larawebhook\Shared\Application\Ports\EventBus; use Proxynth\Larawebhook\Shared\Infrastructure\Laravel\EventBus\LaravelEventBus; use Proxynth\Larawebhook\Shared\Infrastructure\Laravel\Providers\LarawebhookServiceProvider;