Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Oct 29, 2019
1 parent 7568a1b commit 4e611ec
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-webhook-client` will be documented in this file

## 2.3.0 - 2019-10-30

- add `WebhookConfigRepository` to make it easier to programmatically add config

## 2.2.0 - 2019-09-04

- Add Laravel 6 support
Expand Down
23 changes: 17 additions & 6 deletions src/WebhookClientServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,32 @@ public function boot()
return Route::post($url, '\Spatie\WebhookClient\WebhookController')->name("webhook-client-{$name}");
});

$this->app->singleton(WebhookConfigRepository::class, function() {
$configRepository = new WebhookConfigRepository();

collect(config('webhook-client.configs'))
->map(function (array $config) {
return new WebhookConfig($config);
})
->each(function(WebhookConfig $webhookConfig) use ($configRepository) {
$configRepository->addConfig($webhookConfig);
});

return $configRepository;
});

$this->app->bind(WebhookConfig::class, function () {
$routeName = Route::currentRouteName();

$configName = Str::after($routeName, 'webhook-client-');

$config = collect(config('webhook-client.configs'))
->first(function (array $config) use ($configName) {
return $config['name'] === $configName;
});
$webhookConfig = app(WebhookConfigRepository::class)->getConfig($configName);

if (is_null($config)) {
if (is_null($webhookConfig)) {
throw InvalidConfig::couldNotFindConfig($configName);
}

return new WebhookConfig($config);
return $webhookConfig;
});
}

Expand Down
19 changes: 19 additions & 0 deletions src/WebhookConfigRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Spatie\WebhookClient;

class WebhookConfigRepository
{
/** @var \Spatie\WebhookClient\WebhookConfig[] */
protected $configs;

public function addConfig(WebhookConfig $webhookConfig)
{
$this->configs[$webhookConfig->name] = $webhookConfig;
}

public function getConfig(string $name): ?WebhookConfig
{
return $this->configs[$name] ?? null;
}
}
17 changes: 17 additions & 0 deletions tests/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Spatie\WebhookClient\Tests\TestClasses\WebhookModelWithoutPayloadSaved;
use Spatie\WebhookClient\Tests\TestClasses\NothingIsValidSignatureValidator;
use Spatie\WebhookClient\Tests\TestClasses\EverythingIsValidSignatureValidator;
use Spatie\WebhookClient\WebhookClientServiceProvider;
use Spatie\WebhookClient\WebhookConfig;
use Spatie\WebhookClient\WebhookConfigRepository;

class WebhookControllerTest extends TestCase
{
Expand Down Expand Up @@ -48,6 +51,8 @@ public function setUp(): void
/** @test */
public function it_can_process_a_webhook_request()
{
$this->withoutExceptionHandling();

$this
->postJson('incoming-webhooks', $this->payload, $this->headers)
->assertSuccessful();
Expand Down Expand Up @@ -83,12 +88,14 @@ public function a_request_with_an_invalid_payload_will_not_get_processed()
public function it_can_work_with_an_alternative_signature_validator()
{
config()->set('webhook-client.configs.0.signature_validator', EverythingIsValidSignatureValidator::class);
$this->refreshWebhookConfigRepository();

$this
->postJson('incoming-webhooks', $this->payload, [])
->assertOk();

config()->set('webhook-client.configs.0.signature_validator', NothingIsValidSignatureValidator::class);
$this->refreshWebhookConfigRepository();

$this
->postJson('incoming-webhooks', $this->payload, [])
Expand All @@ -99,6 +106,7 @@ public function it_can_work_with_an_alternative_signature_validator()
public function it_can_work_with_an_alternative_profile()
{
config()->set('webhook-client.configs.0.webhook_profile', ProcessNothingWebhookProfile::class);
$this->refreshWebhookConfigRepository();

$this
->postJson('incoming-webhooks', $this->payload, $this->headers)
Expand All @@ -119,6 +127,7 @@ public function it_can_work_with_an_alternative_config()
->assertStatus(Response::HTTP_INTERNAL_SERVER_ERROR);

config()->set('webhook-client.configs.0.name', 'alternative-config');
$this->refreshWebhookConfigRepository();

$this
->postJson('incoming-webhooks-alternative-config', $this->payload, $this->headers)
Expand All @@ -129,6 +138,7 @@ public function it_can_work_with_an_alternative_config()
public function it_can_work_with_an_alternative_model()
{
config()->set('webhook-client.configs.0.webhook_model', WebhookModelWithoutPayloadSaved::class);
$this->refreshWebhookConfigRepository();

$this
->postJson('incoming-webhooks', $this->payload, $this->headers)
Expand Down Expand Up @@ -158,4 +168,11 @@ protected function getValidPayloadAndHeaders(): array

return [$payload, $headers];
}

protected function refreshWebhookConfigRepository()
{
$webhookConfig = new WebhookConfig(config('webhook-client.configs.0'));

app(WebhookConfigRepository::class)->addConfig($webhookConfig);
}
}

0 comments on commit 4e611ec

Please sign in to comment.