event_id = $event_id; $this->entity = $entity; } /** * Execute the job. * * @return void */ public function handle() :bool { if (! $this->entity->company || $this->entity->company->company_users->first()->is_migrating == true) { return true; } $subscriptions = \App\Models\Webhook::where('company_id', $this->entity->company_id) ->where('event_id', $this->event_id) ->get(); if (! $subscriptions || $subscriptions->count() == 0) { return true; } $subscriptions->each(function ($subscription) { $this->process($subscription); }); return true; } private function process($subscription) { // generate JSON data $manager = new Manager(); $manager->setSerializer(new ArraySerializer()); $class = sprintf('App\\Transformers\\%sTransformer', class_basename($this->entity)); $transformer = new $class(); $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 = [ 'Content-Length' => strlen(json_encode($data)), 'Accept' => 'application/json', ]; $client = new \GuzzleHttp\Client(['headers' => array_merge($base_headers, $headers)]); $response = $client->post($subscription->target_url, [ RequestOptions::JSON => $data, // or 'json' => [...] ]); if ($response->getStatusCode() == 410 || $response->getStatusCode() == 200) { $subscription->delete(); } } public function failed($exception) { info(print_r($exception->getMessage(), 1)); } }