getOrDefault('ads_path_filesystem'); $adsPath = is_string($adsPathSetting) ? $adsPathSetting : ''; if (! $this->validatePath($adsPath)) { return; } $files = $this->getImageFiles($adsPath); if ($files === []) { $this->warn('No valid image files found in the ads directory.'); return; } $this->processFiles($files); $this->info('Ads data import completed successfully.'); } private function validatePath(?string $adsPath): bool { if (in_array($adsPath, [null, '', '0'], true)) { $this->error('Ads path is not configured in website_settings.'); return false; } if (! is_dir($adsPath)) { $this->error("The ads path '{$adsPath}' does not exist in the filesystem."); return false; } return true; } /** * @return array */ private function getImageFiles(string $adsPath): array { return array_filter(scandir($adsPath), function ($file) use ($adsPath) { $filePath = $adsPath . DIRECTORY_SEPARATOR . $file; return is_file($filePath) && in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), self::ALLOWED_EXTENSIONS); }); } /** * @param array $files */ private function processFiles(array $files): void { // Get existing images to avoid duplicates $existingImages = WebsiteAd::query()->pluck('image')->toArray(); $newFiles = Collection::make($files) ->filter(fn ($file) => ! in_array($file, $existingImages)) ->map(fn ($file) => ['image' => $file]) ->values(); $skippedCount = count($files) - $newFiles->count(); if ($skippedCount > 0) { $this->warn("Skipped {$skippedCount} existing files."); } $newFiles->chunk(self::CHUNK_SIZE)->each(function ($chunk) { WebsiteAd::insert($chunk->all()); $this->info('Processed ' . $chunk->count() . ' files.'); }); } }