forked from jerrybendy/coding-webhook-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhook.php
More file actions
180 lines (139 loc) · 4.35 KB
/
Webhook.php
File metadata and controls
180 lines (139 loc) · 4.35 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* Created by PhpStorm.
* User: jerry
* Date: 16/6/12
* Time: 20:29
*/
namespace Jerrybendy\Coding;
class Webhook
{
/**
* define some support message types
* you'd better use these constants instead of string when
* you bind message callback
*/
const MESSAGE_TYPE_TEST = 'ping'; // This is for test event
const MESSAGE_TYPE_PUSH = 'push';
const MESSAGE_TYPE_TOPIC = 'topic';
const MESSAGE_TYPE_MEMBER = 'member';
const MESSAGE_TYPE_TASK = 'task';
const MESSAGE_TYPE_DOCUMENT = 'document';
const MESSAGE_TYPE_WATCH = 'watch';
const MESSAGE_TYPE_STAR = 'star';
const MESSAGE_TYPE_PR = 'pull_request';
const MESSAGE_TYPE_MR = 'merge_request';
const MESSAGE_TYPE_FAIL = 'fail';
const MESSAGE_TYPE_TOKEN_FAIL = 'token_fail';
/**
* @var string
*/
protected $token;
/**
* @var array
*/
protected $callback_container = [];
/**
* Webhook constructor.
*
* @param string $token
*/
public function __construct($token = '')
{
$this->token = $token;
}
/**
* @param string|array $messageType
* @param callable $callback
* @return $this
*/
public function on($messageType, callable $callback)
{
if (is_array($messageType)) {
foreach ($messageType as $mt) {
$this->on($mt, $callback);
}
return $this;
}
if (isset($this->callback_container[$messageType]) && !empty($this->callback_container[$messageType])) {
$this->callback_container [$messageType] [] = $callback;
} else {
$this->callback_container [$messageType] = [$callback];
}
return $this;
}
/**
* A short method to bind fail callback
*
* @param callable $callback
* @return $this
*/
public function onFail(callable $callback)
{
return $this->on(self::MESSAGE_TYPE_FAIL, $callback);
}
/**
* A short method to bind token fail callback,
* it will called when token is not match $this->token
*
* @param callable $callback
* @return $this
*/
public function onTokenFail(callable $callback)
{
return $this->on(self::MESSAGE_TYPE_TOKEN_FAIL, $callback);
}
/**
* Run the application and prepare to receive requests
*
*/
public function run()
{
/*
* Check header and get the message type
* if the message type is not registered, then return it with do nothing
*/
$message_type = isset($_SERVER['HTTP_X_CODING_EVENT']) ? $_SERVER['HTTP_X_CODING_EVENT'] : false;
if (!$message_type) {
$this->_invokeMessageType(self::MESSAGE_TYPE_FAIL, [new Webhook_Header_Error_Exception('Cannot find event header')]);
return;
}
if (!isset($this->callback_container[$message_type])) {
return;
}
/*
* Get and parse the post body
*/
$post = trim(file_get_contents('php://input'));
if ($post === false) {
$this->_invokeMessageType(self::MESSAGE_TYPE_FAIL, [new Webhook_Post_Content_Error_Exception('Cannot read post content')]);
return;
}
$post_parsed = json_decode($post);
if ($post_parsed === null) {
$this->_invokeMessageType(self::MESSAGE_TYPE_FAIL, [new Webhook_Post_Parse_Error_Exception('Cannot parse post content')]);
return;
}
/*
* Check token
*/
if (! empty($this->token) && (! isset($post_parsed->token) || $post_parsed->token !== $this->token)) {
$this->_invokeMessageType(self::MESSAGE_TYPE_TOKEN_FAIL, [new Webhook_Token_Error_Exception('Wrong token'), $post_parsed]);
return;
}
$this->_invokeMessageType($message_type, [$post_parsed]);
}
/**
* invoke all registered message if type matched
*
* @param string $type
* @param array $params
*/
protected function _invokeMessageType($type, array $params)
{
$callbacks = isset($this->callback_container[$type]) ? $this->callback_container[$type] : [];
foreach ($callbacks as $callback) {
call_user_func_array($callback, $params);
}
}
}