ImportJob.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Jobs\Import;
  3. use App\Events\SendWebSocketMessageEvent;
  4. use App\Models\Import;
  5. use App\Services\ImportOrdersService;
  6. use App\Services\ImportReclamationsService;
  7. use Exception;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Queue\Queueable;
  10. use Illuminate\Support\Facades\Log;
  11. class ImportJob implements ShouldQueue
  12. {
  13. use Queueable;
  14. /**
  15. * Create a new job instance.
  16. */
  17. public function __construct(
  18. private readonly Import $import,
  19. private readonly int $userId,
  20. )
  21. {}
  22. /**
  23. * Execute the job.
  24. */
  25. public function handle(): void
  26. {
  27. try {
  28. switch($this->import->type) {
  29. case 'orders':
  30. (new ImportOrdersService($this->import))->handle();
  31. break;
  32. case 'reclamations':
  33. (new ImportReclamationsService($this->import))->handle();
  34. break;
  35. }
  36. Log::info('Import ' . $this->import->type. ' job done!');
  37. event(new SendWebSocketMessageEvent('Импорт завершён!', $this->userId, ['path' => $this->import->filename, 'type' => $this->import->type]));
  38. } catch (Exception $e) {
  39. Log::info('Import ' . $this->import->type. ' job failed! ERROR: ' . $e->getMessage());
  40. event(new SendWebSocketMessageEvent('Ошибка импорта! ' . $e->getMessage(), $this->userId, ['error' => $e->getMessage()]));
  41. }
  42. }
  43. }