Skip to content

Commit 345ee00

Browse files
committed
first partial PSR-14 support for webhooks
1 parent e36b98e commit 345ee00

File tree

7 files changed

+24
-13
lines changed

7 files changed

+24
-13
lines changed

.github/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ class YourListeningController
189189
$response = \SimpleAsFuck\ApiToolkit\Factory\Server\ResponseFactory::makeWebhookResult();
190190
//$response = \SimpleAsFuck\ApiToolkit\Factory\Symfony\ResponseFactory::makeWebhookResult();
191191
$response = \SimpleAsFuck\ApiToolkit\Factory\Server\ResponseFactory::makeWebhookResult(
192+
// https://www.php-fig.org/psr/psr-14/#stoppable-events
192193
// you can inform server site application to stop
193-
// dispatching webhook for another listener after current listener
194-
// which has less priority
194+
// dispatching webhook for another listener with less priority
195195
// server services in this package support this functionality
196196
stopDispatching: true
197197
);

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"psr/log": "^2.0|^3.0",
1919
"symfony/psr-http-message-bridge": "^2.1|^6.0|^7.0",
2020
"symfony/http-foundation": "^5.4|^6.0|^7.0",
21-
"symfony/http-kernel": "^5.4|^6.0|^7.0"
21+
"symfony/http-kernel": "^5.4|^6.0|^7.0",
22+
"psr/event-dispatcher": "^1.0"
2223
},
2324
"require-dev": {
2425
"ext-pdo": "*",

src/Model/Webhook/Result.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@
44

55
namespace SimpleAsFuck\ApiToolkit\Model\Webhook;
66

7-
final class Result
7+
use Psr\EventDispatcher\StoppableEventInterface;
8+
9+
final class Result implements StoppableEventInterface
810
{
911
public function __construct(
12+
/** @deprecated will be private use PRS-14 interface $this::isPropagationStopped() */
1013
public readonly bool $stopDispatching,
1114
) {
1215
}
16+
17+
public function isPropagationStopped(): bool
18+
{
19+
/** @phpstan-ignore-next-line property.deprecated */
20+
return $this->stopDispatching;
21+
}
1322
}

src/Service/Webhook/LaravelMysqlRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function loadForDispatching(string $type, array $attributes): iterable
4040
}
4141

4242
foreach ($connection->table('WebhookWithoutAttribute')->pluck('webhookId') as $webhookId) {
43-
/** @phpstan-ignore-next-line */
43+
/** @var int $webhookId */
4444
if (array_key_exists($webhookId, $webhookMatches)) {
4545
continue;
4646
}
@@ -187,7 +187,7 @@ private function getWebhookRequiredAttributes(ConnectionInterface $connection, i
187187
$attributes = [];
188188
foreach ($connection->table('WebhookRequiredAttribute')->where('webhookId', '=', $webhookId)->get() as $requiredAttribute) {
189189
/** @var \stdClass $requiredAttribute */
190-
/** @var \stdClass|null $storedAttribute */
190+
/** @var object{key: string, value: string}|null $storedAttribute */
191191
$storedAttribute = $connection->table('WebhookAttribute')->where('id', '=', $requiredAttribute->webhookAttributeId)->first();
192192
if ($storedAttribute !== null) {
193193
$attributes[$storedAttribute->key] = $storedAttribute->value;

src/Service/Webhook/ResultTransformer.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,28 @@
44

55
namespace SimpleAsFuck\ApiToolkit\Service\Webhook;
66

7+
use Psr\EventDispatcher\StoppableEventInterface;
78
use SimpleAsFuck\ApiToolkit\Model\Webhook\Result;
89
use SimpleAsFuck\ApiToolkit\Service\Transformation\Transformer;
910
use SimpleAsFuck\Validator\Rule\Custom\UserClassRule;
1011
use SimpleAsFuck\Validator\Rule\Object\ObjectRule;
1112

1213
/**
13-
* @implements UserClassRule<Result>
14-
* @implements Transformer<Result>
14+
* @implements UserClassRule<Result&StoppableEventInterface>
15+
* @implements Transformer<Result&StoppableEventInterface>
1516
*/
1617
final class ResultTransformer implements UserClassRule, Transformer
1718
{
18-
public function validate(ObjectRule $rule): Result
19+
public function validate(ObjectRule $rule): Result&StoppableEventInterface
1920
{
2021
return new Result($rule->property('stopDispatching')->bool()->nullable() ?? false);
2122
}
2223

2324
/**
24-
* @param Result $transformed
25+
* @param Result&StoppableEventInterface $transformed
2526
*/
2627
public function toApi($transformed): \stdClass
2728
{
28-
return (object) ['stopDispatching' => $transformed->stopDispatching];
29+
return (object) ['stopDispatching' => $transformed->isPropagationStopped()];
2930
}
3031
}

src/Service/Webhook/WebhookClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ final public function callWebhooks(iterable $webhooks, array $headers = [], arra
6464
->notNull()
6565
;
6666

67-
if ($callResult->stopDispatching) {
67+
if ($callResult->isPropagationStopped()) {
6868
break;
6969
}
7070
} catch (\Throwable $exception) {

test/Service/Webhook/LaravelMysqlRepositoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public function testSave(string $type, Params $params): void
197197
foreach ($dbRequiredAttributeMaps as $dbRequiredAttributeMap) {
198198
self::assertSame($webhook->id, (string) $dbRequiredAttributeMap->webhookId);
199199

200-
/** @var \stdClass $dbRequiredAttribute */
200+
/** @var object{key: string, value: string} $dbRequiredAttribute */
201201
$dbRequiredAttribute = $connection->selectOne('select * from WebhookAttribute where id = ?', [$dbRequiredAttributeMap->webhookAttributeId]);
202202
self::assertSame($params->attributes[$dbRequiredAttribute->key], $dbRequiredAttribute->value);
203203
}

0 commit comments

Comments
 (0)