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

77 lines
2 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Console\Commands;
use App\Models\Media;
2022-04-20 15:23:31 +02:00
use Illuminate\Console\Command;
class ReindexElasticMedia extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'media: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('ES: Test présence index...');
if (Media::indexExists()) {
$this->info('ES: Suppression index...');
Media::deleteIndex();
}
$this->info('ES: Création index...');
Media::createIndex();
$pageNumber = 0;
$perPage = 200;
/** @var \Illuminate\Pagination\LengthAwarePaginator $mediaCollection */
$mediaCollection = Media::paginate($perPage);
if (!$mediaCollection->total()) {
$this->info('Pas de medias');
}
$bar = $this->output->createProgressBar(count($mediaCollection));
$bar->start($mediaCollection->total());
// * LOOP
do {
if ($pageNumber > 0) {
/** @var \Illuminate\Pagination\LengthAwarePaginator $mediaCollection */
$mediaCollection = Media::paginate($perPage, ['*'], 'page', $pageNumber);
}
/** @var \App\Models\Media $media */
foreach ($mediaCollection->getCollection() as $media) {
/** @throws \RuntimeException */
$media->addToElasticSearch();
$bar->advance();
}
++$pageNumber;
} while ($mediaCollection->hasMorePages());
$bar->finish();
$this->newLine();
$this->info('Fin du traitement');
}
}