event_id = $event_id; $this->entity = $entity; $this->company = $company; } /** * Execute the job. * * @return bool */ public function handle() {//todo set multidb here MultiDB::setDb($this->company->db); if (! $this->company || $this->company->is_disabled) { return true; } $subscriptions = Webhook::where('company_id', $this->company->id) ->where('event_id', $this->event_id) ->get(); if (! $subscriptions || $subscriptions->count() == 0) { return; } $subscriptions->each(function ($subscription) { $this->process($subscription); }); } 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 Client(['headers' => array_merge($base_headers, $headers)]); try { $response = $client->post($subscription->target_url, [ RequestOptions::JSON => $data, // or 'json' => [...] ]); SystemLogger::dispatch( array_merge((array)$response,$data), SystemLog::CATEGORY_WEBHOOK, SystemLog::EVENT_WEBHOOK_SUCCESS, SystemLog::TYPE_WEBHOOK_RESPONSE, $this->company->clients->first(), $this->company ); // if ($response->getStatusCode() == 410 || $response->getStatusCode() == 200) // return true;// $subscription->delete(); } catch(\Exception $e){ nlog($e->getMessage()); SystemLogger::dispatch( $e->getMessage(), SystemLog::CATEGORY_WEBHOOK, SystemLog::EVENT_WEBHOOK_RESPONSE, SystemLog::TYPE_WEBHOOK_RESPONSE, $this->company->clients->first(), $this->company, ); } } public function failed($exception) { nlog(print_r($exception->getMessage(), 1)); } }