generated from simple-as-fuck/php-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.php
91 lines (77 loc) · 3.49 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types=1);
namespace SimpleAsFuck\ApiToolkit\Service\Webhook;
use GuzzleHttp\RequestOptions;
use Psr\Log\LoggerInterface;
use SimpleAsFuck\ApiToolkit\Model\Webhook\Webhook;
use SimpleAsFuck\ApiToolkit\Service\Http\MessageService;
use SimpleAsFuck\Validator\Factory\UnexpectedValueException;
abstract class Client
{
public function __construct(
private Config $config,
private \GuzzleHttp\Client $client,
private ?LoggerInterface $logger,
) {
}
/**
* @param iterable<Webhook> $webhooks
* @param array<string, string|array<string>> $headers
* @param array<RequestOptions::*, mixed> $options
*/
final public function callWebhooks(iterable $webhooks, int $tries, array $headers, array $options): void
{
$requestHeaders = $headers;
$requestOptions = $options;
$token = $this->config->getBearerToken();
if (! array_key_exists('Authorization', $requestHeaders) && $token !== null) {
$requestHeaders['Authorization'] = 'Bearer '.$token;
}
$requestOptions[RequestOptions::HEADERS] = $requestHeaders;
$nextTryWebhooks = [];
foreach ($webhooks as $webhook) {
if (count($nextTryWebhooks) !== 0) {
$nextTryWebhooks[] = $webhook;
continue;
}
try {
$requestOptions[RequestOptions::JSON] = (new WebhookTransformer())->toApi($webhook);
$response = $this->client->request('POST', $webhook->params()->listeningUrl(), $requestOptions);
$callResult = MessageService::parseJsonFromBody(new UnexpectedValueException(), $response, 'Webhook response body', false)
->object()->class(new ResultTransformer())
->notNull()
;
if ($callResult->stopDispatching()) {
break;
}
} catch (\Throwable $exception) {
$this->logger?->warning('Call webhook: "'.$webhook->id().'" fail message: "'.$exception->getMessage().'", webhook url: "'.$webhook->params()->listeningUrl().'"', [
'webhookId' => $webhook->id(),
'exception' => $exception
]);
$nextTryWebhooks[] = $webhook;
}
}
if (count($nextTryWebhooks) !== 0) {
++$tries;
$maximumTries = $this->config->getMaxTries();
if ($tries >= $maximumTries) {
$webhookIdsWithoutRetry = implode(', ', array_map(static fn (Webhook $webhook): string => '"'.$webhook->id().'"', $nextTryWebhooks));
$this->logger?->error('Call webhooks: '.$webhookIdsWithoutRetry.' fail without any retry, maximum tries: '.$maximumTries);
return;
}
$this->dispatchWebhooks($nextTryWebhooks, $this->config->getDelayBetweenTries(), $tries, $headers, $options);
}
}
/**
* method SHOULD add iterable with webhooks into some queue for asynchronous or delayed calls
*
* while webhook iterable is ready for calls from queue, queue worker MUST call static::callWebhooks method
*
* @param iterable<Webhook> $webhooks
* @param int<0, max> $delayInSeconds
* @param array<string, string|array<string>> $headers
* @param array<RequestOptions::*, mixed> $options
*/
abstract public function dispatchWebhooks(iterable $webhooks, int $delayInSeconds, int $tries, array $headers, array $options): void;
}