1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Copyright (C) 2013 Mailgun |
7
|
|
|
* |
8
|
|
|
* This software may be modified and distributed under the terms |
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mailgun\Api; |
13
|
|
|
|
14
|
|
|
use Mailgun\Assert; |
15
|
|
|
use Mailgun\HttpClient\RequestBuilder; |
16
|
|
|
use Mailgun\Hydrator\Hydrator; |
17
|
|
|
use Mailgun\Model\Webhook\CreateResponse; |
18
|
|
|
use Mailgun\Model\Webhook\DeleteResponse; |
19
|
|
|
use Mailgun\Model\Webhook\IndexResponse; |
20
|
|
|
use Mailgun\Model\Webhook\ShowResponse; |
21
|
|
|
use Mailgun\Model\Webhook\UpdateResponse; |
22
|
|
|
use Psr\Http\Client\ClientInterface; |
23
|
|
|
use Psr\Http\Message\ResponseInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @see https://documentation.mailgun.com/en/latest/api-webhooks.html |
27
|
|
|
* |
28
|
|
|
* @author Tobias Nyholm <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class Webhook extends HttpApi |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $apiKey; |
36
|
|
|
|
37
|
8 |
|
public function __construct(ClientInterface $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator, string $apiKey) |
38
|
|
|
{ |
39
|
8 |
|
parent::__construct($httpClient, $requestBuilder, $hydrator); |
40
|
8 |
|
$this->apiKey = $apiKey; |
41
|
8 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* This function verifies the webhook signature with your API key to to see if it is authentic. |
45
|
|
|
* |
46
|
|
|
* If this function returns FALSE, you must not process the request. |
47
|
|
|
* You should reject the request with status code 403 Forbidden. |
48
|
|
|
*/ |
49
|
3 |
|
public function verifyWebhookSignature(int $timestamp, string $token, string $signature): bool |
50
|
|
|
{ |
51
|
3 |
|
if (empty($timestamp) || empty($token) || empty($signature)) { |
52
|
1 |
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
$hmac = hash_hmac('sha256', $timestamp.$token, $this->apiKey); |
56
|
|
|
|
57
|
2 |
|
if (function_exists('hash_equals')) { |
58
|
|
|
// hash_equals is constant time, but will not be introduced until PHP 5.6 |
59
|
2 |
|
return hash_equals($hmac, $signature); |
60
|
|
|
} else { |
61
|
|
|
return $hmac === $signature; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return IndexResponse|ResponseInterface |
67
|
|
|
*/ |
68
|
1 |
|
public function index(string $domain) |
69
|
|
|
{ |
70
|
1 |
|
Assert::notEmpty($domain); |
71
|
1 |
|
$response = $this->httpGet(sprintf('/v3/domains/%s/webhooks', $domain)); |
72
|
|
|
|
73
|
|
|
return $this->hydrateResponse($response, IndexResponse::class); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return ShowResponse|ResponseInterface |
78
|
|
|
*/ |
79
|
1 |
View Code Duplication |
public function show(string $domain, string $webhook) |
|
|
|
|
80
|
|
|
{ |
81
|
1 |
|
Assert::notEmpty($domain); |
82
|
1 |
|
Assert::notEmpty($webhook); |
83
|
1 |
|
$response = $this->httpGet(sprintf('/v3/domains/%s/webhooks/%s', $domain, $webhook)); |
84
|
|
|
|
85
|
|
|
return $this->hydrateResponse($response, ShowResponse::class); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return CreateResponse|ResponseInterface |
90
|
|
|
*/ |
91
|
1 |
View Code Duplication |
public function create(string $domain, string $id, array $url) |
|
|
|
|
92
|
|
|
{ |
93
|
1 |
|
Assert::notEmpty($domain); |
94
|
1 |
|
Assert::notEmpty($id); |
95
|
1 |
|
Assert::notEmpty($url); |
96
|
|
|
|
97
|
|
|
$params = [ |
98
|
1 |
|
'id' => $id, |
99
|
1 |
|
'url' => $url, |
100
|
|
|
]; |
101
|
|
|
|
102
|
1 |
|
$response = $this->httpPost(sprintf('/v3/domains/%s/webhooks', $domain), $params); |
103
|
|
|
|
104
|
|
|
return $this->hydrateResponse($response, CreateResponse::class); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return UpdateResponse|ResponseInterface |
109
|
|
|
*/ |
110
|
1 |
View Code Duplication |
public function update(string $domain, string $id, array $url) |
|
|
|
|
111
|
|
|
{ |
112
|
1 |
|
Assert::notEmpty($domain); |
113
|
1 |
|
Assert::notEmpty($id); |
114
|
1 |
|
Assert::notEmpty($url); |
115
|
|
|
|
116
|
|
|
$params = [ |
117
|
1 |
|
'url' => $url, |
118
|
|
|
]; |
119
|
|
|
|
120
|
1 |
|
$response = $this->httpPut(sprintf('/v3/domains/%s/webhooks/%s', $domain, $id), $params); |
121
|
|
|
|
122
|
|
|
return $this->hydrateResponse($response, UpdateResponse::class); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return DeleteResponse|ResponseInterface |
127
|
|
|
*/ |
128
|
1 |
View Code Duplication |
public function delete(string $domain, string $id) |
|
|
|
|
129
|
|
|
{ |
130
|
1 |
|
Assert::notEmpty($domain); |
131
|
1 |
|
Assert::notEmpty($id); |
132
|
|
|
|
133
|
1 |
|
$response = $this->httpDelete(sprintf('/v3/domains/%s/webhooks/%s', $domain, $id)); |
134
|
|
|
|
135
|
|
|
return $this->hydrateResponse($response, DeleteResponse::class); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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.