|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace EragLaravelDisposableEmail\Commands; |
| 6 | + |
| 7 | +use EragLaravelDisposableEmail\Support\Cache; |
| 8 | +use EragLaravelDisposableEmail\Support\ResponseParser; |
| 9 | +use EragLaravelDisposableEmail\Support\UrlList; |
| 10 | +use Illuminate\Console\Command; |
| 11 | +use Illuminate\Http\Client\Response; |
| 12 | +use Illuminate\Support\Facades\File; |
| 13 | +use Illuminate\Support\Facades\Http; |
| 14 | +use Throwable; |
| 15 | + |
| 16 | +class Sync extends Command |
| 17 | +{ |
| 18 | + protected $signature = 'erag:sync-disposable-email-list'; |
| 19 | + |
| 20 | + protected $description = 'Fetch and update the disposable email domains list'; |
| 21 | + |
| 22 | + public function handle(): int |
| 23 | + { |
| 24 | + Cache::clear(); |
| 25 | + |
| 26 | + $remoteUrls = $this->remoteUrls(); |
| 27 | + $directory = config('disposable-email.blacklist_file'); |
| 28 | + |
| 29 | + if (! is_string($directory) || trim($directory) === '') { |
| 30 | + $this->error('Invalid disposable-email.blacklist_file config value.'); |
| 31 | + |
| 32 | + return self::FAILURE; |
| 33 | + } |
| 34 | + |
| 35 | + if ($remoteUrls === []) { |
| 36 | + $this->error('No valid remote URLs configured in disposable-email.remote_url.'); |
| 37 | + |
| 38 | + return self::FAILURE; |
| 39 | + } |
| 40 | + |
| 41 | + if (! $this->ensureDirectoryExists($directory)) { |
| 42 | + return self::FAILURE; |
| 43 | + } |
| 44 | + |
| 45 | + $synced = 0; |
| 46 | + $failed = 0; |
| 47 | + |
| 48 | + foreach ($remoteUrls as $url) { |
| 49 | + $result = $this->syncUrl($url, $directory); |
| 50 | + |
| 51 | + if ($result) { |
| 52 | + $synced++; |
| 53 | + } else { |
| 54 | + $failed++; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + $this->newLine(); |
| 59 | + $this->info("Sync complete. Synced: {$synced}. Failed: {$failed}."); |
| 60 | + |
| 61 | + return $synced > 0 ? self::SUCCESS : self::FAILURE; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @return array<int, string> |
| 66 | + */ |
| 67 | + protected function remoteUrls(): array |
| 68 | + { |
| 69 | + return UrlList::from(config('disposable-email.remote_url', [])); |
| 70 | + } |
| 71 | + |
| 72 | + protected function ensureDirectoryExists(string $directory): bool |
| 73 | + { |
| 74 | + try { |
| 75 | + if (! File::exists($directory)) { |
| 76 | + File::makeDirectory($directory, 0755, true); |
| 77 | + $this->info("Directory created at: {$directory}"); |
| 78 | + } |
| 79 | + |
| 80 | + return true; |
| 81 | + } catch (Throwable $exception) { |
| 82 | + $this->error("Unable to create blacklist directory [{$directory}]: {$exception->getMessage()}"); |
| 83 | + |
| 84 | + return false; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + protected function syncUrl(string $url, string $directory): bool |
| 89 | + { |
| 90 | + $this->line("Fetching: {$url}"); |
| 91 | + |
| 92 | + try { |
| 93 | + $response = $this->fetch($url); |
| 94 | + } catch (Throwable $exception) { |
| 95 | + $this->error("Request failed for [{$url}]: {$exception->getMessage()}"); |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + if (! $response->successful()) { |
| 101 | + $this->error("Failed to fetch [{$url}]. HTTP status: {$response->status()}."); |
| 102 | + |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + $domains = ResponseParser::parse($response->body()); |
| 107 | + |
| 108 | + if ($domains === []) { |
| 109 | + $this->warn("No valid domains found in [{$url}]. Skipping write."); |
| 110 | + |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + $filePath = $directory.DIRECTORY_SEPARATOR.$this->filename($url); |
| 115 | + try { |
| 116 | + $this->write($filePath, $domains); |
| 117 | + } catch (Throwable $exception) { |
| 118 | + $this->error("Unable to write [{$filePath}]: {$exception->getMessage()}"); |
| 119 | + |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + $this->info('Saved '.number_format(count($domains))." domains to {$filePath}"); |
| 124 | + |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + protected function syncTimeout(): int |
| 129 | + { |
| 130 | + $timeout = config('disposable-email.sync_timeout', 30); |
| 131 | + |
| 132 | + if (! is_numeric($timeout)) { |
| 133 | + return 30; |
| 134 | + } |
| 135 | + |
| 136 | + $timeout = (int) $timeout; |
| 137 | + |
| 138 | + return $timeout > 0 ? $timeout : 30; |
| 139 | + } |
| 140 | + |
| 141 | + private function filename(string $url): string |
| 142 | + { |
| 143 | + $path = parse_url($url, PHP_URL_PATH); |
| 144 | + $name = is_string($path) ? pathinfo($path, PATHINFO_FILENAME) : ''; |
| 145 | + $name = strtolower((string) preg_replace('/[^a-zA-Z0-9_-]+/', '-', $name)); |
| 146 | + $name = trim($name, '-_'); |
| 147 | + |
| 148 | + return ($name === '' ? 'disposable-domains' : $name).'.txt'; |
| 149 | + } |
| 150 | + |
| 151 | + private function fetch(string $url): Response |
| 152 | + { |
| 153 | + return Http::timeout($this->syncTimeout())->retry(2, 500)->get($url); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @param array<int, string> $domains |
| 158 | + */ |
| 159 | + private function write(string $path, array $domains): void |
| 160 | + { |
| 161 | + File::put($path, implode(PHP_EOL, $domains).PHP_EOL); |
| 162 | + } |
| 163 | +} |
0 commit comments