Skip to content

Commit e0f861f

Browse files
committed
chore: cleanup code
1 parent 181b778 commit e0f861f

File tree

3 files changed

+76
-19
lines changed

3 files changed

+76
-19
lines changed

app/Jobs/ProcessModPackFile.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
use App\Events\ModPack\ModPackProcessProgress;
66
use App\Models\Modpack;
7+
use App\Services\Modpacks\ModpackUpdaterService;
78
use Illuminate\Bus\Batchable;
89
use Illuminate\Bus\Queueable;
910
use Illuminate\Contracts\Queue\ShouldQueue;
1011
use Illuminate\Foundation\Bus\Dispatchable;
1112
use Illuminate\Queue\InteractsWithQueue;
1213
use Illuminate\Queue\SerializesModels;
13-
use Illuminate\Support\Facades\Redis;
1414
use Illuminate\Support\Facades\Storage;
1515
use Illuminate\Support\Str;
1616
use Throwable;
@@ -64,19 +64,15 @@ public function handle()
6464
$this->modpack->path,
6565
$this->modpack->name
6666
);
67-
$filePathPrevented = Str::of($filePath)
68-
->replace('.', '-');
6967

70-
Redis::hIncrBy("modpackManifestInfoUpdate:{$this->modpack->id}", 'size', $fileSize);
71-
Redis::hIncrBy("modpackManifestInfoUpdate:{$this->modpack->id}", 'files', 1);
72-
73-
Redis::hSet("modpackManifestUpdate:{$this->modpack->id}", $filePathPrevented, json_encode([
74-
'url' => $fileUrl,
75-
'size' => $fileSize,
76-
'name' => $fileName,
77-
'path' => $filePath,
78-
'sha256' => $fileHash
79-
]));
68+
ModpackUpdaterService::fileProcessed(
69+
modPack: $this->modpack,
70+
fileName: $fileName,
71+
fileSize: $fileSize,
72+
fileUrl: $fileUrl,
73+
filePath: $filePath,
74+
fileHash: $fileHash
75+
);
8076

8177
ModPackProcessProgress::broadcast($this->modpack, $this->batch()->progress());
8278
}

app/Listeners/Modpack/StartModPackUpdate.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88
use App\Events\ModPack\ModPackProcessStarted;
99
use App\Events\ModPack\ModPackUpdateRequested;
1010
use App\Jobs\ProcessModPackFile;
11+
use App\Services\Modpacks\ModpackUpdaterService;
1112
use Exception;
1213
use Illuminate\Bus\Batch;
1314
use Illuminate\Contracts\Queue\ShouldQueue;
1415
use Illuminate\Queue\InteractsWithQueue;
1516
use Illuminate\Support\Facades\Bus;
1617
use Illuminate\Support\Facades\Log;
17-
use Illuminate\Support\Facades\Redis;
1818
use Illuminate\Support\Facades\Storage;
1919
use Throwable;
20-
use function React\Promise\map;
2120

2221
class StartModPackUpdate implements ShouldQueue
2322
{
@@ -40,8 +39,7 @@ public function handle(ModPackUpdateRequested $event)
4039
return true;
4140
}
4241

43-
Redis::del("modpackManifestUpdate:$modpack->id");
44-
Redis::del("modpackManifestInfoUpdate:$modpack->id");
42+
ModpackUpdaterService::flush($modpack);
4543

4644
$jobs = $files->map(fn($file) => new ProcessModPackFile($modpack, $file));
4745
$batch = Bus::batch($jobs->toArray())
@@ -50,8 +48,8 @@ public function handle(ModPackUpdateRequested $event)
5048
ModPackProcessCanceled::broadcast($modpack);
5149
return;
5250
}
53-
$manifest = Redis::hGetAll("modpackManifestUpdate:$modpack->id");
54-
$manifestInfo = Redis::hGetAll("modpackManifestInfoUpdate:$modpack->id");
51+
52+
[$manifest, $manifestInfo] = ModpackUpdaterService::getUpdate($modpack);
5553

5654
if (!empty($manifest) && !empty($manifestInfo)) {
5755
$modpack->update([
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace App\Services\Modpacks;
4+
5+
use App\Models\Modpack;
6+
use Illuminate\Support\Facades\Redis;
7+
use Illuminate\Support\Str;
8+
9+
class ModpackUpdaterService
10+
{
11+
private static function getManifestKey(Modpack $modpack): string
12+
{
13+
return "modPack:manifestUpdate:$modpack->id";
14+
}
15+
16+
private static function getManifestInfoKey(Modpack $modpack): string
17+
{
18+
return "modPack:manifestInfoUpdate:$modpack->id";
19+
}
20+
21+
22+
static public function flush(Modpack $modpack): void
23+
{
24+
Redis::del(self::getManifestKey($modpack));
25+
Redis::del(self::getManifestInfoKey($modpack));
26+
}
27+
28+
static public function getUpdate(Modpack $modpack): array
29+
{
30+
return [
31+
Redis::hGetAll(self::getManifestKey($modpack)),
32+
Redis::hGetAll(self::getManifestInfoKey($modpack)),
33+
];
34+
}
35+
36+
public static function fileProcessed(
37+
Modpack $modPack,
38+
string $fileName,
39+
int $fileSize,
40+
string $fileUrl,
41+
string $filePath,
42+
string $fileHash,
43+
)
44+
{
45+
$manifestKey = self::getManifestKey($modPack);
46+
$manifestInfoKey = self::getManifestInfoKey($modPack);
47+
48+
$safeFilePath = Str::of($filePath)
49+
->replace('.', '-');
50+
51+
52+
Redis::hIncrBy($manifestInfoKey, 'size', $fileSize);
53+
Redis::hIncrBy($manifestInfoKey, 'files', 1);
54+
55+
Redis::hSet($manifestKey, $safeFilePath, json_encode([
56+
'url' => $fileUrl,
57+
'size' => $fileSize,
58+
'name' => $fileName,
59+
'path' => $filePath,
60+
'sha256' => $fileHash
61+
]));
62+
}
63+
}

0 commit comments

Comments
 (0)