1 | <?php |
||
11 | class WebhookCall |
||
12 | { |
||
13 | /** @var \Spatie\WebhookServer\CallWebhookJob */ |
||
14 | protected $callWebhookJob; |
||
15 | |||
16 | /** @var string */ |
||
17 | protected $secret; |
||
18 | |||
19 | /** @var \Spatie\WebhookServer\Signer\Signer */ |
||
20 | protected $signer; |
||
21 | |||
22 | /** @var array */ |
||
23 | protected $headers = []; |
||
24 | |||
25 | /** @var array */ |
||
26 | private $payload = []; |
||
27 | |||
28 | public static function create(): self |
||
29 | { |
||
30 | $config = config('webhook-server'); |
||
31 | |||
32 | return (new static()) |
||
33 | ->onQueue($config['queue']) |
||
34 | ->useHttpVerb($config['http_verb']) |
||
35 | ->maximumTries($config['tries']) |
||
36 | ->useBackoffStrategy($config['backoff_strategy']) |
||
37 | ->timeoutInSeconds($config['timeout_in_seconds']) |
||
38 | ->signUsing($config['signer']) |
||
39 | ->withHeaders($config['headers']) |
||
40 | ->withTags($config['tags']) |
||
41 | ->verifySsl($config['verify_ssl']); |
||
42 | } |
||
43 | |||
44 | public function __construct() |
||
45 | { |
||
46 | $this->callWebhookJob = app(CallWebhookJob::class); |
||
47 | } |
||
48 | |||
49 | public function url(string $url) |
||
50 | { |
||
51 | $this->callWebhookJob->webhookUrl = $url; |
||
52 | |||
53 | return $this; |
||
54 | } |
||
55 | |||
56 | public function payload(array $payload) |
||
57 | { |
||
58 | $this->payload = $payload; |
||
59 | |||
60 | $this->callWebhookJob->payload = $payload; |
||
61 | |||
62 | return $this; |
||
63 | } |
||
64 | |||
65 | public function onQueue(string $queue) |
||
66 | { |
||
67 | $this->callWebhookJob->queue = $queue; |
||
68 | |||
69 | return $this; |
||
70 | } |
||
71 | |||
72 | public function useSecret(string $secret) |
||
73 | { |
||
74 | $this->secret = $secret; |
||
75 | |||
76 | return $this; |
||
77 | } |
||
78 | |||
79 | public function useHttpVerb(string $verb) |
||
80 | { |
||
81 | $this->callWebhookJob->httpVerb = $verb; |
||
82 | |||
83 | return $this; |
||
84 | } |
||
85 | |||
86 | public function maximumTries(int $tries) |
||
87 | { |
||
88 | $this->callWebhookJob->tries = $tries; |
||
89 | |||
90 | return $this; |
||
91 | } |
||
92 | |||
93 | public function useBackoffStrategy(string $backoffStrategyClass) |
||
94 | { |
||
95 | if (! is_subclass_of($backoffStrategyClass, BackoffStrategy::class)) { |
||
96 | throw InvalidBackoffStrategy::doesNotExtendBackoffStrategy($backoffStrategyClass); |
||
97 | } |
||
98 | |||
99 | $this->callWebhookJob->backoffStrategyClass = $backoffStrategyClass; |
||
100 | |||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | public function timeoutInSeconds(int $timeoutInSeconds) |
||
105 | { |
||
106 | $this->callWebhookJob->requestTimeout = $timeoutInSeconds; |
||
107 | |||
108 | return $this; |
||
109 | } |
||
110 | |||
111 | public function signUsing(string $signerClass) |
||
112 | { |
||
113 | if (! is_subclass_of($signerClass, Signer::class)) { |
||
114 | throw InvalidSigner::doesImplementSigner($signerClass); |
||
115 | } |
||
116 | |||
117 | $this->signer = app($signerClass); |
||
118 | |||
119 | return $this; |
||
120 | } |
||
121 | |||
122 | public function withHeaders(array $headers) |
||
123 | { |
||
124 | $this->headers = $headers; |
||
125 | |||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | public function verifySsl(bool $verifySsl = true) |
||
130 | { |
||
131 | $this->callWebhookJob->verifySsl = $verifySsl; |
||
132 | |||
133 | return $this; |
||
134 | } |
||
135 | |||
136 | public function doNotVerifySsl() |
||
137 | { |
||
138 | $this->verifySsl(false); |
||
139 | |||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | public function meta(array $meta) |
||
144 | { |
||
145 | $this->callWebhookJob->meta = $meta; |
||
146 | |||
147 | return $this; |
||
148 | } |
||
149 | |||
150 | public function withTags(array $tags) |
||
151 | { |
||
152 | $this->callWebhookJob->tags = $tags; |
||
153 | |||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | public function dispatch(): void |
||
158 | { |
||
159 | if (! $this->callWebhookJob->webhookUrl) { |
||
160 | throw CouldNotCallWebhook::urlNotSet(); |
||
161 | } |
||
162 | |||
163 | if (empty($this->secret)) { |
||
164 | throw CouldNotCallWebhook::secretNotSet(); |
||
165 | } |
||
166 | |||
167 | $this->callWebhookJob->headers = $this->getAllHeaders(); |
||
168 | |||
169 | dispatch($this->callWebhookJob); |
||
170 | } |
||
171 | |||
172 | protected function getAllHeaders(): array |
||
173 | { |
||
174 | $headers = array_merge( |
||
175 | config('webhook-server.headers'), |
||
176 | $this->headers, |
||
177 | ); |
||
|
|||
178 | |||
179 | $signature = $this->signer->calculateSignature($this->payload, $this->secret); |
||
180 | |||
181 | $headers[$this->signer->signatureHeaderName()] = $signature; |
||
182 | |||
183 | return $headers; |
||
186 |