|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DrewM\MailChimp; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* A MailChimp Webhook request. |
|
7
|
|
|
* How to Set Up Webhooks: http://eepurl.com/bs-j_T |
|
8
|
|
|
* |
|
9
|
|
|
* @author Drew McLellan <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class Webhook |
|
12
|
|
|
{ |
|
13
|
|
|
private static $eventSubscriptions = array(); |
|
14
|
|
|
private static $receivedWebhook = false; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Subscribe to an incoming webhook request. The callback will be invoked when a matching webhook is received. |
|
18
|
|
|
* @param string $event Name of the webhook event, e.g. subscribe, unsubscribe, campaign |
|
19
|
|
|
* @param callable $callback A callable function to invoke with the data from the received webhook |
|
20
|
|
|
* @return void |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function subscribe($event, callable $callback) |
|
23
|
|
|
{ |
|
24
|
|
|
if (!isset(self::$eventSubscriptions[$event])) self::$eventSubscriptions[$event] = array(); |
|
25
|
|
|
self::$eventSubscriptions[$event][] = $callback; |
|
26
|
|
|
|
|
27
|
|
|
self::receive(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Retrieve the incoming webhook request as sent. |
|
32
|
|
|
* @param string $input An optional raw POST body to use instead of php://input - mainly for unit testing. |
|
33
|
|
|
* @return array|false An associative array containing the details of the received webhook |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function receive($input = null) |
|
36
|
|
|
{ |
|
37
|
|
|
if (is_null($input)) { |
|
38
|
|
|
if (self::$receivedWebhook !== false) { |
|
39
|
|
|
$input = self::$receivedWebhook; |
|
40
|
|
|
} else { |
|
41
|
|
|
$input = file_get_contents("php://input"); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (!is_null($input) && $input != '') { |
|
46
|
|
|
return self::processWebhook($input); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Process the raw request into a PHP array and dispatch any matching subscription callbacks |
|
54
|
|
|
* @param string $input The raw HTTP POST request |
|
55
|
|
|
* @return array|false An associative array containing the details of the received webhook |
|
56
|
|
|
*/ |
|
57
|
|
|
private static function processWebhook($input) |
|
58
|
|
|
{ |
|
59
|
|
|
self::$receivedWebhook = $input; |
|
|
|
|
|
|
60
|
|
|
parse_str($input, $result); |
|
61
|
|
|
if ($result && isset($result['type'])) { |
|
62
|
|
|
self::dispatchWebhookEvent($result['type'], $result['data']); |
|
63
|
|
|
return $result; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Call any subscribed callbacks for this event |
|
71
|
|
|
* @param string $event The name of the callback event |
|
72
|
|
|
* @param array $data An associative array of the webhook data |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
|
private static function dispatchWebhookEvent($event, $data) |
|
76
|
|
|
{ |
|
77
|
|
|
if (isset(self::$eventSubscriptions[$event])) { |
|
78
|
|
|
foreach(self::$eventSubscriptions[$event] as $callback) { |
|
79
|
|
|
$callback($data); |
|
80
|
|
|
} |
|
81
|
|
|
// reset subscriptions |
|
82
|
|
|
self::$eventSubscriptions[$event] = array(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.