You've already forked Epicnabbo-Catalogus-Updated-Daily
🆙 phpstan done an refactoring 🆙
This commit is contained in:
@@ -57,12 +57,19 @@ class ImportAdsData extends Command
|
||||
|
||||
private function getImageFiles(string $adsPath): array
|
||||
{
|
||||
return array_filter(scandir($adsPath), function ($file) use ($adsPath) {
|
||||
$filePath = $adsPath . DIRECTORY_SEPARATOR . $file;
|
||||
$files = scandir($adsPath);
|
||||
if (! is_array($files)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return is_file($filePath) &&
|
||||
in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), self::ALLOWED_EXTENSIONS);
|
||||
$filtered = array_filter($files, function (string $file) use ($adsPath): bool {
|
||||
$filePath = $adsPath . DIRECTORY_SEPARATOR . $file;
|
||||
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
||||
$ext = strtolower((string) $ext);
|
||||
return is_file($filePath) && in_array($ext, self::ALLOWED_EXTENSIONS, true);
|
||||
});
|
||||
|
||||
return array_values(array_map(fn ($f): string => (string) $f, $filtered));
|
||||
}
|
||||
|
||||
private function processFiles(array $files): void
|
||||
@@ -71,8 +78,8 @@ class ImportAdsData extends Command
|
||||
$existingImages = WebsiteAd::pluck('image')->toArray();
|
||||
|
||||
$newFiles = Collection::make($files)
|
||||
->filter(fn ($file) => ! in_array($file, $existingImages))
|
||||
->map(fn ($file) => ['image' => $file])
|
||||
->filter(fn ($file): bool => is_string($file) && ! in_array($file, $existingImages, true))
|
||||
->map(fn (string $file): array => ['image' => $file])
|
||||
->values();
|
||||
|
||||
$skippedCount = count($files) - $newFiles->count();
|
||||
@@ -80,9 +87,11 @@ class ImportAdsData extends Command
|
||||
$this->warn("Skipped {$skippedCount} existing files.");
|
||||
}
|
||||
|
||||
$newFiles->chunk(self::CHUNK_SIZE)->each(function ($chunk): void {
|
||||
WebsiteAd::insert($chunk->toArray());
|
||||
$this->info('Processed ' . $chunk->count() . ' files.');
|
||||
});
|
||||
$newFiles->chunk(self::CHUNK_SIZE)->each(
|
||||
function (Collection $chunk): void {
|
||||
WebsiteAd::insert($chunk->toArray());
|
||||
$this->info('Processed ' . $chunk->count() . ' files.');
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user