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

110 lines
2.7 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
{
2023-01-31 15:06:21 +01:00
public $afterCommit = true;
2023-01-31 11:05:01 +01:00
/**
* 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();
2023-02-16 02:36:09 +01:00
if ($subscriptions) {
2023-02-01 05:00:45 +01:00
WebhookHandler::dispatch(Webhook::EVENT_CREATE_VENDOR, $vendor, $vendor->company)->delay(0);
2023-02-16 02:36:09 +01:00
}
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;
2023-02-16 02:36:09 +01:00
if ($vendor->getOriginal('deleted_at') && !$vendor->deleted_at) {
2023-01-31 13:53:54 +01:00
$event = Webhook::EVENT_RESTORE_VENDOR;
2023-02-16 02:36:09 +01:00
}
2023-01-31 13:53:54 +01:00
2023-02-16 02:36:09 +01:00
if ($vendor->is_deleted) {
$event = Webhook::EVENT_DELETE_VENDOR;
}
2023-01-31 13:53:54 +01:00
$subscriptions = Webhook::where('company_id', $vendor->company_id)
->where('event_id', $event)
2023-01-31 11:05:01 +01:00
->exists();
2023-02-16 02:36:09 +01:00
if ($subscriptions) {
2023-02-01 05:00:45 +01:00
WebhookHandler::dispatch($event, $vendor, $vendor->company)->delay(0);
2023-02-16 02:36:09 +01:00
}
2023-01-31 11:05:01 +01:00
}
/**
* Handle the vendor "deleted" event.
*
* @param Vendor $vendor
* @return void
*/
public function deleted(Vendor $vendor)
{
2023-02-16 02:36:09 +01:00
if ($vendor->is_deleted) {
2023-01-31 13:53:54 +01:00
return;
2023-02-16 02:36:09 +01:00
}
2023-01-31 13:53:54 +01:00
$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-02-01 05:00:45 +01:00
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_VENDOR, $vendor, $vendor->company)->delay(0);
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)
{
//
}
}