210 lines
5.9 KiB
PHP
210 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Enums\LogType;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Filesystem\Filesystem;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
abstract class XArcadiaFlux implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
/**
|
|
* The Laravel disk name
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $disk = null;
|
|
|
|
/**
|
|
* This is for Laravel Console usage
|
|
* @var \Illuminate\Console\Command|null
|
|
*/
|
|
public $console = null;
|
|
|
|
/**
|
|
* The relative folder where to archive data
|
|
*
|
|
* @var string
|
|
*/
|
|
private $archiveFolderName = 'archives';
|
|
|
|
/**
|
|
* The files to ignore
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $ignoreFiles = [
|
|
'.gitignore', '.gitkeep'
|
|
];
|
|
|
|
/**
|
|
* Check if the file extension
|
|
*
|
|
* @param string $filename
|
|
* @param string $extension
|
|
* @return boolean
|
|
*/
|
|
protected function checkExtension(string $filename, string $extension): bool
|
|
{
|
|
$isExtension = strtolower(pathinfo($filename, \PATHINFO_EXTENSION)) === $extension;
|
|
if (!$isExtension) {
|
|
Log::warning(sprintf(
|
|
'%s : unwanted file type in %s wanted %s',
|
|
get_class($this),
|
|
$filename,
|
|
$extension
|
|
));
|
|
}
|
|
return $isExtension;
|
|
}
|
|
|
|
/**
|
|
* Move the file to archives
|
|
*
|
|
* @param string $filename
|
|
* @return void
|
|
*/
|
|
protected function moveToArchives(string $filename): void
|
|
{
|
|
$archiveFile = sprintf('%s/%s', $this->archiveFolderName, $filename);
|
|
if ($this->getDisk()->exists($archiveFile)) {
|
|
$this->log(
|
|
sprintf(
|
|
'File %s exists, and therefore must be delete before archive',
|
|
$archiveFile
|
|
),
|
|
LogType::info()
|
|
);
|
|
if (!$this->getDisk()->delete($archiveFile)) {
|
|
Log::critical(sprintf('%s : Could not delete file %s', get_class($this), $archiveFile));
|
|
};
|
|
}
|
|
|
|
if (!$this->getDisk()->move($filename, sprintf('%s/%s', $this->archiveFolderName, $filename))) {
|
|
$this->log(
|
|
sprintf(
|
|
'File %s could not be moved to archives',
|
|
$filename
|
|
),
|
|
LogType::warning()
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Suppression des fichiers dans
|
|
* archives qui ont plus de 30j
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function clearOldArchives(): void
|
|
{
|
|
$disk = $this->getDisk();
|
|
$archives = $disk->files($this->archiveFolderName);
|
|
|
|
// /** @var File */
|
|
foreach ($archives as $archive) {
|
|
$pi = \pathinfo($archive, \PATHINFO_BASENAME);
|
|
if (in_array($pi, $this->ignoreFiles)) {
|
|
continue;
|
|
}
|
|
|
|
if ($disk->lastModified($archive) < strtotime(Carbon::now()->subDays(30))) {
|
|
if (!$disk->delete($archive)) {
|
|
Log::critical(sprintf('%s : Could not delete file %s', get_class($this), $archive));
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the concerned disk
|
|
*
|
|
* @return Filesystem
|
|
*/
|
|
protected function getDisk(): Filesystem
|
|
{
|
|
return Storage::disk($this->disk);
|
|
}
|
|
|
|
/**
|
|
* Logs data
|
|
*
|
|
* @param string $message
|
|
* @param LogType $logtype
|
|
* @return void
|
|
* @throws \ErrorException If logtype is not handled.
|
|
*/
|
|
protected function log(string $message, LogType $logtype = null): void
|
|
{
|
|
if (!$logtype) {
|
|
$logtype = LogType::debug();
|
|
}
|
|
$message = sprintf('%s : %s', get_class($this), $message);
|
|
switch ($logtype->value) {
|
|
case LogType::debug()->value:
|
|
Log::debug($message);
|
|
break;
|
|
case LogType::info()->value:
|
|
Log::info($message);
|
|
break;
|
|
case LogType::log()->value:
|
|
Log::log($message);
|
|
break;
|
|
case LogType::warning()->value:
|
|
Log::warning($message);
|
|
break;
|
|
case LogType::error()->value:
|
|
Log::error($message);
|
|
break;
|
|
case LogType::critical()->value:
|
|
Log::critical($message);
|
|
break;
|
|
default:
|
|
throw new \ErrorException(sprintf('%s : unhandled log type %s', get_class($this), $logtype->label));
|
|
}//end switch
|
|
$this->consolePrint($message, $logtype);
|
|
}
|
|
|
|
/**
|
|
* Prints to the console
|
|
*
|
|
* @param string $message
|
|
* @param LogType $logtype
|
|
* @return void
|
|
* @throws \ErrorException If logtype is not handled.
|
|
*/
|
|
protected function consolePrint(string $message, LogType $logtype): void
|
|
{
|
|
if ($this->console) {
|
|
switch ($logtype->value) {
|
|
case LogType::debug()->value:
|
|
case LogType::info()->value:
|
|
case LogType::log()->value:
|
|
$this->console->info($message);
|
|
break;
|
|
case LogType::warning()->value:
|
|
$this->console->comment($message);
|
|
break;
|
|
case LogType::error()->value:
|
|
case LogType::critical()->value:
|
|
$this->console->error($message);
|
|
break;
|
|
default:
|
|
throw new \ErrorException(sprintf('%s : unhandled log type %s', get_class($this), $logtype->label));
|
|
}
|
|
}
|
|
}
|
|
}
|