Skip to content

Commit c35e789

Browse files
committed
skip dispatching empty array with webhooks into queue
1 parent 4a49976 commit c35e789

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

.github/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ catch (\Throwable $exception) {
293293

294294
### Api server webhook tools
295295

296-
For webhook dispatching from server site to a client is here prepared Dispatcher.
297-
Dispatcher will find necessary webhooks for calling by using abstract webhook [Repository](../src/Service/Webhook/Repository.php)
296+
For webhook dispatching from server site to a client is here prepared WebhookDispatcher.
297+
WebhookDispatcher will find necessary webhooks for calling by using abstract webhook [Repository](../src/Service/Webhook/Repository.php)
298298
and after then call them by abstract webhook [Client](../src/Service/Webhook/Client.php).
299299

300300
You need to implement webhook Repository, webhook Client and have prepared
@@ -324,7 +324,7 @@ php artisan vendor:publish --tag=api-toolkit-migration
324324
* @var \SimpleAsFuck\ApiToolkit\Service\Webhook\Client $webhookClient
325325
*/
326326

327-
$dispatcher = new \SimpleAsFuck\ApiToolkit\Service\Webhook\Dispatcher($webhookRepository, $webhookClient);
327+
$dispatcher = new \SimpleAsFuck\ApiToolkit\Service\Webhook\WebhookDispatcher($webhookRepository, $webhookClient);
328328

329329
// simplest dispatch, when something happened on the server side,
330330
// webhooks calls are added into a queue

src/Service/Webhook/LaravelMysqlRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
/**
2424
* @param non-empty-string $type
2525
* @param array<non-empty-string, non-empty-string> $attributes
26-
* @return iterable<Webhook>
26+
* @return iterable<Webhook>&\Countable
2727
*/
2828
public function loadForDispatching(string $type, array $attributes): iterable
2929
{
@@ -68,7 +68,7 @@ public function loadForDispatching(string $type, array $attributes): iterable
6868
return $diff;
6969
});
7070

71-
return $webhooks;
71+
return new \ArrayIterator($webhooks);
7272
}
7373

7474
/**

src/Service/Webhook/Repository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ abstract class Repository
2626
*
2727
* @param non-empty-string $type
2828
* @param array<non-empty-string, non-empty-string> $attributes with webhook will be dispatched
29-
* @return iterable<Webhook>
29+
* @return iterable<Webhook>&\Countable
3030
*/
3131
abstract public function loadForDispatching(string $type, array $attributes): iterable;
3232

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use GuzzleHttp\RequestOptions;
88

9-
class Dispatcher
9+
class WebhookDispatcher
1010
{
1111
public function __construct(
1212
private Repository $repository,
@@ -20,9 +20,12 @@ public function __construct(
2020
* @param array<non-empty-string, string|array<string>> $headers to apply for http request
2121
* @param array<RequestOptions::*, mixed> $options to apply for http request
2222
*/
23-
public function dispatch(string $type, array $attributes, bool $synchronouslyFirstTry = false, array $headers = [], array $options = []): void
23+
public function dispatch(string $type, array $attributes = [], bool $synchronouslyFirstTry = false, array $headers = [], array $options = []): void
2424
{
2525
$webhooks = $this->repository->loadForDispatching($type, $attributes);
26+
if (count($webhooks) === 0) {
27+
return;
28+
}
2629

2730
if ($synchronouslyFirstTry) {
2831
$this->client->callWebhooks($webhooks, 0, $headers, $options);
@@ -33,15 +36,18 @@ public function dispatch(string $type, array $attributes, bool $synchronouslyFir
3336
}
3437

3538
/**
39+
* @param positive-int $delayInSeconds
3640
* @param non-empty-string $type
3741
* @param array<non-empty-string, non-empty-string> $attributes
38-
* @param positive-int $delayInSeconds
3942
* @param array<non-empty-string, string|array<string>> $headers to apply on http requests
4043
* @param array<RequestOptions::*, mixed> $options to apply on http requests
4144
*/
42-
public function dispatchWithDelay(string $type, array $attributes, int $delayInSeconds, array $headers = [], array $options = []): void
45+
public function dispatchWithDelay(int $delayInSeconds, string $type, array $attributes = [], array $headers = [], array $options = []): void
4346
{
4447
$webhooks = $this->repository->loadForDispatching($type, $attributes);
48+
if (count($webhooks) === 0) {
49+
return;
50+
}
4551

4652
$this->client->dispatchWebhooks($webhooks, $delayInSeconds, 0, $headers, $options);
4753
}

0 commit comments

Comments
 (0)