Skip to content

Commit 73ffc64

Browse files
Add command to fetch ozu datas and use them locally
1 parent 9f5338d commit 73ffc64

4 files changed

Lines changed: 192 additions & 1 deletion

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"illuminate/contracts": "^11.0|^12.0",
2929
"intervention/image-laravel": "^1.0",
3030
"spatie/laravel-package-tools": "^1.14.0",
31-
"spatie/laravel-sluggable": "^3.6"
31+
"spatie/laravel-sluggable": "^3.6",
32+
"ext-zip": "*"
3233
},
3334
"require-dev": {
3435
"laravel/pint": "^1.0",

src/Client.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Http\Client\PendingRequest;
66
use Illuminate\Support\Facades\Http;
7+
use Storage;
78

89
class Client
910
{
@@ -42,6 +43,50 @@ public function seedFile(string $collection, int $id, string $field, string $pat
4243
)->getBody()?->getContents();
4344
}
4445

46+
public function downloadOzuDatabase(): ?string
47+
{
48+
$data = Http::withToken($this->apiKey)
49+
->baseUrl(
50+
sprintf(
51+
'%s/api/websites/%s',
52+
rtrim($this->apiHost, '/'),
53+
$this->apiKey,
54+
)
55+
)
56+
->acceptJson()
57+
->throw()
58+
->get('/database');
59+
60+
if($data->successful()) {
61+
Storage::put('tmp/ozu.sql', $data->body());
62+
return Storage::path('tmp/ozu.sql');
63+
}
64+
65+
return null;
66+
}
67+
68+
public function downloadOzuAssets(): ?string
69+
{
70+
$data = Http::withToken($this->apiKey)
71+
->baseUrl(
72+
sprintf(
73+
'%s/api/websites/%s',
74+
rtrim($this->apiHost, '/'),
75+
$this->apiKey,
76+
)
77+
)
78+
->acceptJson()
79+
->throw()
80+
->get('/assets');
81+
82+
if($data->successful()) {
83+
Storage::put('tmp/ozu-assets.zip', $data->body());
84+
return Storage::path('tmp/ozu-assets.zip');
85+
}
86+
87+
return null;
88+
}
89+
4590
public function apiKey(): ?string
4691
{
4792
return $this->apiKey;

src/Console/FetchDataFromOzu.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
}

src/OzuServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function configurePackage(Package $package): void
3030
->hasMigrations(['create_ozu_tables'])->runsMigrations()
3131
->hasCommands([
3232
Console\ConfigureCmsCommand::class,
33+
Console\FetchDataFromOzu::class,
3334
])
3435
->hasConfigFile();
3536
}

0 commit comments

Comments
 (0)