photo/app/Jobs/CatalogMove.php

84 lines
1.8 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Jobs;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Models\Abstracted\Catalog;
use Illuminate\Support\Facades\Storage;
class CatalogMove implements ShouldQueue,ShouldBeUnique
{
use InteractsWithQueue,Queueable,SerializesModels;
// Our object
private Catalog $o;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Catalog $o) {
$this->o = $o->withoutRelations();
}
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
* @throws \Exception
*/
public function handle(): void
{
$from = $this->o->file_name_rel();
$to = $this->o->file_name_rel(FALSE);
Log::info(sprintf('%s: Moving [%s|%s] from [%s] to [%s]',
__METHOD__,
$this->o::config,
$this->o->id,
$from,
$to,
));
// If the move is successful, commit the transaction
if ($this->o->isMoveable()) {
// If our move fails, we'll abort the update
DB::beginTransaction();
$this->o->filename = $this->o->file_name();
$this->o->save();
if (Storage::disk($this->o::fs)->move($from,$to))
DB::commit();
else
Log::error(sprintf('%s: Move failed for file [%s]',__METHOD__,$from));
} else
Log::alert(sprintf('%s: Unable to move file [%s] with reason [%s]',
__METHOD__,
$from,
$this->o->isMoveableReason(),
));
}
}