🆙 phpstan done an refactoring 🆙

This commit is contained in:
Remco
2026-01-20 20:40:28 +01:00
parent fccf4c2116
commit 981fd59af5
14 changed files with 148 additions and 126 deletions
@@ -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.');
},
);
}
}