-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWebhookField.php
158 lines (129 loc) · 4.18 KB
/
WebhookField.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace pju\KirbyWebhookField;
use Kirby\Exception\InvalidArgumentException;
/**
* Class WebhookField
* @package pju\KirbyWebhookField
*/
class WebhookField
{
/**
* The allowed status codes. Lists only persisting status codes
* Temporary ones such as hookNotFound can not be manually set
*
* @var array
*/
private static $allowedStates = ['success', 'progress', 'error'];
/**
* Format the configuration for a certain hook
*
* @param string $hookName
* @return array
*/
private static function formatHookConfig(string $hookName): array
{
$hooks = kirby()->option('pju.webhook-field.hooks');
$config = $hooks[$hookName];
if (is_string($config)) {
$config = ['url' => $config];
}
if (!isset($config['method'])) {
$config['method'] = 'POST';
}
if (isset($config['payload']) && is_callable($config['payload'])) {
$payload = $config['payload'];
$config['payload'] = $payload();
}
$config['showOutdated'] = $config['showOutdated'] ?? true;
return array_merge(['name' => $hookName], $config);
}
/**
* Get the hook config for a certain hook
* Falls back to the first hook in the config if it is not explicitly set
*
* @param string $hookName
* @return array
*/
public static function getHook(string $hookName): array
{
$hooks = kirby()->option('pju.webhook-field.hooks');
if (!$hooks || count($hooks) === 0) {
return ['name' => 'No hooks'];
}
if ($hookName === '') {
reset($hooks);
$firstKey = key($hooks);
return WebhookField::formatHookConfig($firstKey);
}
if (!isset($hooks[$hookName])) {
// TODO: find a better way?
return ['name' => 'Hook not found'];
};
return WebhookField::formatHookConfig($hookName);
}
/**
* Set the status of a hook to the specified state
* Returns a message for debugging
*
* @param string $hookName
* @param string $status
* @return string
* @throws InvalidArgumentException
*/
public static function setState(string $hookName, string $status)
{
if (!in_array($status, WebhookField::$allowedStates)) {
throw new InvalidArgumentException('Status not allowed');
}
$kirby = kirby();
$kirby->impersonate('kirby');
$cache = $kirby->cache('pju.webhook-field');
$state = $cache->get($hookName);
$state['status'] = $status;
// Don't save the time if we update to success - we want to know when the hook was triggered
if ($status !== 'success') {
$state['updated'] = time();
}
$cache->set($hookName, $state);
return 'status for ' . $hookName . ' changed to ' . $status;
}
/**
* Runs a callback for the specified hook
* The callback receives the status and the
*
* @param string $hookName
* @param string $status
*/
public static function runCallback(string $hookName, string $status)
{
$hook = WebhookField::getHook($hookName);
if (isset($hook['callback']) && is_callable($hook['callback'])) {
$req = kirby()->request();
$hook['callback']($status, $req);
}
}
/**
* Get the state of the specified hook
*
* @param string $hookName
* @return array
*/
public static function getState(string $hookName): array
{
$hooks = kirby()->option('pju.webhook-field.hooks');
if (!$hooks || count($hooks) === 0) {
return ['status' => 'hooksEmpty'];
}
if (!isset($hooks[$hookName])) {
return ['status' => 'hookNotfound'];
}
if (!isset($hooks[$hookName]['url']) || !$hooks[$hookName]['url']) {
return ['status' => 'hookNoUrl'];
}
$kirby = kirby();
$kirby->impersonate('kirby');
$cache = $kirby->cache('pju.webhook-field');
$state = $cache->get($hookName);
return $state ? $state : ['status' => 'new', 'updated' => null];
}
}