ImportJob.php 1.8 KB

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