|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DevWizard\Textify\Events; |
| 6 | + |
| 7 | +use DevWizard\Textify\DTOs\TextifyMessage; |
| 8 | +use Illuminate\Broadcasting\InteractsWithSockets; |
| 9 | +use Illuminate\Foundation\Events\Dispatchable; |
| 10 | +use Illuminate\Queue\SerializesModels; |
| 11 | + |
| 12 | +/** |
| 13 | + * Event fired when a queued Textify job fails |
| 14 | + * |
| 15 | + * This event is dispatched when a SendTextifyJob fails to process, |
| 16 | + * allowing applications to handle job failures with custom logic |
| 17 | + * such as retries, notifications, or logging. |
| 18 | + */ |
| 19 | +class TextifyJobFailed |
| 20 | +{ |
| 21 | + use Dispatchable, InteractsWithSockets, SerializesModels; |
| 22 | + |
| 23 | + /** |
| 24 | + * Create a new event instance. |
| 25 | + * |
| 26 | + * @param TextifyMessage $message The SMS message that failed to send |
| 27 | + * @param string $provider The provider that was being used |
| 28 | + * @param \Throwable $exception The exception that caused the failure |
| 29 | + */ |
| 30 | + public function __construct( |
| 31 | + public readonly TextifyMessage $message, |
| 32 | + public readonly string $provider, |
| 33 | + public readonly \Throwable $exception |
| 34 | + ) {} |
| 35 | + |
| 36 | + /** |
| 37 | + * Get the message that failed to send |
| 38 | + */ |
| 39 | + public function getMessage(): TextifyMessage |
| 40 | + { |
| 41 | + return $this->message; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Get the provider that was being used |
| 46 | + */ |
| 47 | + public function getProvider(): string |
| 48 | + { |
| 49 | + return $this->provider; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Get the exception that caused the failure |
| 54 | + */ |
| 55 | + public function getException(): \Throwable |
| 56 | + { |
| 57 | + return $this->exception; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get the recipient phone number |
| 62 | + */ |
| 63 | + public function getRecipient(): string |
| 64 | + { |
| 65 | + return $this->message->getTo(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get the message content |
| 70 | + */ |
| 71 | + public function getMessageContent(): string |
| 72 | + { |
| 73 | + return $this->message->getMessage(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get the sender ID |
| 78 | + */ |
| 79 | + public function getSender(): ?string |
| 80 | + { |
| 81 | + return $this->message->getFrom(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Get the error message |
| 86 | + */ |
| 87 | + public function getErrorMessage(): string |
| 88 | + { |
| 89 | + return $this->exception->getMessage(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get the error code if available |
| 94 | + */ |
| 95 | + public function getErrorCode(): ?string |
| 96 | + { |
| 97 | + return $this->exception->getCode() ? (string) $this->exception->getCode() : null; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Get metadata for logging or debugging |
| 102 | + */ |
| 103 | + public function getMetadata(): array |
| 104 | + { |
| 105 | + return [ |
| 106 | + 'message_id' => $this->message->getId(), |
| 107 | + 'to' => $this->message->getTo(), |
| 108 | + 'from' => $this->message->getFrom(), |
| 109 | + 'provider' => $this->provider, |
| 110 | + 'error_message' => $this->exception->getMessage(), |
| 111 | + 'error_code' => $this->exception->getCode(), |
| 112 | + 'error_file' => $this->exception->getFile(), |
| 113 | + 'error_line' => $this->exception->getLine(), |
| 114 | + 'timestamp' => now()->toISOString(), |
| 115 | + ]; |
| 116 | + } |
| 117 | +} |
0 commit comments