| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\Events; |
| 4 |
|
| 5 |
use Throwable; |
| 6 |
|
| 7 |
/** |
| 8 |
* Event dispatched when alt text generation fails. |
| 9 |
* |
| 10 |
* This event is fired when an AI provider fails to generate alt text |
| 11 |
* for an image. Listeners can use this for error logging, notifications, |
| 12 |
* or triggering fallback mechanisms. |
| 13 |
* |
| 14 |
* @package AATXT\App\Events |
| 15 |
*/ |
| 16 |
final class AltTextGenerationFailedEvent |
| 17 |
{ |
| 18 |
/** |
| 19 |
* WordPress attachment ID |
| 20 |
* |
| 21 |
* @var int |
| 22 |
*/ |
| 23 |
private $imageId; |
| 24 |
|
| 25 |
/** |
| 26 |
* Provider name that failed |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
private $provider; |
| 31 |
|
| 32 |
/** |
| 33 |
* The exception that caused the failure |
| 34 |
* |
| 35 |
* @var Throwable |
| 36 |
*/ |
| 37 |
private $exception; |
| 38 |
|
| 39 |
/** |
| 40 |
* Timestamp when the event occurred |
| 41 |
* |
| 42 |
* @var int |
| 43 |
*/ |
| 44 |
private $timestamp; |
| 45 |
|
| 46 |
/** |
| 47 |
* Whether the error has been handled/logged |
| 48 |
* |
| 49 |
* @var bool |
| 50 |
*/ |
| 51 |
private $handled = false; |
| 52 |
|
| 53 |
/** |
| 54 |
* Constructor |
| 55 |
* |
| 56 |
* @param int $imageId WordPress attachment ID |
| 57 |
* @param string $provider Provider name (e.g., 'OpenAI Vision', 'Anthropic') |
| 58 |
* @param Throwable $exception The exception that caused the failure |
| 59 |
*/ |
| 60 |
public function __construct(int $imageId, string $provider, Throwable $exception) |
| 61 |
{ |
| 62 |
$this->imageId = $imageId; |
| 63 |
$this->provider = $provider; |
| 64 |
$this->exception = $exception; |
| 65 |
$this->timestamp = time(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get the image ID. |
| 70 |
* |
| 71 |
* @return int |
| 72 |
*/ |
| 73 |
public function getImageId(): int |
| 74 |
{ |
| 75 |
return $this->imageId; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get the provider name. |
| 80 |
* |
| 81 |
* @return string |
| 82 |
*/ |
| 83 |
public function getProvider(): string |
| 84 |
{ |
| 85 |
return $this->provider; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get the exception that caused the failure. |
| 90 |
* |
| 91 |
* @return Throwable |
| 92 |
*/ |
| 93 |
public function getException(): Throwable |
| 94 |
{ |
| 95 |
return $this->exception; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get the error message. |
| 100 |
* |
| 101 |
* @return string |
| 102 |
*/ |
| 103 |
public function getErrorMessage(): string |
| 104 |
{ |
| 105 |
return $this->exception->getMessage(); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get the timestamp when the event occurred. |
| 110 |
* |
| 111 |
* @return int Unix timestamp |
| 112 |
*/ |
| 113 |
public function getTimestamp(): int |
| 114 |
{ |
| 115 |
return $this->timestamp; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Mark the error as handled. |
| 120 |
* |
| 121 |
* This can be used by listeners to indicate they have |
| 122 |
* processed/logged the error to prevent duplicate handling. |
| 123 |
* |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public function markAsHandled(): void |
| 127 |
{ |
| 128 |
$this->handled = true; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Check if the error has been handled. |
| 133 |
* |
| 134 |
* @return bool |
| 135 |
*/ |
| 136 |
public function isHandled(): bool |
| 137 |
{ |
| 138 |
return $this->handled; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get a formatted error message for logging. |
| 143 |
* |
| 144 |
* @return string |
| 145 |
*/ |
| 146 |
public function getFormattedErrorMessage(): string |
| 147 |
{ |
| 148 |
return sprintf( |
| 149 |
'%s - %s', |
| 150 |
$this->provider, |
| 151 |
$this->exception->getMessage() |
| 152 |
); |
| 153 |
} |
| 154 |
} |
| 155 |
|