2020-05-04 13:13:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Util;
|
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
use App\Models\Webhook;
|
2020-05-06 13:49:42 +02:00
|
|
|
use App\Transformers\ArraySerializer;
|
2020-10-28 11:10:49 +01:00
|
|
|
use GuzzleHttp\Client;
|
2020-09-06 11:38:10 +02:00
|
|
|
use GuzzleHttp\RequestOptions;
|
2020-05-04 13:13:46 +02:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2020-05-06 13:49:42 +02:00
|
|
|
use League\Fractal\Manager;
|
|
|
|
use League\Fractal\Resource\Item;
|
2020-05-04 13:13:46 +02:00
|
|
|
|
2020-07-06 13:22:36 +02:00
|
|
|
class WebhookHandler implements ShouldQueue
|
2020-05-04 13:13:46 +02:00
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
2020-05-06 13:49:42 +02:00
|
|
|
private $entity;
|
|
|
|
|
|
|
|
private $event_id;
|
|
|
|
|
2020-05-04 13:13:46 +02:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param $event_id
|
|
|
|
* @param $entity
|
2020-05-04 13:13:46 +02:00
|
|
|
*/
|
2020-05-06 13:49:42 +02:00
|
|
|
public function __construct($event_id, $entity)
|
2020-05-04 13:13:46 +02:00
|
|
|
{
|
2020-05-06 13:49:42 +02:00
|
|
|
$this->event_id = $event_id;
|
|
|
|
$this->entity = $entity;
|
2020-05-04 13:13:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return bool
|
2020-05-04 13:13:46 +02:00
|
|
|
*/
|
2020-05-27 06:46:19 +02:00
|
|
|
public function handle() :bool
|
2020-05-04 13:13:46 +02:00
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $this->entity->company || $this->entity->company->company_users->first()->is_migrating == true) {
|
2020-08-24 13:53:22 +02:00
|
|
|
return true;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-05-27 06:46:19 +02:00
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
$subscriptions = Webhook::where('company_id', $this->entity->company_id)
|
2020-05-06 13:49:42 +02:00
|
|
|
->where('event_id', $this->event_id)
|
|
|
|
->get();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $subscriptions || $subscriptions->count() == 0) {
|
2020-05-27 06:46:19 +02:00
|
|
|
return true;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-05-06 13:49:42 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$subscriptions->each(function ($subscription) {
|
2020-05-06 13:49:42 +02:00
|
|
|
$this->process($subscription);
|
|
|
|
});
|
2020-05-27 06:46:19 +02:00
|
|
|
|
|
|
|
return true;
|
2020-05-06 13:49:42 +02:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-05-06 13:49:42 +02:00
|
|
|
private function process($subscription)
|
|
|
|
{
|
|
|
|
// generate JSON data
|
|
|
|
$manager = new Manager();
|
|
|
|
$manager->setSerializer(new ArraySerializer());
|
|
|
|
|
2020-08-24 13:53:22 +02:00
|
|
|
$class = sprintf('App\\Transformers\\%sTransformer', class_basename($this->entity));
|
|
|
|
|
|
|
|
$transformer = new $class();
|
2020-05-06 13:49:42 +02:00
|
|
|
|
|
|
|
$resource = new Item($this->entity, $transformer, $this->entity->getEntityType());
|
|
|
|
$data = $manager->createData($resource)->toArray();
|
|
|
|
|
|
|
|
$this->postData($subscription, $data, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function postData($subscription, $data, $headers = [])
|
|
|
|
{
|
|
|
|
$base_headers = [
|
2020-08-24 13:53:22 +02:00
|
|
|
'Content-Length' => strlen(json_encode($data)),
|
2020-09-06 11:38:10 +02:00
|
|
|
'Accept' => 'application/json',
|
2020-05-06 13:49:42 +02:00
|
|
|
];
|
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
$client = new Client(['headers' => array_merge($base_headers, $headers)]);
|
2020-08-24 13:53:22 +02:00
|
|
|
|
2020-05-06 13:49:42 +02:00
|
|
|
$response = $client->post($subscription->target_url, [
|
2020-09-06 11:38:10 +02:00
|
|
|
RequestOptions::JSON => $data, // or 'json' => [...]
|
2020-05-06 13:49:42 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
if ($response->getStatusCode() == 410 || $response->getStatusCode() == 200) {
|
|
|
|
$subscription->delete();
|
|
|
|
}
|
2020-05-04 13:13:46 +02:00
|
|
|
}
|
2020-07-01 08:03:46 +02:00
|
|
|
|
|
|
|
public function failed($exception)
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
info(print_r($exception->getMessage(), 1));
|
2020-07-01 08:03:46 +02:00
|
|
|
}
|
2020-05-04 13:13:46 +02:00
|
|
|
}
|