Repository navigation
Expand file tree
/
Copy pathWebhookStatus.php
More file actions
71 lines (63 loc) · 2.2 KB
/
Copy pathWebhookStatus.php
File metadata and controls
71 lines (63 loc) · 2.2 KB
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
<?php
/**
* @file WebhookStatus.php
*
* @brief Persists and reads back the last webhook send attempt per publication,
* so it can be shown to journal managers (sent/DOI/warnings/errors) and used to
* decide what event a manual resend should use.
*/
namespace APP\plugins\generic\webhook;
use Illuminate\Support\Facades\DB;
class WebhookStatus
{
protected const TABLE = 'webhook_publication_status';
public static function get(int $publicationId): ?array
{
$row = DB::table(self::TABLE)->where('publication_id', $publicationId)->first();
if (!$row) {
return null;
}
return [
'event' => $row->event,
'sentAt' => $row->sent_at,
'success' => (bool) $row->success,
'zsId' => $row->zs_id,
'zsUrl' => $row->zs_url,
'warnings' => $row->warnings ? json_decode($row->warnings, true) : [],
'error' => $row->error,
];
}
public static function recordSuccess(int $publicationId, string $event, ?string $zsId, ?string $zsUrl, array $warnings): void
{
DB::table(self::TABLE)->updateOrInsert(
['publication_id' => $publicationId],
[
'event' => $event,
'sent_at' => date('Y-m-d H:i:s'),
'success' => true,
'zs_id' => $zsId,
'zs_url' => $zsUrl,
'warnings' => $warnings ? json_encode($warnings) : null,
'error' => null,
]
);
}
/**
* Records a final failure (after all of SendWebhookJob's retries are
* exhausted). Deliberately leaves zs_id/zs_url/warnings untouched - they
* still reflect the last successful response, if any, since this attempt
* produced no new response to replace them with.
*/
public static function recordFailure(int $publicationId, string $event, string $error): void
{
DB::table(self::TABLE)->updateOrInsert(
['publication_id' => $publicationId],
[
'event' => $event,
'sent_at' => date('Y-m-d H:i:s'),
'success' => false,
'error' => $error,
]
);
}
}