1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bencoderus\Webhook\Webhooks; |
4
|
|
|
|
5
|
|
|
use Bencoderus\Webhook\Exceptions\WebhookException; |
6
|
|
|
use Bencoderus\Webhook\Traits\SendWebhook; |
7
|
|
|
use Illuminate\Http\Resources\DelegatesToResource; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
abstract class BaseWebhook |
11
|
|
|
{ |
12
|
|
|
use SendWebhook, DelegatesToResource; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Webhook encryption signature. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
public $signature; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The URL the outgoing will be dispatched to. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $url; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The data that will be dispatched with the outgoing webhook. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $payload; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* An instance of the webhook payload. |
37
|
|
|
* |
38
|
|
|
* @var null |
39
|
|
|
*/ |
40
|
|
|
private $resource; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create a webhook instance. |
44
|
|
|
* |
45
|
|
|
* @param null $model |
46
|
|
|
*/ |
47
|
|
|
public function __construct($model = null) |
48
|
|
|
{ |
49
|
|
|
$this->resource = $model; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Add a webhook signature. |
54
|
|
|
* |
55
|
|
|
* @param string $signatureName |
56
|
|
|
* @param string $encryptionKey |
57
|
|
|
* @param string $hashAlgorithm |
58
|
|
|
* |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function withSignature(string $signatureName, string $encryptionKey, string $hashAlgorithm = 'sha512'): self |
62
|
|
|
{ |
63
|
|
|
$encryptionKey = hash($hashAlgorithm, $encryptionKey); |
64
|
|
|
|
65
|
|
|
$this->signature = [$signatureName => $encryptionKey]; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add webhook url. |
72
|
|
|
* |
73
|
|
|
* @param string $url |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function url(string $url): self |
78
|
|
|
{ |
79
|
|
|
$this->url = $url; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Send webhook asynchronously(Queue) or synchronously. |
86
|
|
|
* |
87
|
|
|
* @param bool $sendWithQueue |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
* @throws \Bencoderus\Webhook\Exceptions\WebhookException |
91
|
|
|
*/ |
92
|
|
|
public function send(bool $sendWithQueue = true): bool |
93
|
|
|
{ |
94
|
|
|
$data = $this->prepareWebhook(); |
95
|
|
|
|
96
|
|
|
if (empty($data['url'])) { |
97
|
|
|
throw new WebhookException("You need to set a webhook URL"); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (! config('webhook.dispatch_webhook')) { |
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($sendWithQueue) { |
105
|
|
|
return $this->sendViaQueue($data); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->sendViaHttp($data); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Generate webhook data. |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function prepareWebhook(): array |
117
|
|
|
{ |
118
|
|
|
$webhookData = []; |
119
|
|
|
|
120
|
|
|
$this->generatePayload(); |
121
|
|
|
|
122
|
|
|
if ($this->signature) { |
|
|
|
|
123
|
|
|
$webhookData['signature'] = $this->signature ?? []; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$webhookData['url'] = $this->url; |
127
|
|
|
$webhookData['payload'] = $this->payload; |
128
|
|
|
$webhookData['webhook_id'] = Str::uuid(); |
129
|
|
|
|
130
|
|
|
return $webhookData; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Format outgoing webhook payload |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
private function generatePayload(): array |
139
|
|
|
{ |
140
|
|
|
$this->payload = array_merge( |
141
|
|
|
['event' => $this->event], |
|
|
|
|
142
|
|
|
['data' => $this->data()] |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
return $this->payload; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Webhook payload data. |
150
|
|
|
* |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
|
|
abstract public function data(); |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.