AsyncExceptionRecorder.php 1022 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace App\Support;
  3. use App\Models\SystemException;
  4. class AsyncExceptionRecorder
  5. {
  6. public function __construct(
  7. private string $type,
  8. private string $message,
  9. private string $file,
  10. private int $line,
  11. private string $trace,
  12. private array $requestData,
  13. private ?int $userId
  14. ) {}
  15. public static function dispatch(...$args): self
  16. {
  17. $instance = new static(...$args);
  18. register_shutdown_function(function () use ($instance) {
  19. $instance->handle();
  20. });
  21. return $instance;
  22. }
  23. public function handle(): void
  24. {
  25. SystemException::create([
  26. 'type' => $this->type,
  27. 'message' => $this->message,
  28. 'file' => $this->file,
  29. 'line' => $this->line,
  30. 'trace' => $this->trace,
  31. 'request_data' => $this->requestData,
  32. 'user_id' => $this->userId,
  33. 'status' => SystemException::STATUS_PENDING,
  34. ]);
  35. }
  36. }