|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Code16\OzuClient\Console; |
| 4 | + |
| 5 | +use Code16\OzuClient\Client; |
| 6 | +use Illuminate\Console\Command; |
| 7 | +use Illuminate\Support\Facades\Artisan; |
| 8 | +use Symfony\Component\Process\Process; |
| 9 | +use ZipArchive; |
| 10 | + |
| 11 | +class FetchDataFromOzu extends Command |
| 12 | +{ |
| 13 | + protected $signature = 'ozu:fetch-ozu-data'; |
| 14 | + |
| 15 | + protected $description = 'Gets Ozu’s CMS data and replaces your local database and assets with it.'; |
| 16 | + |
| 17 | + private string $databaseDumpPath; |
| 18 | + private string $assetsZipPath; |
| 19 | + private string $assetsExtractPath; |
| 20 | + |
| 21 | + public function handle(Client $ozuClient): int |
| 22 | + { |
| 23 | + |
| 24 | + $this->warn('/!\\ This action will erase your local database and assets.'); |
| 25 | + |
| 26 | + if (!$this->confirm("Are you sure you want to continue? This cannot be undone.")) { |
| 27 | + return self::SUCCESS; |
| 28 | + } |
| 29 | + |
| 30 | + $this->initializePaths(); |
| 31 | + |
| 32 | + if (!$this->downloadDatabase($ozuClient)) { |
| 33 | + return self::FAILURE; |
| 34 | + } |
| 35 | + |
| 36 | + if (!$this->importDatabase()) { |
| 37 | + return self::FAILURE; |
| 38 | + } |
| 39 | + |
| 40 | + if (!$this->downloadAssets($ozuClient)) { |
| 41 | + return self::FAILURE; |
| 42 | + } |
| 43 | + |
| 44 | + if (!$this->extractAssets()) { |
| 45 | + return self::FAILURE; |
| 46 | + } |
| 47 | + |
| 48 | + $this->cleanTemporaryFiles(); |
| 49 | + $this->info('✅ Ozu data successfully imported.'); |
| 50 | + return self::SUCCESS; |
| 51 | + } |
| 52 | + |
| 53 | + private function initializePaths(): void |
| 54 | + { |
| 55 | + $this->databaseDumpPath = storage_path('app/tmp/ozu.sql'); |
| 56 | + $this->assetsZipPath = storage_path('app/tmp/ozu-assets.zip'); |
| 57 | + $this->assetsExtractPath = storage_path('app/public/data/' . config('ozu-client.website_key') . '/'); |
| 58 | + } |
| 59 | + |
| 60 | + private function downloadDatabase(Client $ozuClient): bool |
| 61 | + { |
| 62 | + $this->info('⬇️ Downloading Ozu database...'); |
| 63 | + $dbPath = $ozuClient->downloadOzuDatabase(); |
| 64 | + |
| 65 | + if (!$dbPath) { |
| 66 | + $this->error('❌ Failed to download the Ozu database.'); |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + $this->databaseDumpPath = $dbPath; |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + private function importDatabase(): bool |
| 75 | + { |
| 76 | + $this->info('🧹 Wiping local database...'); |
| 77 | + Artisan::call('db:wipe'); |
| 78 | + |
| 79 | + $this->info('🛠 Importing Ozu database...'); |
| 80 | + $config = config('database.connections.mysql'); |
| 81 | + |
| 82 | + $process = Process::fromShellCommandline(sprintf( |
| 83 | + 'mysql -h%s -P%s -u%s %s < %s', |
| 84 | + $config['host'], |
| 85 | + $config['port'], |
| 86 | + $config['username'], |
| 87 | + $config['database'], |
| 88 | + $this->databaseDumpPath |
| 89 | + )); |
| 90 | + |
| 91 | + $process->setEnv(['MYSQL_PWD' => $config['password']]); |
| 92 | + $process->run(); |
| 93 | + |
| 94 | + if (!$process->isSuccessful()) { |
| 95 | + $this->error("❌ Error importing the database:"); |
| 96 | + $this->error($process->getErrorOutput()); |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + $this->info("✅ Database imported successfully."); |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + private function downloadAssets(Client $ozuClient): bool |
| 105 | + { |
| 106 | + $this->info('⬇️ Downloading Ozu assets...'); |
| 107 | + $zipPath = $ozuClient->downloadOzuAssets(); |
| 108 | + |
| 109 | + if (!$zipPath) { |
| 110 | + $this->error('❌ Failed to download Ozu assets.'); |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + $this->assetsZipPath = $zipPath; |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + private function extractAssets(): bool |
| 119 | + { |
| 120 | + $this->info('📦 Extracting Ozu assets...'); |
| 121 | + |
| 122 | + try { |
| 123 | + $zip = new ZipArchive(); |
| 124 | + if ($zip->open($this->assetsZipPath) === true) { |
| 125 | + $zip->extractTo($this->assetsExtractPath); |
| 126 | + $zip->close(); |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + $this->error('❌ Failed to open the zip file.'); |
| 131 | + } catch (\Throwable $e) { |
| 132 | + $this->error('❌ Exception while extracting assets:'); |
| 133 | + $this->error($e->getMessage()); |
| 134 | + } |
| 135 | + |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + private function cleanTemporaryFiles(): void |
| 140 | + { |
| 141 | + @unlink($this->databaseDumpPath); |
| 142 | + @unlink($this->assetsZipPath); |
| 143 | + } |
| 144 | +} |
0 commit comments