|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (C) 2013 Mailgun |
|
5
|
|
|
* |
|
6
|
|
|
* This software may be modified and distributed under the terms |
|
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Mailgun\Api; |
|
11
|
|
|
|
|
12
|
|
|
use Http\Client\HttpClient; |
|
13
|
|
|
use Mailgun\Assert; |
|
14
|
|
|
use Mailgun\Hydrator\Hydrator; |
|
15
|
|
|
use Mailgun\Model\Webhook\CreateResponse; |
|
16
|
|
|
use Mailgun\Model\Webhook\DeleteResponse; |
|
17
|
|
|
use Mailgun\Model\Webhook\IndexResponse; |
|
18
|
|
|
use Mailgun\Model\Webhook\ShowResponse; |
|
19
|
|
|
use Mailgun\Model\Webhook\UpdateResponse; |
|
20
|
|
|
use Mailgun\RequestBuilder; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class Webhook extends HttpApi |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $apiKey; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param HttpClient $httpClient |
|
34
|
|
|
* @param RequestBuilder $requestBuilder |
|
35
|
|
|
* @param Hydrator $hydrator |
|
36
|
|
|
* @param string $apiKey |
|
37
|
|
|
*/ |
|
38
|
8 |
|
public function __construct(HttpClient $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator, $apiKey) |
|
39
|
|
|
{ |
|
40
|
8 |
|
parent::__construct($httpClient, $requestBuilder, $hydrator); |
|
41
|
8 |
|
$this->apiKey = $apiKey; |
|
42
|
8 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* This function verifies the webhook signature with your API key to to see if it is authentic. |
|
46
|
|
|
* |
|
47
|
|
|
* If this function returns FALSE, you must not process the request. |
|
48
|
|
|
* You should reject the request with status code 403 Forbidden. |
|
49
|
|
|
* |
|
50
|
|
|
* @param int $timestamp |
|
51
|
|
|
* @param string $token |
|
52
|
|
|
* @param string $signature |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public function verifyWebhookSignature($timestamp, $token, $signature) |
|
57
|
|
|
{ |
|
58
|
3 |
|
if (empty($timestamp) || empty($token) || empty($signature)) { |
|
59
|
1 |
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
$hmac = hash_hmac('sha256', $timestamp.$token, $this->apiKey); |
|
63
|
|
|
|
|
64
|
2 |
|
if (function_exists('hash_equals')) { |
|
65
|
|
|
// hash_equals is constant time, but will not be introduced until PHP 5.6 |
|
66
|
|
|
return hash_equals($hmac, $signature); |
|
67
|
|
|
} else { |
|
68
|
2 |
|
return $hmac === $signature; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $domain |
|
74
|
|
|
* |
|
75
|
|
|
* @return IndexResponse |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function index($domain) |
|
78
|
|
|
{ |
|
79
|
1 |
|
Assert::notEmpty($domain); |
|
80
|
1 |
|
$response = $this->httpGet(sprintf('/v3/domains/%s/webhooks', $domain)); |
|
81
|
|
|
|
|
82
|
1 |
|
return $this->hydrateResponse($response, IndexResponse::class); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $domain |
|
87
|
|
|
* @param string $webhook |
|
88
|
|
|
* |
|
89
|
|
|
* @return ShowResponse |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function show($domain, $webhook) |
|
92
|
|
|
{ |
|
93
|
1 |
|
Assert::notEmpty($domain); |
|
94
|
1 |
|
Assert::notEmpty($webhook); |
|
95
|
1 |
|
$response = $this->httpGet(sprintf('/v3/domains/%s/webhooks/%s', $domain, $webhook)); |
|
96
|
|
|
|
|
97
|
1 |
|
return $this->hydrateResponse($response, ShowResponse::class); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $domain |
|
102
|
|
|
* @param string $id |
|
103
|
|
|
* @param string $url |
|
104
|
|
|
* |
|
105
|
|
|
* @return CreateResponse |
|
106
|
|
|
*/ |
|
107
|
1 |
View Code Duplication |
public function create($domain, $id, $url) |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
1 |
|
Assert::notEmpty($domain); |
|
110
|
1 |
|
Assert::notEmpty($id); |
|
111
|
1 |
|
Assert::notEmpty($url); |
|
112
|
|
|
|
|
113
|
|
|
$params = [ |
|
114
|
1 |
|
'id' => $id, |
|
115
|
1 |
|
'url' => $url, |
|
116
|
1 |
|
]; |
|
117
|
|
|
|
|
118
|
1 |
|
$response = $this->httpPost(sprintf('/v3/domains/%s/webhooks', $domain), $params); |
|
119
|
|
|
|
|
120
|
1 |
|
return $this->hydrateResponse($response, CreateResponse::class); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $domain |
|
125
|
|
|
* @param string $id |
|
126
|
|
|
* @param string $url |
|
127
|
|
|
* |
|
128
|
|
|
* @return UpdateResponse |
|
129
|
|
|
*/ |
|
130
|
1 |
View Code Duplication |
public function update($domain, $id, $url) |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
1 |
|
Assert::notEmpty($domain); |
|
133
|
1 |
|
Assert::notEmpty($id); |
|
134
|
1 |
|
Assert::notEmpty($url); |
|
135
|
|
|
|
|
136
|
|
|
$params = [ |
|
137
|
1 |
|
'url' => $url, |
|
138
|
1 |
|
]; |
|
139
|
|
|
|
|
140
|
1 |
|
$response = $this->httpPut(sprintf('/v3/domains/%s/webhooks/%s', $domain, $id), $params); |
|
141
|
|
|
|
|
142
|
1 |
|
return $this->hydrateResponse($response, UpdateResponse::class); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param string $domain |
|
147
|
|
|
* @param string $id |
|
148
|
|
|
* |
|
149
|
|
|
* @return DeleteResponse |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function delete($domain, $id) |
|
152
|
|
|
{ |
|
153
|
1 |
|
Assert::notEmpty($domain); |
|
154
|
1 |
|
Assert::notEmpty($id); |
|
155
|
|
|
|
|
156
|
1 |
|
$response = $this->httpDelete(sprintf('/v3/domains/%s/webhooks/%s', $domain, $id)); |
|
157
|
|
|
|
|
158
|
1 |
|
return $this->hydrateResponse($response, DeleteResponse::class); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.