ldc-dam/app/Console/Commands/DeleteMediaArchive.php

92 lines
2.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Console\Commands;
2022-04-20 15:23:31 +02:00
use App\Models\Media;
use App\Models\Sujet;
use Illuminate\Console\Command;
class DeleteMediaArchive extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'media:deletearchive';
/**
* The console command description.
*
* @var string
2022-04-20 15:23:31 +02:00
* @phpcs:disable Generic.Files.LineLength.TooLong
*/
protected $description = 'Suppression définitive de tous les médias (et leurs fichiers) qui ont été supprimés (en soft delete)';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
2022-04-20 15:23:31 +02:00
//phpcs:ignore Generic.Files.LineLength.TooLong
2022-04-19 16:07:27 +02:00
$this->info('Vous allez supprimer définitivement tous les médias (et leurs fichiers) qui ont été supprimés (en soft delete).');
if (!$this->confirm('Etes-vous sûr de vouloir lancer ce traitement ?')) {
return 0;
2022-04-19 16:07:27 +02:00
}
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Sujet> */
$subjects = Sujet::where('sujet_dam', true)->get();
2022-04-19 16:07:27 +02:00
$this->newLine();
$this->line('Liste des sujets disponibles');
$this->line('----------------------------');
$this->line('[0] Tous les sujets');
foreach ($subjects as $subject) {
$this->line("[{$subject->codeSujet}] {$subject->libelle}");
2022-04-19 16:07:27 +02:00
}
$sujetId = $this->askWithCompletion(
'Pour quel sujet de média souhaitez vous executer le traitement ?',
$subjects->pluck('codeSujet')->toArray(),
0
);
$this->newLine();
2022-04-19 16:07:27 +02:00
if ($sujetId != 0 and !$subjects->pluck('codeSujet')->contains(\intval($sujetId))) {
2022-04-19 16:07:27 +02:00
$this->error('Id de sujet incorrect');
return -1;
2022-04-19 16:07:27 +02:00
}
$sujetId = intval($sujetId);
$mediaCollection = Media::onlyTrashed()->with('sujet.formats');
if ($sujetId !== 0) {
$mediaCollection->where('sujet_id', $sujetId);
2022-04-19 16:07:27 +02:00
}
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Media> */
$mediaCollection = $mediaCollection->get();
2022-04-19 16:07:27 +02:00
if (!$mediaCollection->count()) {
2022-04-19 16:07:27 +02:00
$this->error('Aucun média a supprimer');
return -1;
2022-04-19 16:07:27 +02:00
}
$this->info($mediaCollection->count() . ' média(s) trouvé(s) - Début du traitement');
$bar = $this->output->createProgressBar($mediaCollection->count());
2022-04-19 16:07:27 +02:00
$bar->start();
foreach ($mediaCollection as $media) {
$media->prune();
2022-04-19 16:07:27 +02:00
$bar->advance();
}
$bar->finish();
$this->newLine();
2022-04-19 16:07:27 +02:00
$this->info('Fin du traitement');
}
}