added pointer interfaces for jobs

This commit is contained in:
Denis Duliçi
2021-09-06 11:53:57 +03:00
parent abd55133f1
commit c08a8cfc4e
63 changed files with 635 additions and 1524 deletions

View File

@ -3,33 +3,17 @@
namespace App\Jobs\Setting;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldDelete;
use App\Models\Setting\Category;
class DeleteCategory extends Job
class DeleteCategory extends Job implements ShouldDelete
{
protected $category;
/**
* Create a new job instance.
*
* @param $category
*/
public function __construct($category)
{
$this->category = $category;
}
/**
* Execute the job.
*
* @return boolean|Exception
*/
public function handle()
public function handle(): bool
{
$this->authorize();
\DB::transaction(function () {
$this->category->delete();
$this->model->delete();
});
return true;
@ -37,26 +21,24 @@ class DeleteCategory extends Job
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
public function authorize(): void
{
// Can not delete the last category by type
if (Category::where('type', $this->category->type)->count() == 1) {
$message = trans('messages.error.last_category', ['type' => strtolower(trans_choice('general.' . $this->category->type . 's', 1))]);
if (Category::where('type', $this->model->type)->count() == 1) {
$message = trans('messages.error.last_category', ['type' => strtolower(trans_choice('general.' . $this->model->type . 's', 1))]);
throw new \Exception($message);
}
if ($relationships = $this->getRelationships()) {
$message = trans('messages.warning.deleted', ['name' => $this->category->name, 'text' => implode(', ', $relationships)]);
$message = trans('messages.warning.deleted', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]);
throw new \Exception($message);
}
}
public function getRelationships()
public function getRelationships(): array
{
$rels = [
'items' => 'items',
@ -65,6 +47,6 @@ class DeleteCategory extends Job
'transactions' => 'transactions',
];
return $this->countRelationships($this->category, $rels);
return $this->countRelationships($this->model, $rels);
}
}