@@ -14,176 +14,176 @@ |
||
14 | 14 | class PullRequests extends AbstractReceiver |
15 | 15 | { |
16 | 16 | |
17 | - /** Available sub-Receiver */ |
|
18 | - const REVIEW_COMMENTS = 'ReviewComments'; |
|
17 | + /** Available sub-Receiver */ |
|
18 | + const REVIEW_COMMENTS = 'ReviewComments'; |
|
19 | 19 | |
20 | - /** |
|
21 | - * List pull requests |
|
22 | - * |
|
23 | - * @link https://developer.github.com/v3/pulls/#list-pull-requests |
|
24 | - * |
|
25 | - * @param string $state |
|
26 | - * @param string $head |
|
27 | - * @param string $base |
|
28 | - * @param string $sort |
|
29 | - * @param string $direction |
|
30 | - * |
|
31 | - * @return array |
|
32 | - * @throws \Exception |
|
33 | - */ |
|
34 | - public function listPullRequests(string $state = AbstractApi::STATE_OPEN, string $head = null, string $base = null, |
|
35 | - string $sort = AbstractApi::SORT_CREATED, |
|
36 | - string $direction = AbstractApi::DIRECTION_ASC): array |
|
37 | - { |
|
38 | - return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls?:args', $this->getOwner(), |
|
39 | - $this->getRepo(), http_build_query([ |
|
40 | - 'state' => $state, |
|
41 | - 'head' => $head, |
|
42 | - 'base' => $base, |
|
43 | - 'sort' => $sort, |
|
44 | - 'direction' => $direction |
|
45 | - ]))); |
|
46 | - } |
|
20 | + /** |
|
21 | + * List pull requests |
|
22 | + * |
|
23 | + * @link https://developer.github.com/v3/pulls/#list-pull-requests |
|
24 | + * |
|
25 | + * @param string $state |
|
26 | + * @param string $head |
|
27 | + * @param string $base |
|
28 | + * @param string $sort |
|
29 | + * @param string $direction |
|
30 | + * |
|
31 | + * @return array |
|
32 | + * @throws \Exception |
|
33 | + */ |
|
34 | + public function listPullRequests(string $state = AbstractApi::STATE_OPEN, string $head = null, string $base = null, |
|
35 | + string $sort = AbstractApi::SORT_CREATED, |
|
36 | + string $direction = AbstractApi::DIRECTION_ASC): array |
|
37 | + { |
|
38 | + return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls?:args', $this->getOwner(), |
|
39 | + $this->getRepo(), http_build_query([ |
|
40 | + 'state' => $state, |
|
41 | + 'head' => $head, |
|
42 | + 'base' => $base, |
|
43 | + 'sort' => $sort, |
|
44 | + 'direction' => $direction |
|
45 | + ]))); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Get a single pull request |
|
50 | - * |
|
51 | - * @link https://developer.github.com/v3/pulls/#get-a-single-pull-request |
|
52 | - * |
|
53 | - * @param int $number |
|
54 | - * |
|
55 | - * @return array |
|
56 | - * @throws \Exception |
|
57 | - */ |
|
58 | - public function getSinglePullRequest(int $number): array |
|
59 | - { |
|
60 | - return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/:number', $this->getOwner(), |
|
61 | - $this->getRepo(), $number)); |
|
62 | - } |
|
48 | + /** |
|
49 | + * Get a single pull request |
|
50 | + * |
|
51 | + * @link https://developer.github.com/v3/pulls/#get-a-single-pull-request |
|
52 | + * |
|
53 | + * @param int $number |
|
54 | + * |
|
55 | + * @return array |
|
56 | + * @throws \Exception |
|
57 | + */ |
|
58 | + public function getSinglePullRequest(int $number): array |
|
59 | + { |
|
60 | + return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/:number', $this->getOwner(), |
|
61 | + $this->getRepo(), $number)); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Create a pull request |
|
66 | - * |
|
67 | - * @link https://developer.github.com/v3/pulls/#create-a-pull-request |
|
68 | - * |
|
69 | - * @param string $title |
|
70 | - * @param string $head |
|
71 | - * @param string $base |
|
72 | - * @param string $body |
|
73 | - * |
|
74 | - * @return array |
|
75 | - * @throws \Exception |
|
76 | - */ |
|
77 | - public function createPullrequest(string $title, string $head, string $base, string $body): array |
|
78 | - { |
|
79 | - return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls', $this->getOwner(), |
|
80 | - $this->getRepo()), Request::METHOD_POST, [ |
|
81 | - 'title' => $title, |
|
82 | - 'head' => $head, |
|
83 | - 'base' => $base, |
|
84 | - 'body' => $body |
|
85 | - ]); |
|
86 | - } |
|
64 | + /** |
|
65 | + * Create a pull request |
|
66 | + * |
|
67 | + * @link https://developer.github.com/v3/pulls/#create-a-pull-request |
|
68 | + * |
|
69 | + * @param string $title |
|
70 | + * @param string $head |
|
71 | + * @param string $base |
|
72 | + * @param string $body |
|
73 | + * |
|
74 | + * @return array |
|
75 | + * @throws \Exception |
|
76 | + */ |
|
77 | + public function createPullrequest(string $title, string $head, string $base, string $body): array |
|
78 | + { |
|
79 | + return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls', $this->getOwner(), |
|
80 | + $this->getRepo()), Request::METHOD_POST, [ |
|
81 | + 'title' => $title, |
|
82 | + 'head' => $head, |
|
83 | + 'base' => $base, |
|
84 | + 'body' => $body |
|
85 | + ]); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Update a pull request |
|
90 | - * |
|
91 | - * @link https://developer.github.com/v3/pulls/#update-a-pull-request |
|
92 | - * |
|
93 | - * @param int $number |
|
94 | - * @param string $title |
|
95 | - * @param string $body |
|
96 | - * @param string $state |
|
97 | - * |
|
98 | - * @return array |
|
99 | - * @throws \Exception |
|
100 | - */ |
|
101 | - public function updatePullRequest(int $number, string $title = null, string $body = null, |
|
102 | - string $state = null): array |
|
103 | - { |
|
104 | - return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/:number', $this->getOwner(), |
|
105 | - $this->getRepo(), $number), Request::METHOD_PATCH, [ |
|
106 | - 'title' => $title, |
|
107 | - 'body' => $body, |
|
108 | - 'state' => $state |
|
109 | - ]); |
|
110 | - } |
|
88 | + /** |
|
89 | + * Update a pull request |
|
90 | + * |
|
91 | + * @link https://developer.github.com/v3/pulls/#update-a-pull-request |
|
92 | + * |
|
93 | + * @param int $number |
|
94 | + * @param string $title |
|
95 | + * @param string $body |
|
96 | + * @param string $state |
|
97 | + * |
|
98 | + * @return array |
|
99 | + * @throws \Exception |
|
100 | + */ |
|
101 | + public function updatePullRequest(int $number, string $title = null, string $body = null, |
|
102 | + string $state = null): array |
|
103 | + { |
|
104 | + return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/:number', $this->getOwner(), |
|
105 | + $this->getRepo(), $number), Request::METHOD_PATCH, [ |
|
106 | + 'title' => $title, |
|
107 | + 'body' => $body, |
|
108 | + 'state' => $state |
|
109 | + ]); |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * List commits on a pull request |
|
114 | - * |
|
115 | - * @link https://developer.github.com/v3/pulls/#list-commits-on-a-pull-request |
|
116 | - * |
|
117 | - * @param int $number |
|
118 | - * |
|
119 | - * @return array |
|
120 | - * @throws \Exception |
|
121 | - */ |
|
122 | - public function listCommits(int $number): array |
|
123 | - { |
|
124 | - return $this->getApi()->request($this->getApi() |
|
125 | - ->sprintf('/repos/:owner/:repo/pulls/:number/commits', $this->getOwner(), |
|
126 | - $this->getRepo(), $number)); |
|
127 | - } |
|
112 | + /** |
|
113 | + * List commits on a pull request |
|
114 | + * |
|
115 | + * @link https://developer.github.com/v3/pulls/#list-commits-on-a-pull-request |
|
116 | + * |
|
117 | + * @param int $number |
|
118 | + * |
|
119 | + * @return array |
|
120 | + * @throws \Exception |
|
121 | + */ |
|
122 | + public function listCommits(int $number): array |
|
123 | + { |
|
124 | + return $this->getApi()->request($this->getApi() |
|
125 | + ->sprintf('/repos/:owner/:repo/pulls/:number/commits', $this->getOwner(), |
|
126 | + $this->getRepo(), $number)); |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * List pull requests files |
|
131 | - * |
|
132 | - * @link https://developer.github.com/v3/pulls/#list-pull-requests-files |
|
133 | - * |
|
134 | - * @param int $number |
|
135 | - * |
|
136 | - * @return array |
|
137 | - * @throws \Exception |
|
138 | - */ |
|
139 | - public function listPullRequestsFiles(int $number): array |
|
140 | - { |
|
141 | - return $this->getApi()->request($this->getApi() |
|
142 | - ->sprintf('/repos/:owner/:repo/pulls/:number/files', $this->getOwner(), |
|
143 | - $this->getRepo(), $number)); |
|
144 | - } |
|
129 | + /** |
|
130 | + * List pull requests files |
|
131 | + * |
|
132 | + * @link https://developer.github.com/v3/pulls/#list-pull-requests-files |
|
133 | + * |
|
134 | + * @param int $number |
|
135 | + * |
|
136 | + * @return array |
|
137 | + * @throws \Exception |
|
138 | + */ |
|
139 | + public function listPullRequestsFiles(int $number): array |
|
140 | + { |
|
141 | + return $this->getApi()->request($this->getApi() |
|
142 | + ->sprintf('/repos/:owner/:repo/pulls/:number/files', $this->getOwner(), |
|
143 | + $this->getRepo(), $number)); |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * Get if a pull request has been merged |
|
148 | - * |
|
149 | - * @link https://developer.github.com/v3/pulls/#get-if-a-pull-request-has-been-merged |
|
150 | - * |
|
151 | - * @param int $number |
|
152 | - * |
|
153 | - * @return bool |
|
154 | - * @throws \Exception |
|
155 | - */ |
|
156 | - public function checkPullRequestsMerged(int $number): bool |
|
157 | - { |
|
158 | - $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/:number/merge', $this->getOwner(), |
|
159 | - $this->getRepo(), $number)); |
|
146 | + /** |
|
147 | + * Get if a pull request has been merged |
|
148 | + * |
|
149 | + * @link https://developer.github.com/v3/pulls/#get-if-a-pull-request-has-been-merged |
|
150 | + * |
|
151 | + * @param int $number |
|
152 | + * |
|
153 | + * @return bool |
|
154 | + * @throws \Exception |
|
155 | + */ |
|
156 | + public function checkPullRequestsMerged(int $number): bool |
|
157 | + { |
|
158 | + $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/:number/merge', $this->getOwner(), |
|
159 | + $this->getRepo(), $number)); |
|
160 | 160 | |
161 | - if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
162 | - return true; |
|
163 | - } |
|
161 | + if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
|
162 | + return true; |
|
163 | + } |
|
164 | 164 | |
165 | - return false; |
|
166 | - } |
|
165 | + return false; |
|
166 | + } |
|
167 | 167 | |
168 | - /** |
|
169 | - * Merge a pull request (Merge Button) |
|
170 | - * |
|
171 | - * @link https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button |
|
172 | - * |
|
173 | - * @param int $number |
|
174 | - * @param string $commitMessage |
|
175 | - * @param string $sha |
|
176 | - * |
|
177 | - * @return array |
|
178 | - * @throws \Exception |
|
179 | - */ |
|
180 | - public function mergePullRequest(int $number, string $commitMessage = null, string $sha = null): array |
|
181 | - { |
|
182 | - return $this->getApi()->request($this->getApi() |
|
183 | - ->sprintf('/repos/:owner/:repo/pulls/:number/merge', $this->getOwner(), |
|
184 | - $this->getRepo(), $number), Request::METHOD_PUT, [ |
|
185 | - 'commit_message' => $commitMessage, |
|
186 | - 'sha' => $sha |
|
187 | - ]); |
|
188 | - } |
|
168 | + /** |
|
169 | + * Merge a pull request (Merge Button) |
|
170 | + * |
|
171 | + * @link https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button |
|
172 | + * |
|
173 | + * @param int $number |
|
174 | + * @param string $commitMessage |
|
175 | + * @param string $sha |
|
176 | + * |
|
177 | + * @return array |
|
178 | + * @throws \Exception |
|
179 | + */ |
|
180 | + public function mergePullRequest(int $number, string $commitMessage = null, string $sha = null): array |
|
181 | + { |
|
182 | + return $this->getApi()->request($this->getApi() |
|
183 | + ->sprintf('/repos/:owner/:repo/pulls/:number/merge', $this->getOwner(), |
|
184 | + $this->getRepo(), $number), Request::METHOD_PUT, [ |
|
185 | + 'commit_message' => $commitMessage, |
|
186 | + 'sha' => $sha |
|
187 | + ]); |
|
188 | + } |
|
189 | 189 | } |
190 | 190 | \ No newline at end of file |
@@ -12,24 +12,24 @@ |
||
12 | 12 | class WebHook extends AbstractApi |
13 | 13 | { |
14 | 14 | |
15 | - /** Constants */ |
|
16 | - const PAYLOAD = 'Payload'; |
|
15 | + /** Constants */ |
|
16 | + const PAYLOAD = 'Payload'; |
|
17 | 17 | |
18 | - /** |
|
19 | - * Returns Event object |
|
20 | - * |
|
21 | - * @param string $event |
|
22 | - * |
|
23 | - * @return null|EventInterface |
|
24 | - */ |
|
25 | - public function getEvent(string $event) |
|
26 | - { |
|
27 | - $class = (string)$this->sprintf(':namespace\Event\:event', __NAMESPACE__, $event); |
|
18 | + /** |
|
19 | + * Returns Event object |
|
20 | + * |
|
21 | + * @param string $event |
|
22 | + * |
|
23 | + * @return null|EventInterface |
|
24 | + */ |
|
25 | + public function getEvent(string $event) |
|
26 | + { |
|
27 | + $class = (string)$this->sprintf(':namespace\Event\:event', __NAMESPACE__, $event); |
|
28 | 28 | |
29 | - if (class_exists($class)) { |
|
30 | - return new $class($this); |
|
31 | - } |
|
29 | + if (class_exists($class)) { |
|
30 | + return new $class($this); |
|
31 | + } |
|
32 | 32 | |
33 | - return null; |
|
34 | - } |
|
33 | + return null; |
|
34 | + } |
|
35 | 35 | } |
36 | 36 | \ No newline at end of file |
@@ -11,17 +11,17 @@ |
||
11 | 11 | interface EventInterface |
12 | 12 | { |
13 | 13 | |
14 | - /** |
|
15 | - * Constructor, pass a WebHook object |
|
16 | - * |
|
17 | - * @param WebHook $webHook |
|
18 | - */ |
|
19 | - public function __construct(WebHook $webHook); |
|
14 | + /** |
|
15 | + * Constructor, pass a WebHook object |
|
16 | + * |
|
17 | + * @param WebHook $webHook |
|
18 | + */ |
|
19 | + public function __construct(WebHook $webHook); |
|
20 | 20 | |
21 | - /** |
|
22 | - * Parse raw data |
|
23 | - * |
|
24 | - * @return Payload |
|
25 | - */ |
|
26 | - public function parse(): Payload; |
|
21 | + /** |
|
22 | + * Parse raw data |
|
23 | + * |
|
24 | + * @return Payload |
|
25 | + */ |
|
26 | + public function parse(): Payload; |
|
27 | 27 | } |
28 | 28 | \ No newline at end of file |
@@ -13,190 +13,190 @@ |
||
13 | 13 | class Payload implements EventInterface |
14 | 14 | { |
15 | 15 | |
16 | - /** Protected properties */ |
|
17 | - protected $webHook; |
|
18 | - protected $secret = null; |
|
19 | - protected $rawData; |
|
20 | - protected $parsedData; |
|
21 | - |
|
22 | - /** |
|
23 | - * Constructor, pass a WebHook object |
|
24 | - * |
|
25 | - * @param WebHook $webHook |
|
26 | - */ |
|
27 | - public function __construct(WebHook $webHook) |
|
28 | - { |
|
29 | - $this->setWebHook($webHook); |
|
30 | - $this->setRawData($webHook->getRequest()->getContent()); |
|
31 | - } |
|
32 | - |
|
33 | - /** |
|
34 | - * Get webHook |
|
35 | - * |
|
36 | - * @return null|WebHook |
|
37 | - */ |
|
38 | - public function getWebHook() |
|
39 | - { |
|
40 | - return $this->webHook; |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Set webHook |
|
45 | - * |
|
46 | - * @param mixed $webHook |
|
47 | - * |
|
48 | - * @return Payload |
|
49 | - */ |
|
50 | - public function setWebHook($webHook): Payload |
|
51 | - { |
|
52 | - $this->webHook = $webHook; |
|
53 | - |
|
54 | - return $this; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Set secret, encode this secret with Hmac, SHA1 method |
|
59 | - * |
|
60 | - * @param string $secret |
|
61 | - * |
|
62 | - * @return Payload |
|
63 | - */ |
|
64 | - public function setSecret(string $secret): Payload |
|
65 | - { |
|
66 | - $this->secret = hash_hmac('sha1', $this->rawData, $secret); |
|
67 | - |
|
68 | - return $this; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Get secret |
|
73 | - * |
|
74 | - * @return null|string |
|
75 | - */ |
|
76 | - public function getSecret() |
|
77 | - { |
|
78 | - return $this->secret; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Get rawData |
|
83 | - * |
|
84 | - * @return resource|string |
|
85 | - */ |
|
86 | - public function getRawData() |
|
87 | - { |
|
88 | - return $this->rawData; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Set rawData |
|
93 | - * |
|
94 | - * @param resource|string $rawData |
|
95 | - * |
|
96 | - * @return Payload |
|
97 | - */ |
|
98 | - public function setRawData($rawData): Payload |
|
99 | - { |
|
100 | - $this->rawData = $rawData; |
|
101 | - |
|
102 | - return $this; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Get parsedData |
|
107 | - * |
|
108 | - * @return mixed |
|
109 | - */ |
|
110 | - public function getData() |
|
111 | - { |
|
112 | - return $this->parsedData; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Set parsedData |
|
117 | - * |
|
118 | - * @param mixed $parsedData |
|
119 | - * |
|
120 | - * @return Payload |
|
121 | - */ |
|
122 | - protected function setParsedData($parsedData): Payload |
|
123 | - { |
|
124 | - $data = json_decode($parsedData); |
|
125 | - if (JSON_ERROR_NONE === json_last_error()) { |
|
126 | - $this->parsedData = $data; |
|
127 | - } |
|
128 | - |
|
129 | - return $this; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Debugger |
|
134 | - * |
|
135 | - * @return array |
|
136 | - */ |
|
137 | - public function __debugInfo(): array |
|
138 | - { |
|
139 | - return [ |
|
140 | - 'ramData' => (array)$this->getRawData(), |
|
141 | - 'jsonEncoded' => json_decode($this->getRawData()) |
|
142 | - ]; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Parse raw data |
|
147 | - * |
|
148 | - * @return Payload |
|
149 | - * @throws BadSignatureException |
|
150 | - * @throws \Exception |
|
151 | - */ |
|
152 | - public function parse(): Payload |
|
153 | - { |
|
154 | - /** Check signature from header */ |
|
155 | - if (!$this->_checkSignature()) { |
|
156 | - throw new BadSignatureException('Hook secret does not match.'); |
|
157 | - } |
|
158 | - |
|
159 | - /** Get data from different locations according to content-type */ |
|
160 | - switch ($_SERVER['CONTENT_TYPE']) { |
|
161 | - case 'application/json': |
|
162 | - $data = $this->getRawData(); |
|
163 | - break; |
|
164 | - |
|
165 | - case 'application/x-www-form-urlencoded': |
|
166 | - $data = $_POST['payload']; |
|
167 | - break; |
|
168 | - |
|
169 | - default: |
|
170 | - throw new \Exception('Unsupported content type: "' . $_SERVER['CONTENT_TYPE'] . '"'); |
|
171 | - } |
|
172 | - $this->setParsedData($data); |
|
173 | - |
|
174 | - return $this; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Check X-Hub-Signature |
|
179 | - * |
|
180 | - * @throws BadSignatureException |
|
181 | - * @return bool |
|
182 | - */ |
|
183 | - private function _checkSignature(): bool |
|
184 | - { |
|
185 | - if (null !== $this->secret) { |
|
186 | - if ($this->getWebHook()->getRequest()->server->get('HTTP_X_HUB_SIGNATURE')) { |
|
187 | - /** |
|
188 | - * Split signature into algorithm and hash |
|
189 | - * |
|
190 | - * @link http://isometriks.com/verify-github-webhooks-with-php |
|
191 | - */ |
|
192 | - list(, $hash) = explode('=', $this->getWebHook()->getRequest()->server->get('HTTP_X_HUB_SIGNATURE'), 2); |
|
193 | - |
|
194 | - return $this->secret == $hash; |
|
195 | - } |
|
196 | - |
|
197 | - throw new BadSignatureException('HTTP header "X-Hub-Signature" is missing.'); |
|
198 | - } |
|
199 | - |
|
200 | - return true; |
|
201 | - } |
|
16 | + /** Protected properties */ |
|
17 | + protected $webHook; |
|
18 | + protected $secret = null; |
|
19 | + protected $rawData; |
|
20 | + protected $parsedData; |
|
21 | + |
|
22 | + /** |
|
23 | + * Constructor, pass a WebHook object |
|
24 | + * |
|
25 | + * @param WebHook $webHook |
|
26 | + */ |
|
27 | + public function __construct(WebHook $webHook) |
|
28 | + { |
|
29 | + $this->setWebHook($webHook); |
|
30 | + $this->setRawData($webHook->getRequest()->getContent()); |
|
31 | + } |
|
32 | + |
|
33 | + /** |
|
34 | + * Get webHook |
|
35 | + * |
|
36 | + * @return null|WebHook |
|
37 | + */ |
|
38 | + public function getWebHook() |
|
39 | + { |
|
40 | + return $this->webHook; |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Set webHook |
|
45 | + * |
|
46 | + * @param mixed $webHook |
|
47 | + * |
|
48 | + * @return Payload |
|
49 | + */ |
|
50 | + public function setWebHook($webHook): Payload |
|
51 | + { |
|
52 | + $this->webHook = $webHook; |
|
53 | + |
|
54 | + return $this; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Set secret, encode this secret with Hmac, SHA1 method |
|
59 | + * |
|
60 | + * @param string $secret |
|
61 | + * |
|
62 | + * @return Payload |
|
63 | + */ |
|
64 | + public function setSecret(string $secret): Payload |
|
65 | + { |
|
66 | + $this->secret = hash_hmac('sha1', $this->rawData, $secret); |
|
67 | + |
|
68 | + return $this; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Get secret |
|
73 | + * |
|
74 | + * @return null|string |
|
75 | + */ |
|
76 | + public function getSecret() |
|
77 | + { |
|
78 | + return $this->secret; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Get rawData |
|
83 | + * |
|
84 | + * @return resource|string |
|
85 | + */ |
|
86 | + public function getRawData() |
|
87 | + { |
|
88 | + return $this->rawData; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Set rawData |
|
93 | + * |
|
94 | + * @param resource|string $rawData |
|
95 | + * |
|
96 | + * @return Payload |
|
97 | + */ |
|
98 | + public function setRawData($rawData): Payload |
|
99 | + { |
|
100 | + $this->rawData = $rawData; |
|
101 | + |
|
102 | + return $this; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Get parsedData |
|
107 | + * |
|
108 | + * @return mixed |
|
109 | + */ |
|
110 | + public function getData() |
|
111 | + { |
|
112 | + return $this->parsedData; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Set parsedData |
|
117 | + * |
|
118 | + * @param mixed $parsedData |
|
119 | + * |
|
120 | + * @return Payload |
|
121 | + */ |
|
122 | + protected function setParsedData($parsedData): Payload |
|
123 | + { |
|
124 | + $data = json_decode($parsedData); |
|
125 | + if (JSON_ERROR_NONE === json_last_error()) { |
|
126 | + $this->parsedData = $data; |
|
127 | + } |
|
128 | + |
|
129 | + return $this; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Debugger |
|
134 | + * |
|
135 | + * @return array |
|
136 | + */ |
|
137 | + public function __debugInfo(): array |
|
138 | + { |
|
139 | + return [ |
|
140 | + 'ramData' => (array)$this->getRawData(), |
|
141 | + 'jsonEncoded' => json_decode($this->getRawData()) |
|
142 | + ]; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Parse raw data |
|
147 | + * |
|
148 | + * @return Payload |
|
149 | + * @throws BadSignatureException |
|
150 | + * @throws \Exception |
|
151 | + */ |
|
152 | + public function parse(): Payload |
|
153 | + { |
|
154 | + /** Check signature from header */ |
|
155 | + if (!$this->_checkSignature()) { |
|
156 | + throw new BadSignatureException('Hook secret does not match.'); |
|
157 | + } |
|
158 | + |
|
159 | + /** Get data from different locations according to content-type */ |
|
160 | + switch ($_SERVER['CONTENT_TYPE']) { |
|
161 | + case 'application/json': |
|
162 | + $data = $this->getRawData(); |
|
163 | + break; |
|
164 | + |
|
165 | + case 'application/x-www-form-urlencoded': |
|
166 | + $data = $_POST['payload']; |
|
167 | + break; |
|
168 | + |
|
169 | + default: |
|
170 | + throw new \Exception('Unsupported content type: "' . $_SERVER['CONTENT_TYPE'] . '"'); |
|
171 | + } |
|
172 | + $this->setParsedData($data); |
|
173 | + |
|
174 | + return $this; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Check X-Hub-Signature |
|
179 | + * |
|
180 | + * @throws BadSignatureException |
|
181 | + * @return bool |
|
182 | + */ |
|
183 | + private function _checkSignature(): bool |
|
184 | + { |
|
185 | + if (null !== $this->secret) { |
|
186 | + if ($this->getWebHook()->getRequest()->server->get('HTTP_X_HUB_SIGNATURE')) { |
|
187 | + /** |
|
188 | + * Split signature into algorithm and hash |
|
189 | + * |
|
190 | + * @link http://isometriks.com/verify-github-webhooks-with-php |
|
191 | + */ |
|
192 | + list(, $hash) = explode('=', $this->getWebHook()->getRequest()->server->get('HTTP_X_HUB_SIGNATURE'), 2); |
|
193 | + |
|
194 | + return $this->secret == $hash; |
|
195 | + } |
|
196 | + |
|
197 | + throw new BadSignatureException('HTTP header "X-Hub-Signature" is missing.'); |
|
198 | + } |
|
199 | + |
|
200 | + return true; |
|
201 | + } |
|
202 | 202 | } |
203 | 203 | \ No newline at end of file |