@@ -14,167 +14,167 @@ |
||
14 | 14 | class Notifications extends AbstractActivity |
15 | 15 | { |
16 | 16 | |
17 | - /** |
|
18 | - * List your notifications |
|
19 | - * |
|
20 | - * @link https://developer.github.com/v3/activity/notifications/#list-your-notifications |
|
21 | - * |
|
22 | - * @param bool $all |
|
23 | - * @param bool $participating |
|
24 | - * @param string $since |
|
25 | - * @param string $before |
|
26 | - * |
|
27 | - * @return array |
|
28 | - * @throws \Exception |
|
29 | - */ |
|
30 | - public function listNotifications(bool $all = false, bool $participating = false, string $since = 'now', |
|
31 | - string $before = null): array |
|
32 | - { |
|
33 | - return $this->getApi()->request($this->getApi()->sprintf('/notifications?:args', http_build_query([ |
|
34 | - 'all' => $all, |
|
35 | - 'participating' => $participating, |
|
36 | - 'since' => (new DateTime($since))->format(DateTime::ATOM), |
|
37 | - 'before' => (new DateTime($before))->format(DateTime::ATOM) |
|
38 | - ]))); |
|
39 | - } |
|
17 | + /** |
|
18 | + * List your notifications |
|
19 | + * |
|
20 | + * @link https://developer.github.com/v3/activity/notifications/#list-your-notifications |
|
21 | + * |
|
22 | + * @param bool $all |
|
23 | + * @param bool $participating |
|
24 | + * @param string $since |
|
25 | + * @param string $before |
|
26 | + * |
|
27 | + * @return array |
|
28 | + * @throws \Exception |
|
29 | + */ |
|
30 | + public function listNotifications(bool $all = false, bool $participating = false, string $since = 'now', |
|
31 | + string $before = null): array |
|
32 | + { |
|
33 | + return $this->getApi()->request($this->getApi()->sprintf('/notifications?:args', http_build_query([ |
|
34 | + 'all' => $all, |
|
35 | + 'participating' => $participating, |
|
36 | + 'since' => (new DateTime($since))->format(DateTime::ATOM), |
|
37 | + 'before' => (new DateTime($before))->format(DateTime::ATOM) |
|
38 | + ]))); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * List your notifications in a repository |
|
43 | - * |
|
44 | - * @link https://developer.github.com/v3/activity/notifications/#list-your-notifications-in-a-repository |
|
45 | - * |
|
46 | - * @param bool $all |
|
47 | - * @param bool $participating |
|
48 | - * @param string $since |
|
49 | - * @param string $before |
|
50 | - * |
|
51 | - * @return array |
|
52 | - * @throws \Exception |
|
53 | - */ |
|
54 | - public function listRepositoryNotifications(bool $all = false, bool $participating = false, string $since = 'now', |
|
55 | - string $before = null): array |
|
56 | - { |
|
57 | - return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/notifications?:args', |
|
58 | - $this->getActivity()->getOwner(), $this->getActivity()->getRepo(), http_build_query([ |
|
59 | - 'all' => $all, |
|
60 | - 'participating' => $participating, |
|
61 | - 'since' => (new DateTime($since))->format(DateTime::ATOM), |
|
62 | - 'before' => (new DateTime($before))->format(DateTime::ATOM) |
|
63 | - ]))); |
|
64 | - } |
|
41 | + /** |
|
42 | + * List your notifications in a repository |
|
43 | + * |
|
44 | + * @link https://developer.github.com/v3/activity/notifications/#list-your-notifications-in-a-repository |
|
45 | + * |
|
46 | + * @param bool $all |
|
47 | + * @param bool $participating |
|
48 | + * @param string $since |
|
49 | + * @param string $before |
|
50 | + * |
|
51 | + * @return array |
|
52 | + * @throws \Exception |
|
53 | + */ |
|
54 | + public function listRepositoryNotifications(bool $all = false, bool $participating = false, string $since = 'now', |
|
55 | + string $before = null): array |
|
56 | + { |
|
57 | + return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/notifications?:args', |
|
58 | + $this->getActivity()->getOwner(), $this->getActivity()->getRepo(), http_build_query([ |
|
59 | + 'all' => $all, |
|
60 | + 'participating' => $participating, |
|
61 | + 'since' => (new DateTime($since))->format(DateTime::ATOM), |
|
62 | + 'before' => (new DateTime($before))->format(DateTime::ATOM) |
|
63 | + ]))); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Mark as read |
|
68 | - * |
|
69 | - * @link https://developer.github.com/v3/activity/notifications/#mark-as-read |
|
70 | - * |
|
71 | - * @param string $lastReadAt |
|
72 | - * |
|
73 | - * @return array |
|
74 | - */ |
|
75 | - public function markAsRead(string $lastReadAt = 'now'): array |
|
76 | - { |
|
77 | - return $this->getApi()->request($this->getApi()->sprintf('/notifications?:args', |
|
78 | - http_build_query(['last_read_at' => (new DateTime($lastReadAt))->format(DateTime::ATOM)])), |
|
79 | - Request::METHOD_PUT); |
|
80 | - } |
|
66 | + /** |
|
67 | + * Mark as read |
|
68 | + * |
|
69 | + * @link https://developer.github.com/v3/activity/notifications/#mark-as-read |
|
70 | + * |
|
71 | + * @param string $lastReadAt |
|
72 | + * |
|
73 | + * @return array |
|
74 | + */ |
|
75 | + public function markAsRead(string $lastReadAt = 'now'): array |
|
76 | + { |
|
77 | + return $this->getApi()->request($this->getApi()->sprintf('/notifications?:args', |
|
78 | + http_build_query(['last_read_at' => (new DateTime($lastReadAt))->format(DateTime::ATOM)])), |
|
79 | + Request::METHOD_PUT); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Mark notifications as read in a repository |
|
84 | - * |
|
85 | - * @link https://developer.github.com/v3/activity/notifications/#mark-notifications-as-read-in-a-repository |
|
86 | - * |
|
87 | - * @param string $lastReadAt |
|
88 | - * |
|
89 | - * @return array |
|
90 | - */ |
|
91 | - public function markAsReadInRepository(string $lastReadAt = 'now'): array |
|
92 | - { |
|
93 | - return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/notifications?:args', |
|
94 | - $this->getActivity()->getOwner(), $this->getActivity()->getRepo(), |
|
95 | - http_build_query(['last_read_at' => (new DateTime($lastReadAt))->format(DateTime::ATOM)])), |
|
96 | - Request::METHOD_PUT); |
|
97 | - } |
|
82 | + /** |
|
83 | + * Mark notifications as read in a repository |
|
84 | + * |
|
85 | + * @link https://developer.github.com/v3/activity/notifications/#mark-notifications-as-read-in-a-repository |
|
86 | + * |
|
87 | + * @param string $lastReadAt |
|
88 | + * |
|
89 | + * @return array |
|
90 | + */ |
|
91 | + public function markAsReadInRepository(string $lastReadAt = 'now'): array |
|
92 | + { |
|
93 | + return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/notifications?:args', |
|
94 | + $this->getActivity()->getOwner(), $this->getActivity()->getRepo(), |
|
95 | + http_build_query(['last_read_at' => (new DateTime($lastReadAt))->format(DateTime::ATOM)])), |
|
96 | + Request::METHOD_PUT); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * View a single thread |
|
101 | - * |
|
102 | - * @link https://developer.github.com/v3/activity/notifications/#view-a-single-thread |
|
103 | - * |
|
104 | - * @param int $id |
|
105 | - * |
|
106 | - * @return array |
|
107 | - */ |
|
108 | - public function viewThread(int $id): array |
|
109 | - { |
|
110 | - return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id', (string)$id)); |
|
111 | - } |
|
99 | + /** |
|
100 | + * View a single thread |
|
101 | + * |
|
102 | + * @link https://developer.github.com/v3/activity/notifications/#view-a-single-thread |
|
103 | + * |
|
104 | + * @param int $id |
|
105 | + * |
|
106 | + * @return array |
|
107 | + */ |
|
108 | + public function viewThread(int $id): array |
|
109 | + { |
|
110 | + return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id', (string)$id)); |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * Mark a thread as read |
|
115 | - * |
|
116 | - * @link https://developer.github.com/v3/activity/notifications/#mark-a-thread-as-read |
|
117 | - * |
|
118 | - * @param int $id |
|
119 | - * |
|
120 | - * @return array |
|
121 | - */ |
|
122 | - public function markThreadAsRead(int $id): array |
|
123 | - { |
|
124 | - return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id', (string)$id), |
|
125 | - Request::METHOD_PATCH); |
|
126 | - } |
|
113 | + /** |
|
114 | + * Mark a thread as read |
|
115 | + * |
|
116 | + * @link https://developer.github.com/v3/activity/notifications/#mark-a-thread-as-read |
|
117 | + * |
|
118 | + * @param int $id |
|
119 | + * |
|
120 | + * @return array |
|
121 | + */ |
|
122 | + public function markThreadAsRead(int $id): array |
|
123 | + { |
|
124 | + return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id', (string)$id), |
|
125 | + Request::METHOD_PATCH); |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * Get a Thread Subscription |
|
130 | - * |
|
131 | - * @link https://developer.github.com/v3/activity/notifications/#get-a-thread-subscription |
|
132 | - * |
|
133 | - * @param int $id |
|
134 | - * |
|
135 | - * @return array |
|
136 | - */ |
|
137 | - public function getThreadSubscription(int $id): array |
|
138 | - { |
|
139 | - return $this->getApi()->request($this->getApi() |
|
140 | - ->sprintf('/notifications/threads/:id/subscription', (string)$id)); |
|
141 | - } |
|
128 | + /** |
|
129 | + * Get a Thread Subscription |
|
130 | + * |
|
131 | + * @link https://developer.github.com/v3/activity/notifications/#get-a-thread-subscription |
|
132 | + * |
|
133 | + * @param int $id |
|
134 | + * |
|
135 | + * @return array |
|
136 | + */ |
|
137 | + public function getThreadSubscription(int $id): array |
|
138 | + { |
|
139 | + return $this->getApi()->request($this->getApi() |
|
140 | + ->sprintf('/notifications/threads/:id/subscription', (string)$id)); |
|
141 | + } |
|
142 | 142 | |
143 | - /** |
|
144 | - * Set a Thread Subscription |
|
145 | - * |
|
146 | - * @link https://developer.github.com/v3/activity/notifications/#set-a-thread-subscription |
|
147 | - * |
|
148 | - * @param int $id |
|
149 | - * @param bool $subscribed |
|
150 | - * @param bool $ignored |
|
151 | - * |
|
152 | - * @return array |
|
153 | - */ |
|
154 | - public function setThreadSubscription(int $id, bool $subscribed = false, bool $ignored = false): array |
|
155 | - { |
|
156 | - return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id/subscription?:args', $id, |
|
157 | - http_build_query(['subscribed' => $subscribed, 'ignored' => $ignored])), Request::METHOD_PUT); |
|
158 | - } |
|
143 | + /** |
|
144 | + * Set a Thread Subscription |
|
145 | + * |
|
146 | + * @link https://developer.github.com/v3/activity/notifications/#set-a-thread-subscription |
|
147 | + * |
|
148 | + * @param int $id |
|
149 | + * @param bool $subscribed |
|
150 | + * @param bool $ignored |
|
151 | + * |
|
152 | + * @return array |
|
153 | + */ |
|
154 | + public function setThreadSubscription(int $id, bool $subscribed = false, bool $ignored = false): array |
|
155 | + { |
|
156 | + return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id/subscription?:args', $id, |
|
157 | + http_build_query(['subscribed' => $subscribed, 'ignored' => $ignored])), Request::METHOD_PUT); |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * Delete a Thread Subscription |
|
162 | - * |
|
163 | - * @link https://developer.github.com/v3/activity/notifications/#delete-a-thread-subscription |
|
164 | - * |
|
165 | - * @param int $id |
|
166 | - * |
|
167 | - * @return bool |
|
168 | - */ |
|
169 | - public function deleteThreadSubscription(int $id): bool |
|
170 | - { |
|
171 | - $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id/subscription', (string)$id), |
|
172 | - Request::METHOD_DELETE); |
|
160 | + /** |
|
161 | + * Delete a Thread Subscription |
|
162 | + * |
|
163 | + * @link https://developer.github.com/v3/activity/notifications/#delete-a-thread-subscription |
|
164 | + * |
|
165 | + * @param int $id |
|
166 | + * |
|
167 | + * @return bool |
|
168 | + */ |
|
169 | + public function deleteThreadSubscription(int $id): bool |
|
170 | + { |
|
171 | + $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id/subscription', (string)$id), |
|
172 | + Request::METHOD_DELETE); |
|
173 | 173 | |
174 | - if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
175 | - return true; |
|
176 | - } |
|
174 | + if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
175 | + return true; |
|
176 | + } |
|
177 | 177 | |
178 | - return false; |
|
179 | - } |
|
178 | + return false; |
|
179 | + } |
|
180 | 180 | } |
181 | 181 | \ No newline at end of file |
@@ -14,204 +14,204 @@ |
||
14 | 14 | */ |
15 | 15 | class Payload implements EventInterface |
16 | 16 | { |
17 | - /** @var WebHook */ |
|
18 | - protected $webHook; |
|
19 | - |
|
20 | - /** @var null|string */ |
|
21 | - protected $secret = null; |
|
22 | - |
|
23 | - /** @var string|resource */ |
|
24 | - protected $rawData; |
|
25 | - |
|
26 | - /** @var mixed */ |
|
27 | - protected $parsedData; |
|
28 | - |
|
29 | - /** @var ServerBag */ |
|
30 | - protected $serverBag; |
|
31 | - |
|
32 | - /** |
|
33 | - * Constructor, pass a WebHook object |
|
34 | - * |
|
35 | - * @param WebHook $webHook |
|
36 | - */ |
|
37 | - public function __construct(WebHook $webHook) |
|
38 | - { |
|
39 | - $this->setWebHook($webHook); |
|
40 | - $request = $webHook->getRequest(); |
|
41 | - $this->setRawData($request->getContent()); |
|
42 | - $this->serverBag = $request->server; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Get webHook |
|
47 | - * |
|
48 | - * @return null|WebHook |
|
49 | - */ |
|
50 | - public function getWebHook() |
|
51 | - { |
|
52 | - return $this->webHook; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Set webHook |
|
57 | - * |
|
58 | - * @param mixed $webHook |
|
59 | - * |
|
60 | - * @return Payload |
|
61 | - */ |
|
62 | - public function setWebHook($webHook): Payload |
|
63 | - { |
|
64 | - $this->webHook = $webHook; |
|
65 | - |
|
66 | - return $this; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Set secret, encode this secret with Hmac, SHA1 method |
|
71 | - * |
|
72 | - * @param string $secret |
|
73 | - * |
|
74 | - * @return Payload |
|
75 | - */ |
|
76 | - public function setSecret(string $secret): Payload |
|
77 | - { |
|
78 | - $this->secret = hash_hmac('sha1', $this->rawData, $secret); |
|
79 | - |
|
80 | - return $this; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Get secret |
|
85 | - * |
|
86 | - * @return null|string |
|
87 | - */ |
|
88 | - public function getSecret() |
|
89 | - { |
|
90 | - return $this->secret; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Get rawData |
|
95 | - * |
|
96 | - * @return resource|string |
|
97 | - */ |
|
98 | - public function getRawData() |
|
99 | - { |
|
100 | - return $this->rawData; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Set rawData |
|
105 | - * |
|
106 | - * @param resource|string $rawData |
|
107 | - * |
|
108 | - * @return Payload |
|
109 | - */ |
|
110 | - public function setRawData($rawData): Payload |
|
111 | - { |
|
112 | - $this->rawData = $rawData; |
|
113 | - |
|
114 | - return $this; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Get parsedData |
|
119 | - * |
|
120 | - * @return mixed |
|
121 | - */ |
|
122 | - public function getData() |
|
123 | - { |
|
124 | - return $this->parsedData; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Set parsedData |
|
129 | - * |
|
130 | - * @param mixed $parsedData |
|
131 | - * |
|
132 | - * @return Payload |
|
133 | - */ |
|
134 | - protected function setParsedData($parsedData): Payload |
|
135 | - { |
|
136 | - $data = json_decode($parsedData); |
|
137 | - if (JSON_ERROR_NONE === json_last_error()) { |
|
138 | - $this->parsedData = $data; |
|
139 | - } |
|
140 | - |
|
141 | - return $this; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Debugger |
|
146 | - * |
|
147 | - * @return array |
|
148 | - */ |
|
149 | - public function __debugInfo(): array |
|
150 | - { |
|
151 | - return [ |
|
152 | - 'ramData' => (array)$this->getRawData(), |
|
153 | - 'jsonEncoded' => json_decode($this->getRawData()) |
|
154 | - ]; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Parse raw data |
|
159 | - * |
|
160 | - * @return Payload |
|
161 | - * @throws BadSignatureException |
|
162 | - * @throws \Exception |
|
163 | - */ |
|
164 | - public function parse(): Payload |
|
165 | - { |
|
166 | - /** Check signature from header */ |
|
167 | - if (!$this->_checkSignature()) { |
|
168 | - throw new BadSignatureException('Hook secret does not match.'); |
|
169 | - } |
|
170 | - |
|
171 | - /** Get data from different locations according to content-type */ |
|
172 | - switch ($this->serverBag->get('CONTENT_TYPE')) { |
|
173 | - case 'application/json': |
|
174 | - $data = $this->getRawData(); |
|
175 | - break; |
|
176 | - |
|
177 | - case 'application/x-www-form-urlencoded': |
|
178 | - $data = $_POST['payload']; |
|
179 | - break; |
|
180 | - |
|
181 | - default: |
|
182 | - throw new Exception('Unsupported content type: "' . $this->serverBag->get('CONTENT_TYPE') . '"'); |
|
183 | - } |
|
184 | - $this->setParsedData($data); |
|
185 | - |
|
186 | - return $this; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Check X-Hub-Signature |
|
191 | - * |
|
192 | - * @throws BadSignatureException |
|
193 | - * @return bool |
|
194 | - */ |
|
195 | - private function _checkSignature(): bool |
|
196 | - { |
|
197 | - $secret = $this->getSecret(); |
|
198 | - $httpHubSignature = $this->serverBag->get('HTTP_X_HUB_SIGNATURE'); |
|
199 | - |
|
200 | - if (null !== $secret) { |
|
201 | - if ($httpHubSignature) { |
|
202 | - /** |
|
203 | - * Split signature into algorithm and hash |
|
204 | - * |
|
205 | - * @link http://isometriks.com/verify-github-webhooks-with-php |
|
206 | - */ |
|
207 | - list(, $hash) = explode('=', $httpHubSignature, 2); |
|
208 | - |
|
209 | - return $secret == $hash; |
|
210 | - } |
|
211 | - |
|
212 | - throw new BadSignatureException('HTTP header "X-Hub-Signature" is missing.'); |
|
213 | - } |
|
214 | - |
|
215 | - return true; |
|
216 | - } |
|
17 | + /** @var WebHook */ |
|
18 | + protected $webHook; |
|
19 | + |
|
20 | + /** @var null|string */ |
|
21 | + protected $secret = null; |
|
22 | + |
|
23 | + /** @var string|resource */ |
|
24 | + protected $rawData; |
|
25 | + |
|
26 | + /** @var mixed */ |
|
27 | + protected $parsedData; |
|
28 | + |
|
29 | + /** @var ServerBag */ |
|
30 | + protected $serverBag; |
|
31 | + |
|
32 | + /** |
|
33 | + * Constructor, pass a WebHook object |
|
34 | + * |
|
35 | + * @param WebHook $webHook |
|
36 | + */ |
|
37 | + public function __construct(WebHook $webHook) |
|
38 | + { |
|
39 | + $this->setWebHook($webHook); |
|
40 | + $request = $webHook->getRequest(); |
|
41 | + $this->setRawData($request->getContent()); |
|
42 | + $this->serverBag = $request->server; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Get webHook |
|
47 | + * |
|
48 | + * @return null|WebHook |
|
49 | + */ |
|
50 | + public function getWebHook() |
|
51 | + { |
|
52 | + return $this->webHook; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Set webHook |
|
57 | + * |
|
58 | + * @param mixed $webHook |
|
59 | + * |
|
60 | + * @return Payload |
|
61 | + */ |
|
62 | + public function setWebHook($webHook): Payload |
|
63 | + { |
|
64 | + $this->webHook = $webHook; |
|
65 | + |
|
66 | + return $this; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Set secret, encode this secret with Hmac, SHA1 method |
|
71 | + * |
|
72 | + * @param string $secret |
|
73 | + * |
|
74 | + * @return Payload |
|
75 | + */ |
|
76 | + public function setSecret(string $secret): Payload |
|
77 | + { |
|
78 | + $this->secret = hash_hmac('sha1', $this->rawData, $secret); |
|
79 | + |
|
80 | + return $this; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Get secret |
|
85 | + * |
|
86 | + * @return null|string |
|
87 | + */ |
|
88 | + public function getSecret() |
|
89 | + { |
|
90 | + return $this->secret; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Get rawData |
|
95 | + * |
|
96 | + * @return resource|string |
|
97 | + */ |
|
98 | + public function getRawData() |
|
99 | + { |
|
100 | + return $this->rawData; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Set rawData |
|
105 | + * |
|
106 | + * @param resource|string $rawData |
|
107 | + * |
|
108 | + * @return Payload |
|
109 | + */ |
|
110 | + public function setRawData($rawData): Payload |
|
111 | + { |
|
112 | + $this->rawData = $rawData; |
|
113 | + |
|
114 | + return $this; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Get parsedData |
|
119 | + * |
|
120 | + * @return mixed |
|
121 | + */ |
|
122 | + public function getData() |
|
123 | + { |
|
124 | + return $this->parsedData; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Set parsedData |
|
129 | + * |
|
130 | + * @param mixed $parsedData |
|
131 | + * |
|
132 | + * @return Payload |
|
133 | + */ |
|
134 | + protected function setParsedData($parsedData): Payload |
|
135 | + { |
|
136 | + $data = json_decode($parsedData); |
|
137 | + if (JSON_ERROR_NONE === json_last_error()) { |
|
138 | + $this->parsedData = $data; |
|
139 | + } |
|
140 | + |
|
141 | + return $this; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Debugger |
|
146 | + * |
|
147 | + * @return array |
|
148 | + */ |
|
149 | + public function __debugInfo(): array |
|
150 | + { |
|
151 | + return [ |
|
152 | + 'ramData' => (array)$this->getRawData(), |
|
153 | + 'jsonEncoded' => json_decode($this->getRawData()) |
|
154 | + ]; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Parse raw data |
|
159 | + * |
|
160 | + * @return Payload |
|
161 | + * @throws BadSignatureException |
|
162 | + * @throws \Exception |
|
163 | + */ |
|
164 | + public function parse(): Payload |
|
165 | + { |
|
166 | + /** Check signature from header */ |
|
167 | + if (!$this->_checkSignature()) { |
|
168 | + throw new BadSignatureException('Hook secret does not match.'); |
|
169 | + } |
|
170 | + |
|
171 | + /** Get data from different locations according to content-type */ |
|
172 | + switch ($this->serverBag->get('CONTENT_TYPE')) { |
|
173 | + case 'application/json': |
|
174 | + $data = $this->getRawData(); |
|
175 | + break; |
|
176 | + |
|
177 | + case 'application/x-www-form-urlencoded': |
|
178 | + $data = $_POST['payload']; |
|
179 | + break; |
|
180 | + |
|
181 | + default: |
|
182 | + throw new Exception('Unsupported content type: "' . $this->serverBag->get('CONTENT_TYPE') . '"'); |
|
183 | + } |
|
184 | + $this->setParsedData($data); |
|
185 | + |
|
186 | + return $this; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Check X-Hub-Signature |
|
191 | + * |
|
192 | + * @throws BadSignatureException |
|
193 | + * @return bool |
|
194 | + */ |
|
195 | + private function _checkSignature(): bool |
|
196 | + { |
|
197 | + $secret = $this->getSecret(); |
|
198 | + $httpHubSignature = $this->serverBag->get('HTTP_X_HUB_SIGNATURE'); |
|
199 | + |
|
200 | + if (null !== $secret) { |
|
201 | + if ($httpHubSignature) { |
|
202 | + /** |
|
203 | + * Split signature into algorithm and hash |
|
204 | + * |
|
205 | + * @link http://isometriks.com/verify-github-webhooks-with-php |
|
206 | + */ |
|
207 | + list(, $hash) = explode('=', $httpHubSignature, 2); |
|
208 | + |
|
209 | + return $secret == $hash; |
|
210 | + } |
|
211 | + |
|
212 | + throw new BadSignatureException('HTTP header "X-Hub-Signature" is missing.'); |
|
213 | + } |
|
214 | + |
|
215 | + return true; |
|
216 | + } |
|
217 | 217 | } |
218 | 218 | \ No newline at end of file |
@@ -12,62 +12,62 @@ |
||
12 | 12 | class Commits extends AbstractRepositories |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * List commits on a repository |
|
17 | - * |
|
18 | - * @link https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository |
|
19 | - * |
|
20 | - * @param string $sha |
|
21 | - * @param string|null $path |
|
22 | - * @param string|null $author |
|
23 | - * @param string|null $since |
|
24 | - * @param string|null $until |
|
25 | - * |
|
26 | - * @return array |
|
27 | - */ |
|
28 | - public function listCommits(string $sha = AbstractApi::BRANCH_MASTER, string $path = null, string $author = null, |
|
29 | - string $since = null, string $until = null, $page = null, $per_page = null): array |
|
30 | - { |
|
31 | - return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/commits?:args', |
|
32 | - $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), http_build_query([ |
|
33 | - "sha" => $sha, |
|
34 | - "path" => $path, |
|
35 | - "author" => $author, |
|
36 | - "since" => $since, |
|
37 | - "until" => $until, |
|
15 | + /** |
|
16 | + * List commits on a repository |
|
17 | + * |
|
18 | + * @link https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository |
|
19 | + * |
|
20 | + * @param string $sha |
|
21 | + * @param string|null $path |
|
22 | + * @param string|null $author |
|
23 | + * @param string|null $since |
|
24 | + * @param string|null $until |
|
25 | + * |
|
26 | + * @return array |
|
27 | + */ |
|
28 | + public function listCommits(string $sha = AbstractApi::BRANCH_MASTER, string $path = null, string $author = null, |
|
29 | + string $since = null, string $until = null, $page = null, $per_page = null): array |
|
30 | + { |
|
31 | + return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/commits?:args', |
|
32 | + $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), http_build_query([ |
|
33 | + "sha" => $sha, |
|
34 | + "path" => $path, |
|
35 | + "author" => $author, |
|
36 | + "since" => $since, |
|
37 | + "until" => $until, |
|
38 | 38 | "page" => $page, |
39 | 39 | "per_page" => $per_page |
40 | - ]))); |
|
41 | - } |
|
40 | + ]))); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Get a single commit |
|
45 | - * |
|
46 | - * @link https://developer.github.com/v3/repos/commits/#get-a-single-commit |
|
47 | - * |
|
48 | - * @param string $sha |
|
49 | - * |
|
50 | - * @return array |
|
51 | - */ |
|
52 | - public function getSingleCommit(string $sha): array |
|
53 | - { |
|
54 | - return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/commits/:sha', |
|
55 | - $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $sha)); |
|
56 | - } |
|
43 | + /** |
|
44 | + * Get a single commit |
|
45 | + * |
|
46 | + * @link https://developer.github.com/v3/repos/commits/#get-a-single-commit |
|
47 | + * |
|
48 | + * @param string $sha |
|
49 | + * |
|
50 | + * @return array |
|
51 | + */ |
|
52 | + public function getSingleCommit(string $sha): array |
|
53 | + { |
|
54 | + return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/commits/:sha', |
|
55 | + $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $sha)); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Compare two commits |
|
60 | - * |
|
61 | - * @link https://developer.github.com/v3/repos/commits/#compare-two-commits |
|
62 | - * |
|
63 | - * @param string $base |
|
64 | - * @param string $head |
|
65 | - * |
|
66 | - * @return array |
|
67 | - */ |
|
68 | - public function compareTwoCommits(string $base, string $head): array |
|
69 | - { |
|
70 | - return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/compare/:base...:head', |
|
71 | - $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $base, $head)); |
|
72 | - } |
|
58 | + /** |
|
59 | + * Compare two commits |
|
60 | + * |
|
61 | + * @link https://developer.github.com/v3/repos/commits/#compare-two-commits |
|
62 | + * |
|
63 | + * @param string $base |
|
64 | + * @param string $head |
|
65 | + * |
|
66 | + * @return array |
|
67 | + */ |
|
68 | + public function compareTwoCommits(string $base, string $head): array |
|
69 | + { |
|
70 | + return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/compare/:base...:head', |
|
71 | + $this->getRepositories()->getOwner(), $this->getRepositories()->getRepo(), $base, $head)); |
|
72 | + } |
|
73 | 73 | } |