1 | <?php |
||
11 | class Webhook |
||
12 | { |
||
13 | private static $eventSubscriptions = array(); |
||
14 | private static $receivedWebhook = null; |
||
15 | |||
16 | /** |
||
17 | * Subscribe to an incoming webhook request. The callback will be invoked when a matching webhook is received. |
||
18 | * |
||
19 | * @param string $event Name of the webhook event, e.g. subscribe, unsubscribe, campaign |
||
20 | * @param callable $callback A callable function to invoke with the data from the received webhook |
||
21 | * |
||
22 | * @return void |
||
23 | */ |
||
24 | public static function subscribe($event, callable $callback) |
||
25 | { |
||
26 | if (!isset(self::$eventSubscriptions[$event])) self::$eventSubscriptions[$event] = array(); |
||
27 | self::$eventSubscriptions[$event][] = $callback; |
||
28 | |||
29 | self::receive(); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Retrieve the incoming webhook request as sent. |
||
34 | * |
||
35 | * @param string $input An optional raw POST body to use instead of php://input - mainly for unit testing. |
||
36 | * |
||
37 | * @return array|false An associative array containing the details of the received webhook |
||
38 | */ |
||
39 | public static function receive($input = null) |
||
55 | |||
56 | /** |
||
57 | * Process the raw request into a PHP array and dispatch any matching subscription callbacks |
||
58 | * |
||
59 | * @param string $input The raw HTTP POST request |
||
60 | * |
||
61 | * @return array|false An associative array containing the details of the received webhook |
||
62 | */ |
||
63 | private static function processWebhook($input) |
||
74 | |||
75 | /** |
||
76 | * Call any subscribed callbacks for this event |
||
77 | * |
||
78 | * @param string $event The name of the callback event |
||
79 | * @param array $data An associative array of the webhook data |
||
80 | * |
||
81 | * @return void |
||
82 | */ |
||
83 | private static function dispatchWebhookEvent($event, $data) |
||
93 | } |
||
94 |