photo/app/Jobs/CatalogDelete.php

64 lines
1.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Jobs;
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,SerializesModels;
// Our object
private Catalog $o;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Catalog $o)
{
$this->o = $o;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if (! $this->o->remove) {
Log::warning(sprintf('%s: NOT Deleting [%s] not marked for deletion',__METHOD__,$this->o->file_name_rel(FALSE)));
exit;
}
// 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(dirname($this->o->file_name(FALSE)))) {
Log::warning(sprintf('%s: NOT Deleting [%s] parent directory not writable',__METHOD__,$this->o->file_name(FALSE)));
exit;
}
// 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();
}
}