Akaunting/app/Traits/Jobs.php

83 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2019-11-16 07:21:14 +00:00
<?php
namespace App\Traits;
2020-06-26 10:40:19 +00:00
use Exception;
2021-04-21 13:56:34 +00:00
use Illuminate\Contracts\Bus\Dispatcher;
2020-06-26 10:40:19 +00:00
use Throwable;
2019-11-16 07:21:14 +00:00
trait Jobs
{
/**
* Dispatch a job to its appropriate handler.
*
* @param mixed $job
* @return mixed
*/
public function dispatch($job)
{
2019-12-22 12:58:48 +00:00
$function = $this->getDispatchFunction();
2019-11-16 07:21:14 +00:00
2021-04-15 21:59:43 +00:00
return $this->$function($job);
}
/**
* Dispatch a job to its appropriate handler.
*
* @param mixed $command
* @return mixed
*/
public function dispatchQueue($job)
{
return app(Dispatcher::class)->dispatch($job);
}
/**
* Dispatch a command to its appropriate handler in the current process.
*
* Queuable jobs will be dispatched to the "sync" queue.
*
* @param mixed $command
* @param mixed $handler
* @return mixed
*/
public function dispatchSync($job, $handler = null)
{
return app(Dispatcher::class)->dispatchSync($job, $handler);
2019-11-16 07:21:14 +00:00
}
2020-03-09 07:26:02 +00:00
/**
* Dispatch a job to its appropriate handler and return a response array for ajax calls.
*
* @param mixed $job
* @return mixed
*/
public function ajaxDispatch($job)
{
try {
$data = $this->dispatch($job);
$response = [
'success' => true,
'error' => false,
'data' => $data,
'message' => '',
];
2020-06-26 10:40:19 +00:00
} catch (Exception | Throwable $e) {
2020-03-09 07:26:02 +00:00
$response = [
'success' => false,
'error' => true,
'data' => null,
'message' => $e->getMessage(),
];
}
return $response;
}
2019-12-22 12:58:48 +00:00
public function getDispatchFunction()
2019-11-16 07:21:14 +00:00
{
2021-04-15 21:59:43 +00:00
return should_queue() ? 'dispatchQueue' : 'dispatchSync';
2019-11-16 07:21:14 +00:00
}
}