photo/app/Jobs/CatalogDelete.php

81 lines
1.7 KiB
PHP

<?php
namespace App\Jobs;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Models\Abstracted\Catalog;
class CatalogDelete implements ShouldQueue
{
use InteractsWithQueue,Queueable,SerializesModels;
// Our object
private Catalog $o;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Catalog $o)
{
$this->o = $o;
}
public function middleware(): array
{
return [new WithoutOverlapping($this->uniqueId())];
}
/**
* Get the unique ID for the job.
*/
public function uniqueId(): string
{
return $this->o::config.'|'.$this->o->id;
}
/**
* Execute the job.
*
* @return void
*/
public function handle(): void
{
if (! $this->o->remove) {
Log::warning(sprintf('%s: NOT Deleting [%s] not marked for deletion',__METHOD__,$this->o->file_name_rel(FALSE)));
return;
}
// Remove tags;
if ($this->o->tags->count())
$this->o->tags()->detach();
// Remove People;
if ($this->o->people->count())
$this->o->people()->detach();
// Make sure our parent is writable
if (! $this->o->isParentWritable($x=dirname($this->o->file_name_rel(TRUE)))) {
Log::warning(sprintf('%s: NOT Deleting [%s] parent directory not writable',__METHOD__,$this->o->file_name(FALSE)));
$this->fail('Parent not writable:'.$x);
return;
}
// Perform delete
if (file_exists($this->o->file_name(FALSE)))
unlink($this->o->file_name(FALSE));
Log::warning(sprintf('%s: Deleted [%s]',__METHOD__,$this->o->file_name(FALSE)));
$this->o->delete();
}
}