Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/Ingestion/Application/UseCases/ReceiveWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\Provider;
use Proxynth\Larawebhook\Ingestion\Domain\ValueObjects\RawPayload;
use Proxynth\Larawebhook\Processing\Application\Ports\IdempotencyResolver;
use Proxynth\Larawebhook\Processing\Application\Ports\RetryConfigurationResolver;
use Proxynth\Larawebhook\Processing\Application\Ports\WebhookDuplicateDetector;
use Proxynth\Larawebhook\Processing\Domain\Entities\WebhookEvent;
use Proxynth\Larawebhook\Processing\Domain\ValueObjects\DeliveryAttempt;
Expand All @@ -34,6 +35,7 @@ public function __construct(
private WebhookDuplicateDetector $duplicateDetector,
private WebhookPayloadParserResolver $payloadParserResolver,
private WebhookSecretResolver $secretResolver,
private RetryConfigurationResolver $retryConfigurationResolver,
private ValidateWebhook $validateWebhook,
private RecordWebhookLog $recordWebhookLog,
) {}
Expand Down Expand Up @@ -155,7 +157,7 @@ public function handle(ReceiveWebhookCommand $command): ReceiveWebhookResult
errorMessage: $validation->errorMessage,
));

if ($this->shouldRetryAsync()) {
if ($this->retryConfigurationResolver->resolve()->shouldRetryAsync()) {
return ReceiveWebhookResult::acceptedForRetry(
log: $log,
event: $event,
Expand Down Expand Up @@ -216,12 +218,6 @@ private function extractExternalId(
->extractExternalId($decodedPayload, $externalIdHeaderValue);
}

private function shouldRetryAsync(): bool
{
return config('larawebhook.retries.enabled', true)
&& config('larawebhook.retries.async', false);
}

private function failureStatusCode(?string $errorMessage): int
{
$errorMessage ??= '';
Expand Down
18 changes: 18 additions & 0 deletions src/Processing/Application/Data/RetryConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Proxynth\Larawebhook\Processing\Application\Data;

final readonly class RetryConfiguration
{
public function __construct(
public bool $enabled,
public bool $async,
) {}

public function shouldRetryAsync(): bool
{
return $this->enabled && $this->async;
}
}
12 changes: 12 additions & 0 deletions src/Processing/Application/Ports/RetryConfigurationResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Proxynth\Larawebhook\Processing\Application\Ports;

use Proxynth\Larawebhook\Processing\Application\Data\RetryConfiguration;

interface RetryConfigurationResolver
{
public function resolve(): RetryConfiguration;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Proxynth\Larawebhook\Processing\Infrastructure\Config;

use Proxynth\Larawebhook\Processing\Application\Data\RetryConfiguration;
use Proxynth\Larawebhook\Processing\Application\Ports\RetryConfigurationResolver;

class ConfigRetryConfigurationResolver implements RetryConfigurationResolver
{
public function resolve(): RetryConfiguration
{
return new RetryConfiguration(
enabled: (bool) config('larawebhook.retries.enabled', true),
async: (bool) config('larawebhook.retries.async', false),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
use Proxynth\Larawebhook\Ingestion\Infrastructure\Validation\WebhookValidatorFactory;
use Proxynth\Larawebhook\Processing\Application\Ports\IdempotencyResolver;
use Proxynth\Larawebhook\Processing\Application\Ports\ReplayableWebhookRepository;
use Proxynth\Larawebhook\Processing\Application\Ports\RetryConfigurationResolver;
use Proxynth\Larawebhook\Processing\Application\Ports\RetryPolicyResolver;
use Proxynth\Larawebhook\Processing\Application\Ports\WebhookDuplicateDetector;
use Proxynth\Larawebhook\Processing\Application\UseCases\ReplayWebhook;
use Proxynth\Larawebhook\Processing\Application\UseCases\RetryWebhook;
use Proxynth\Larawebhook\Processing\Infrastructure\Config\ConfigRetryConfigurationResolver;
use Proxynth\Larawebhook\Processing\Infrastructure\Config\ConfigRetryPolicyResolver;
use Proxynth\Larawebhook\Processing\Infrastructure\Deduplication\EloquentWebhookDuplicateDetector;
use Proxynth\Larawebhook\Processing\Infrastructure\Idempotency\DefaultIdempotencyResolver;
Expand Down Expand Up @@ -175,6 +177,8 @@ private function registerServices(): void
$this->app->singleton(WebhookSecretResolver::class, ConfigWebhookSecretResolver::class);

$this->app->singleton(RetryPolicyResolver::class, ConfigRetryPolicyResolver::class);

$this->app->singleton(RetryConfigurationResolver::class, ConfigRetryConfigurationResolver::class);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions tests/ArchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
'Proxynth\Larawebhook\Shared\Application',
];

$pureApplications = array_values(array_diff($applications, [
'Proxynth\Larawebhook\Shared\Application\Larawebhook',
]));

$controllers = [
'Proxynth\Larawebhook\Audit\Infrastructure\Laravel\Http\Controllers',
'Proxynth\Larawebhook\Ingestion\Infrastructure\Laravel\Http\Controllers',
Expand Down Expand Up @@ -109,10 +113,6 @@
'Proxynth\Larawebhook\Shared\Infrastructure\Laravel\Http\Controllers',
]);

arch('application does not call config helper')
->expect([
'Proxynth\Larawebhook\Ingestion\Application',
'Proxynth\Larawebhook\Processing\Application',
'Proxynth\Larawebhook\Audit\Application',
])
->not->toUse('config');
arch('application layer does not call config helper')
->expect('config')
->not->toBeUsedIn($pureApplications);
20 changes: 20 additions & 0 deletions tests/Fakes/Processing/FakeRetryConfigurationResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Proxynth\Larawebhook\Tests\Fakes\Processing;

use Proxynth\Larawebhook\Processing\Application\Data\RetryConfiguration;
use Proxynth\Larawebhook\Processing\Application\Ports\RetryConfigurationResolver;

final readonly class FakeRetryConfigurationResolver implements RetryConfigurationResolver
{
public function __construct(
private RetryConfiguration $configuration,
) {}

public function resolve(): RetryConfiguration
{
return $this->configuration;
}
}
26 changes: 17 additions & 9 deletions tests/Unit/Ingestion/Application/UseCases/ReceiveWebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
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\Application\Data\RetryConfiguration;
use Proxynth\Larawebhook\Processing\Application\Ports\RetryConfigurationResolver;
use Proxynth\Larawebhook\Processing\Application\Ports\WebhookDuplicateDetector;
use Proxynth\Larawebhook\Shared\Domain\Enums\WebhookService;
use Proxynth\Larawebhook\Tests\Fakes\Ingestion\FakeWebhookSecretResolver;
use Proxynth\Larawebhook\Tests\Fakes\Processing\FakeRetryConfigurationResolver;
use Proxynth\Larawebhook\Tests\Fakes\Processing\FakeWebhookDuplicateDetector;

function githubPayload(string $deliveryId = 'delivery_123'): string
Expand Down Expand Up @@ -45,7 +48,13 @@ function githubSignature(string $payload, string $secret = 'github_secret'): Sig

config()->set('larawebhook.services.github.webhook_secret', 'secret');
config()->set('larawebhook.retries.enabled', true);
config()->set('larawebhook.retries.async', false);
app()->instance(
RetryConfigurationResolver::class,
new FakeRetryConfigurationResolver(new RetryConfiguration(
enabled: true,
async: false,
)),
);
});

it('receives a valid webhook and logs it successfully', function () {
Expand Down Expand Up @@ -150,9 +159,6 @@ function githubSignature(string $payload, string $secret = 'github_secret'): Sig
});

it('returns failed when signature is invalid and async retries are disabled', function () {
config()->set('larawebhook.retries.enabled', true);
config()->set('larawebhook.retries.async', false);

app()->instance(WebhookSecretResolver::class, new FakeWebhookSecretResolver([
'github' => 'github_secret',
]));
Expand All @@ -179,8 +185,13 @@ function githubSignature(string $payload, string $secret = 'github_secret'): Sig
});

it('returns accepted for retry when signature is invalid and async retires are enabled', function () {
config()->set('larawebhook.retries.enabled', true);
config()->set('larawebhook.retries.async', true);
app()->instance(
RetryConfigurationResolver::class,
new FakeRetryConfigurationResolver(new RetryConfiguration(
enabled: true,
async: true,
)),
);

app()->instance(WebhookSecretResolver::class, new FakeWebhookSecretResolver([
'github' => 'github_secret',
Expand Down Expand Up @@ -271,9 +282,6 @@ function githubSignature(string $payload, string $secret = 'github_secret'): Sig
});

it('marks webhook as failed when validation fails and async retries are disabled', function () {
config()->set('larawebhook.retries.enabled', true);
config()->set('larawebhook.retries.async', false);

app()->instance(WebhookSecretResolver::class, new FakeWebhookSecretResolver([
'github' => 'github_secret',
]));
Expand Down