1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Observers/VendorObserver.php

105 lines
2.6 KiB
PHP
Raw Normal View History

2023-01-31 11:05:01 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Observers;
use App\Jobs\Util\WebhookHandler;
use App\Models\Vendor;
use App\Models\Webhook;
class VendorObserver
{
/**
* Handle the vendor "created" event.
*
* @param Vendor $vendor
* @return void
*/
public function created(Vendor $vendor)
{
2023-01-31 13:53:54 +01:00
$subscriptions = Webhook::where('company_id', $vendor->company_id)
2023-01-31 11:05:01 +01:00
->where('event_id', Webhook::EVENT_CREATE_VENDOR)
->exists();
if ($subscriptions) {
2023-01-31 13:53:54 +01:00
WebhookHandler::dispatch(Webhook::EVENT_CREATE_VENDOR, $vendor, $vendor->company)->delay(rand(1,5));
2023-01-31 11:05:01 +01:00
}
}
/**
* Handle the vendor "updated" event.
*
* @param Vendor $vendor
* @return void
*/
public function updated(Vendor $vendor)
{
2023-01-31 13:53:54 +01:00
$event = Webhook::EVENT_UPDATE_VENDOR;
if($vendor->getOriginal('deleted_at') && !$vendor->deleted_at)
$event = Webhook::EVENT_RESTORE_VENDOR;
if($vendor->is_deleted)
$event = Webhook::EVENT_DELETE_VENDOR;
$subscriptions = Webhook::where('company_id', $vendor->company_id)
->where('event_id', $event)
2023-01-31 11:05:01 +01:00
->exists();
2023-01-31 13:53:54 +01:00
if ($subscriptions)
WebhookHandler::dispatch($event, $vendor, $vendor->company)->delay(rand(1,5));
2023-01-31 11:05:01 +01:00
}
/**
* Handle the vendor "deleted" event.
*
* @param Vendor $vendor
* @return void
*/
public function deleted(Vendor $vendor)
{
2023-01-31 13:53:54 +01:00
if($vendor->is_deleted)
return;
$subscriptions = Webhook::where('company_id', $vendor->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_VENDOR)
2023-01-31 11:05:01 +01:00
->exists();
if ($subscriptions) {
2023-01-31 13:53:54 +01:00
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_VENDOR, $vendor, $vendor->company)->delay(rand(1,5));
2023-01-31 11:05:01 +01:00
}
}
/**
* Handle the vendor "restored" event.
*
* @param Vendor $vendor
* @return void
*/
public function restored(Vendor $vendor)
{
//
}
/**
* Handle the vendor "force deleted" event.
*
* @param Vendor $vendor
* @return void
*/
public function forceDeleted(Vendor $vendor)
{
//
}
}