GenerateSvgJob.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Question;
  4. use App\Models\QuestionAsset;
  5. use App\Services\SvgConverterService;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. class GenerateSvgJob implements ShouldQueue
  12. {
  13. use Dispatchable;
  14. use InteractsWithQueue;
  15. use Queueable;
  16. use SerializesModels;
  17. public function __construct(public readonly int $questionId)
  18. {
  19. }
  20. public function handle(SvgConverterService $service): void
  21. {
  22. $question = Question::find($this->questionId);
  23. if (!$question) {
  24. return;
  25. }
  26. $assets = $service->extractSvgAssets($question->stem ?? '');
  27. foreach ($assets as $asset) {
  28. QuestionAsset::firstOrCreate([
  29. 'question_id' => $question->id,
  30. 'asset_type' => $asset['type'] ?? 'svg',
  31. 'path' => $asset['path'] ?? '',
  32. ], [
  33. 'meta' => $asset['meta'] ?? [],
  34. ]);
  35. }
  36. }
  37. }