ImportJob.php 1.6 KB

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