invoiceninja/app/Jobs/Util/WebhookHandler.php

97 lines
2.4 KiB
PHP
Raw Normal View History

<?php
2020-11-11 11:13:39 +11:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-29 09:21:40 +11:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2020-11-11 11:13:39 +11:00
*
2021-06-16 16:58:16 +10:00
* @license https://www.elastic.co/licensing/elastic-license
2020-11-11 11:13:39 +11:00
*/
2021-06-10 11:15:21 +10:00
namespace App\Jobs\Util;
2021-01-13 10:12:01 +11:00
use App\Jobs\Util\SystemLogger;
2023-01-29 16:19:20 +11:00
use App\Jobs\Util\WebhookSingle;
2020-11-25 15:19:52 +01:00
use App\Libraries\MultiDB;
2021-09-26 20:17:09 +10:00
use App\Models\Client as ClientModel;
2021-01-13 10:12:01 +11:00
use App\Models\SystemLog;
2020-10-28 21:10:49 +11:00
use App\Models\Webhook;
use App\Transformers\ArraySerializer;
2022-12-14 09:25:05 +11:00
use App\Utils\Ninja;
2020-10-28 21:10:49 +11:00
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
2020-07-06 21:22:36 +10:00
class WebhookHandler implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $entity;
private $event_id;
2020-11-17 10:04:10 +11:00
private $company;
2020-11-22 22:14:49 +11:00
2023-01-29 16:19:20 +11:00
public $tries = 1; //number of retries
2020-11-22 22:14:49 +11:00
public $deleteWhenMissingModels = true;
2021-10-14 17:54:38 +11:00
private string $includes;
/**
* Create a new job instance.
*
2020-10-28 21:10:49 +11:00
* @param $event_id
* @param $entity
*/
2021-10-14 17:54:38 +11:00
public function __construct($event_id, $entity, $company, $includes = '')
{
$this->event_id = $event_id;
$this->entity = $entity;
2020-11-17 10:04:10 +11:00
$this->company = $company;
2021-10-14 17:54:38 +11:00
$this->includes = $includes;
}
/**
* Execute the job.
*
2020-10-28 21:10:49 +11:00
* @return bool
*/
2020-11-22 22:14:49 +11:00
public function handle()
2022-12-14 09:25:05 +11:00
{
2020-11-17 10:04:10 +11:00
MultiDB::setDb($this->company->db);
2022-12-14 09:25:05 +11:00
//If the company is disabled, or if on hosted, the user is not a paid hosted user return early
if (! $this->company || $this->company->is_disabled || (Ninja::isHosted() && !$this->company->account->isPaidHostedClient())) {
2020-08-24 21:53:22 +10:00
return true;
2020-11-25 15:19:52 +01:00
}
2020-11-22 22:14:49 +11:00
2020-11-18 20:35:09 +11:00
$subscriptions = Webhook::where('company_id', $this->company->id)
->where('event_id', $this->event_id)
2023-01-29 11:38:36 +11:00
->cursor()
->each(function ($subscription) {
2023-01-29 16:19:20 +11:00
// $this->process($subscription);
2020-08-24 21:53:22 +10:00
2023-01-29 16:19:20 +11:00
WebhookSingle::dispatch($subscription->id, $this->entity, $this->company->db, $this->includes);
2023-01-29 16:19:20 +11:00
});
2021-09-21 07:52:21 +10:00
}
public function failed($exception)
{
2023-01-29 11:38:36 +11:00
2020-12-30 08:10:03 +11:00
nlog(print_r($exception->getMessage(), 1));
2023-01-29 11:38:36 +11:00
}
}