Skip to content

Commit

Permalink
Fix sync handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
hotmeteor committed Oct 27, 2022
1 parent 6210aa7 commit 67d5a28
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,12 @@ Each handler is constructed with the `event` (name of the webhook event) and `da

namespace App\Http\Handlers\Stripe;

use Illuminate\Foundation\Bus\Dispatchable;

class CustomerCreated
{
use Dispatchable;

public function __construct(public string $event, public array $data)
{
}
Expand Down
21 changes: 14 additions & 7 deletions src/Providers/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Receiver\Providers;

use Closure;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Http\JsonResponse;
Expand Down Expand Up @@ -102,7 +101,7 @@ public function receive(Request $request): static
*/
public function ok(): JsonResponse|Response
{
if (! $this->dispatched && $this->fallback) {
if (! $this->dispatched() && $this->fallback) {
$callback = $this->fallback;

$callback($this->webhook);
Expand Down Expand Up @@ -139,6 +138,14 @@ public function webhook(): ?Webhook
return $this->webhook;
}

/**
* @return PendingDispatch|null
*/
public function dispatched(): ?PendingDispatch
{
return $this->dispatched;
}

/**
* @param Request $request
* @return Webhook
Expand All @@ -152,19 +159,19 @@ protected function mapWebhook(Request $request): Webhook
}

/**
* @return void
* @return AbstractProvider
*/
protected function handle(): void
protected function handle(): static
{
$class = $this->getClass($event = $this->webhook->getEvent());

if (class_exists($class)) {
$instance = new $class($event, $this->webhook->getData(), $this->webhook);

$this->dispatched = $instance instanceof ShouldQueue
? dispatch($instance)
: dispatch_sync($instance);
$this->dispatched = dispatch($instance);
}

return $this;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions stubs/receiver-verified.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace {{ providerNamespace }};

use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;
use Receiver\Providers\AbstractProvider;

class {{ providerClass }} extends AbstractProvider
{
use Dispatchable;

/**
* @param Request $request
* @return bool
Expand Down
3 changes: 3 additions & 0 deletions stubs/receiver.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace {{ providerNamespace }};

use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;
use Receiver\Providers\AbstractProvider;

class {{ providerClass }} extends AbstractProvider
{
use Dispatchable;

/**
* @param Request $request
* @return string
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/FooBarred.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace Receiver\Tests\Fixtures;

use Illuminate\Foundation\Bus\Dispatchable;

class FooBarred
{
use Dispatchable;

public function __construct(public string $event, public array $data)
{
}
Expand Down
6 changes: 4 additions & 2 deletions tests/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public function test_handles_webhook_with_existing_handler()

$provider = new TestProvider();

$response = $provider->receive($request)
$response = $provider
->receive($request)
->fallback(fn (Webhook $webhook) => throw new \Exception('Fallback!'))
->ok();

Expand All @@ -33,7 +34,8 @@ public function test_handles_webhook_with_missing_handler()

$provider = new TestProvider();

$response = $provider->receive($request)
$response = $provider
->receive($request)
->fallback(fn (Webhook $webhook) => throw new \Exception('Fallback!'))
->ok();

Expand Down

0 comments on commit 67d5a28

Please sign in to comment.