feat(#16): refactor

This commit is contained in:
Benjamin Nussbaum 2025-05-12 07:19:03 +02:00
parent 580a5833a8
commit cc63c8cce2
9 changed files with 148 additions and 75 deletions

View file

@ -36,8 +36,7 @@ class ScreenGeneratorCommand extends Command
return 1;
}
GenerateScreenJob::dispatchSync($deviceId, $markup);
GenerateScreenJob::dispatchSync($deviceId, null, $markup);
$this->info('Screen generation job finished.');

View file

@ -1,37 +0,0 @@
<?php
namespace App\Jobs;
use App\Models\Plugin;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class GeneratePluginJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*/
public function __construct(
private readonly int $pluginId,
private readonly string $markup
) {}
/**
* Execute the job.
*/
public function handle(): void
{
$newImageUuid = CommonFunctions::generateImage($this->markup);
Plugin::find($this->pluginId)->update(['current_image' => $newImageUuid]);
\Log::info("Plugin $this->pluginId: updated with new image: $newImageUuid");
CommonFunctions::cleanupFolder();
}
}

View file

@ -3,6 +3,8 @@
namespace App\Jobs;
use App\Models\Device;
use App\Models\Plugin;
use App\Services\ImageGenerationService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@ -18,6 +20,7 @@ class GenerateScreenJob implements ShouldQueue
*/
public function __construct(
private readonly int $deviceId,
private readonly ?int $pluginId,
private readonly string $markup
) {}
@ -26,11 +29,16 @@ class GenerateScreenJob implements ShouldQueue
*/
public function handle(): void
{
$newImageUuid = CommonFunctions::generateImage($this->markup);
$newImageUuid = ImageGenerationService::generateImage($this->markup);
Device::find($this->deviceId)->update(['current_screen_image' => $newImageUuid]);
\Log::info("Device $this->deviceId: updated with new image: $newImageUuid");
CommonFunctions::cleanupFolder();
if ($this->pluginId) {
// cache current image
Plugin::find($this->pluginId)->update(['current_image' => $newImageUuid]);
}
ImageGenerationService::cleanupFolder();
}
}

View file

@ -17,7 +17,6 @@ class Plugin extends Model
'data_payload' => 'json',
'data_payload_updated_at' => 'datetime',
'is_native' => 'boolean',
'current_image' => 'string',
];
protected static function boot()

View file

@ -1,6 +1,6 @@
<?php
namespace App\Jobs;
namespace App\Services;
use App\Models\Device;
use App\Models\Plugin;
@ -9,7 +9,7 @@ use Ramsey\Uuid\Uuid;
use Spatie\Browsershot\Browsershot;
use Wnx\SidecarBrowsershot\BrowsershotLambda;
class CommonFunctions
class ImageGenerationService
{
public static function generateImage(string $markup): string {
$uuid = Uuid::uuid4()->toString();
@ -37,7 +37,7 @@ class CommonFunctions
}
try {
CommonFunctions::convertToBmpImageMagick($pngPath, $bmpPath);
ImageGenerationService::convertToBmpImageMagick($pngPath, $bmpPath);
} catch (\ImagickException $e) {
throw new \RuntimeException('Failed to convert image to BMP: '.$e->getMessage(), 0, $e);
}