|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mpociot\CaptainHook\Jobs; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use GuzzleHttp\Client; |
|
7
|
|
|
use GuzzleHttp\Middleware; |
|
8
|
|
|
use GuzzleHttp\Promise\Promise; |
|
9
|
|
|
use Illuminate\Bus\Queueable; |
|
10
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
11
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
|
12
|
|
|
use Illuminate\Queue\SerializesModels; |
|
13
|
|
|
use Mpociot\CaptainHook\CaptainHookLog; |
|
14
|
|
|
use Psr\Http\Message\RequestInterface; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
|
|
17
|
|
|
class TriggerWebhooksJob implements ShouldQueue |
|
18
|
|
|
{ |
|
19
|
|
|
use Queueable, InteractsWithQueue, SerializesModels; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* All the webhooks that should be executed. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array|\Illuminate\Support\Collection |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $webhooks; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The event data to be posted to our hooks. |
|
31
|
|
|
* |
|
32
|
|
|
* @var mixed |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $eventData; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Create a new job instance. |
|
38
|
|
|
* |
|
39
|
|
|
* @param array|\Illuminate\Support\Collection $wekbhooks |
|
|
|
|
|
|
40
|
|
|
* @param $eventData |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($webhooks, $eventData) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->eventData = $eventData; |
|
45
|
|
|
$this->webhooks = $webhooks; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Execute the job. |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
public function handle() |
|
54
|
|
|
{ |
|
55
|
|
|
$config = app('Illuminate\Contracts\Config\Repository'); |
|
56
|
|
|
$client = app(Client::class); |
|
57
|
|
|
|
|
58
|
|
|
if (($logging = $config->get('captain_hook.log.active') && $config->get('queue.driver') != 'sync') && |
|
|
|
|
|
|
59
|
|
|
$config->get('captain_hook.log.storage_time') != -1) { |
|
60
|
|
|
CaptainHookLog::where('updated_at', '<', Carbon::now()->subHours($config->get('captain_hook.log.storage_time')))->delete(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
foreach ($this->webhooks as $webhook) { |
|
64
|
|
|
if ($logging) { |
|
65
|
|
|
$log = new CaptainHookLog([ |
|
66
|
|
|
'webhook_id' => $webhook[ 'id' ], |
|
67
|
|
|
'url' => $webhook[ 'url' ], |
|
68
|
|
|
]); |
|
69
|
|
|
$middleware = Middleware::tap(function (RequestInterface $request, $options) use ($log) { |
|
|
|
|
|
|
70
|
|
|
$log->payload_format = isset($request->getHeader('Content-Type')[0]) ? $request->getHeader('Content-Type')[0] : null; |
|
|
|
|
|
|
71
|
|
|
$log->payload = $request->getBody()->getContents(); |
|
|
|
|
|
|
72
|
|
|
}, function ($request, $options, Promise $response) use ($log) { |
|
73
|
|
|
$response->then(function (ResponseInterface $response) use ($log) { |
|
74
|
|
|
$log->status = $response->getStatusCode(); |
|
|
|
|
|
|
75
|
|
|
$log->response = $response->getBody()->getContents(); |
|
|
|
|
|
|
76
|
|
|
$log->response_format = $log->payload_format = isset($response->getHeader('Content-Type')[0]) ? $response->getHeader('Content-Type')[0] : null; |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
$log->save(); |
|
79
|
|
|
}); |
|
80
|
|
|
}); |
|
81
|
|
|
|
|
82
|
|
|
$client->post($webhook[ 'url' ], [ |
|
83
|
|
|
'body' => $this->eventData, |
|
84
|
|
|
'handler' => $middleware($client->getConfig('handler')), |
|
85
|
|
|
]); |
|
86
|
|
|
} else { |
|
87
|
|
|
$client->postAsync($webhook[ 'url' ], [ |
|
88
|
|
|
'body' => $this->eventData, |
|
89
|
|
|
'verify' => false, |
|
90
|
|
|
'future' => true, |
|
91
|
|
|
]); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.