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

67 lines
1.6 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Media;
class ReindexElasticMedia extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'medias:reindex';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Reindex la base médias dans Elasticsearch';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('Reindexation de la base médias dans Elasticsearch...');
$this->info('Test Présence...');
if (Media::indexExists()) {
$this->info('Suppression index...');
Media::deleteIndex();
}
$this->info('Création...');
Media::createIndex();
$pageNumber = 0;
$perPage = 200;
/** @var \Illuminate\Pagination\LengthAwarePaginator $medias */
$medias = Media::paginate($perPage);
$bar = $this->output->createProgressBar(count($medias));
$bar->start($medias->total());
// * LOOP
do {
if ($pageNumber > 0) {
/** @var \Illuminate\Pagination\LengthAwarePaginator $medias */
$medias = Media::paginate($perPage, ['*'], 'page', $pageNumber);
}
foreach ($medias as $media) {
$media->addToIndex();
$bar->advance();
}
++$pageNumber;
} while ($medias->hasMorePages());
$bar->finish();
$this->line('');
$this->info('Fin du traitement');
}
}