invoiceninja/app/Observers/TaskObserver.php

110 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2019-04-19 19:09:55 +10:00
<?php
2019-05-11 13:32:07 +10:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 13:32:07 +10:00
*
* @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)
2019-05-11 13:32:07 +10:00
*
2021-06-16 16:58:16 +10:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 13:32:07 +10:00
*/
2019-04-19 19:09:55 +10:00
namespace App\Observers;
2020-07-06 21:22:36 +10:00
use App\Jobs\Util\WebhookHandler;
2019-04-19 19:09:55 +10:00
use App\Models\Task;
2020-07-06 21:22:36 +10:00
use App\Models\Webhook;
2019-04-19 19:09:55 +10:00
class TaskObserver
{
2023-02-01 01:06:21 +11:00
public $afterCommit = true;
2019-04-19 19:09:55 +10:00
/**
* Handle the task "created" event.
*
2020-10-28 21:10:49 +11:00
* @param Task $task
2019-04-19 19:09:55 +10:00
* @return void
*/
public function created(Task $task)
{
2023-01-31 23:53:54 +11:00
$subscriptions = Webhook::where('company_id', $task->company_id)
2020-11-25 11:23:39 +11:00
->where('event_id', Webhook::EVENT_CREATE_TASK)
->exists();
2023-02-16 12:36:09 +11:00
if ($subscriptions) {
2023-02-01 15:00:45 +11:00
WebhookHandler::dispatch(Webhook::EVENT_CREATE_TASK, $task, $task->company)->delay(0);
2023-02-16 12:36:09 +11:00
}
2019-04-19 19:09:55 +10:00
}
/**
* Handle the task "updated" event.
*
2020-10-28 21:10:49 +11:00
* @param Task $task
2019-04-19 19:09:55 +10:00
* @return void
*/
public function updated(Task $task)
{
2023-01-31 23:53:54 +11:00
$event = Webhook::EVENT_UPDATE_TASK;
2023-02-16 12:36:09 +11:00
if ($task->getOriginal('deleted_at') && !$task->deleted_at) {
2023-01-31 23:53:54 +11:00
$event = Webhook::EVENT_RESTORE_TASK;
2023-02-16 12:36:09 +11:00
}
2023-01-31 23:53:54 +11:00
2023-02-16 12:36:09 +11:00
if ($task->is_deleted) {
$event = Webhook::EVENT_DELETE_TASK;
}
2023-01-31 23:53:54 +11:00
$subscriptions = Webhook::where('company_id', $task->company_id)
->where('event_id', $event)
->exists();
2020-11-25 11:23:39 +11:00
2023-02-16 12:36:09 +11:00
if ($subscriptions) {
2023-02-01 15:00:45 +11:00
WebhookHandler::dispatch($event, $task, $task->company)->delay(0);
2023-02-16 12:36:09 +11:00
}
2019-04-19 19:09:55 +10:00
}
/**
* Handle the task "deleted" event.
*
2020-10-28 21:10:49 +11:00
* @param Task $task
2019-04-19 19:09:55 +10:00
* @return void
*/
public function deleted(Task $task)
{
2023-02-16 12:36:09 +11:00
if ($task->is_deleted) {
2023-01-31 23:53:54 +11:00
return;
2023-02-16 12:36:09 +11:00
}
2023-01-31 23:53:54 +11:00
$subscriptions = Webhook::where('company_id', $task->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_TASK)
2020-11-25 11:23:39 +11:00
->exists();
2023-02-16 12:36:09 +11:00
if ($subscriptions) {
2023-02-01 15:00:45 +11:00
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_TASK, $task, $task->company)->delay(0);
2023-02-16 12:36:09 +11:00
}
2019-04-19 19:09:55 +10:00
}
/**
* Handle the task "restored" event.
*
2020-10-28 21:10:49 +11:00
* @param Task $task
2019-04-19 19:09:55 +10:00
* @return void
*/
public function restored(Task $task)
{
//
}
/**
* Handle the task "force deleted" event.
*
2020-10-28 21:10:49 +11:00
* @param Task $task
2019-04-19 19:09:55 +10:00
* @return void
*/
public function forceDeleted(Task $task)
{
//
}
}