File: /var/www/limestate-admin/app/Console/Commands/Thumbnail/Clear.php
<?php
namespace App\Console\Commands\Thumbnail;
use App\Collections\AttachmentThumbCollection;
use App\Models\Attachment\AttachmentThumb;
use Illuminate\Console\Command;
use Symfony\Component\Console\Command\Command as CommandAlias;
class Clear extends Command
{
protected $signature = 'thumbnail:clear';
protected $description = 'Удаляет миниатюры которых нет';
public function handle(): int
{
$this->info($this->description);
/** @var AttachmentThumbCollection $attachmentThumbs */
$attachmentThumbs = AttachmentThumb::query()->cursor();
$count = $attachmentThumbs->count();
if (empty($count)) {
$this->info('Миниатюр не найдено');
return CommandAlias::SUCCESS;
}
$this->info('Будет обработано: ' . $count);
$deleted = 0;
$progressBar = $this->output->createProgressBar($count);
foreach ($attachmentThumbs as $attachmentThumb) {
$progressBar->advance();
$path = $attachmentThumb->getRealPath();
if (!file_exists($path)) {
$attachmentThumb->delete();
$deleted++;
}
}
$progressBar->finish();
$this->line('');
$this->info('Удалено: ' . $deleted);
return CommandAlias::SUCCESS;
}
}