SyncQuestionAssetsCommand.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Question;
  4. use App\Models\QuestionAsset;
  5. use App\Services\SvgConverterService;
  6. use Illuminate\Console\Command;
  7. class SyncQuestionAssetsCommand extends Command
  8. {
  9. protected $signature = 'question:sync-assets';
  10. protected $description = 'Extract and sync SVG/image assets for questions';
  11. public function handle(SvgConverterService $svgService): int
  12. {
  13. $questions = Question::query()->get(['id', 'stem']);
  14. foreach ($questions as $question) {
  15. $assets = $svgService->extractSvgAssets($question->stem ?? '');
  16. foreach ($assets as $asset) {
  17. QuestionAsset::firstOrCreate([
  18. 'question_id' => $question->id,
  19. 'asset_type' => $asset['type'] ?? 'svg',
  20. 'path' => $asset['path'] ?? '',
  21. ], [
  22. 'meta' => $asset['meta'] ?? [],
  23. ]);
  24. }
  25. }
  26. $this->info('Question assets synced.');
  27. return self::SUCCESS;
  28. }
  29. }